Fragment 和 FragmentList 不显示 Android Studio

Posted

技术标签:

【中文标题】Fragment 和 FragmentList 不显示 Android Studio【英文标题】:Fragment and FragmentList doesn't display Android Studio 【发布时间】:2015-06-28 20:05:26 【问题描述】:

我想用 FragmentActivity 实现 Tab 栏列表。我的目标是 API 14 (4.0)。我的应用工作正常,但标签栏列表没有出现...我不知道我做错了什么...感谢您的帮助!

MainActivity.java:

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.annotation.TargetApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.html;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static android.location.Location.convert;


public class MainActivity extends ActionBarActivity implements
        ActionBar.TabListener 

    public static final String TAG = MainActivity.class.getSimpleName();

    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The @link android.support.v4.view.ViewPager that will host the section contents.
     */
    ViewPager mViewPager;

    private CurrentReportString mCurrentReportString;
    @InjectView(R.id.jsonText) TextView mJsonText;
    @InjectView(R.id.imageReport) ImageView mImageReport;
    @InjectView(R.id.imageReport2) ImageView mImageReport2;
    @InjectView(R.id.imageReport3) ImageView mImageReport3;
    @InjectView(R.id.adminText) TextView mAdminText;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);


        mAdminText.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            
        );

        String urlWebSite = "http://www.bundoransurfco.com/surf-report/surf-report/?json=1";

        if (isNetworkAvailable()) 
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(urlWebSite)
                    .build();

            Call call = client.newCall(request);
            call.enqueue(new Callback() 
                @Override
                public void onFailure(Request request, IOException e) 

                

                @Override
                public void onResponse(Response response) throws IOException 
                    try 
                        String jsonData = response.body().string();
                        if (response.isSuccessful()) 
                            mCurrentReportString = getCurrentDetails(jsonData);
                            int numberOfPhotos = mCurrentReportString.getNumberOfPhotos();
                            if (numberOfPhotos == 1)
                            new DownloadImageTask(mImageReport)
                                    .execute(mCurrentReportString.getUrlImages());
                            
                            if (numberOfPhotos == 2)
                                new DownloadImageTask(mImageReport)
                                        .execute(mCurrentReportString.getUrlImages());
                                new DownloadImageTask(mImageReport2)
                                        .execute(mCurrentReportString.getUrlImages2());
                            
                            if (numberOfPhotos == 3)
                                new DownloadImageTask(mImageReport)
                                        .execute(mCurrentReportString.getUrlImages());
                                new DownloadImageTask(mImageReport2)
                                        .execute(mCurrentReportString.getUrlImages2());
                                new DownloadImageTask(mImageReport3)
                                        .execute(mCurrentReportString.getUrlImages3());
                            
                            if (numberOfPhotos > 3)
                                new DownloadImageTask(mImageReport)
                                        .execute(mCurrentReportString.getUrlImages());
                                new DownloadImageTask(mImageReport2)
                                        .execute(mCurrentReportString.getUrlImages2());
                                new DownloadImageTask(mImageReport3)
                                        .execute(mCurrentReportString.getUrlImages3());
                            

                            // Write in the main Thread.
                            runOnUiThread(new Runnable() 
                                @Override
                                public void run() 
                                    updateReport();
                                
                            );

                         else 
                            alertUserAboutError();
                        
                     catch (IOException e) 
                        Log.e(TAG, "Exception caught: ", e);
                    
                      catch (JSONException e)
                          Log.e(TAG, "Exception caught: ", e);
                      
                
            );
        
        else 
            Toast.makeText(this, getString(R.string.network_unavailable_message), Toast.LENGTH_LONG).show();
        

        Log.d(TAG, "Main UI code is running!");
    

    @Override
    protected void onResume() 
        super.onResume();
        initUI();
    

    private void updateReport() 
        mJsonText.setText(mCurrentReportString.getJsonContent());
        if (mCurrentReportString.imageBackground == Boolean.FALSE)
        
            Log.i(TAG, "No Background1");
            mImageReport.setVisibility(View.INVISIBLE);
        
        if (mCurrentReportString.imageBackground2 == Boolean.FALSE)
        
            Log.i(TAG, "No Background2");
            mImageReport2.setVisibility(View.INVISIBLE);
        
        if (mCurrentReportString.imageBackground3 == Boolean.FALSE)
        
            Log.i(TAG, "No Background3");
            mImageReport3.setVisibility(View.INVISIBLE);
        

    

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private CurrentReportString getCurrentDetails(String jsonData) throws JSONException 
        JSONObject report = new JSONObject(jsonData);

        JSONObject postJson = report.getJSONObject("post");
        CurrentReportString currentReportString = new CurrentReportString();
        String contentString = postJson.getString("content");


        //Report String
        String contentWithoutWebcam = contentString.replace("CLICK HERE FOR LIVE PEAK WEBCAM", "");
        int lastCharacter = contentWithoutWebcam.indexOf("#gallery-1");
        String contentFirstPart = contentWithoutWebcam.substring(0, lastCharacter);
        String jsonText = stripHtml(contentFirstPart);
        currentReportString.setJsonContent(jsonText);

        //Images
        Document doc = Jsoup.parse(contentString);
        Elements relLinks = doc.select("a[rel]");
//Elements relLinks = doc.select("a[rel=prettyPhoto[gallery-113]]");//
        System.out.println("number of `rel`: "+relLinks.size());
        currentReportString.setNumberOfPhotos(relLinks.size());
        ArrayList<String> urls=new ArrayList<String>();
        for (Element el : relLinks)
         //   Log.i(TAG, el.attr("href"));
            urls.add(el.attr("href"));
        
        if (relLinks.size() == 0)
            Log.i(TAG, "0 image");
            currentReportString.setImageBackground(Boolean.FALSE);
            currentReportString.setImageBackground2(Boolean.FALSE);
            currentReportString.setImageBackground3(Boolean.FALSE);
        
        else if (relLinks.size() == 1) 
            // Log.i(TAG, String.valueOf(urls));
            currentReportString.setUrlImages(urls.get(0));
            Log.i(TAG, "first URL : " + currentReportString.getUrlImages());
            currentReportString.setImageBackground2(Boolean.FALSE);
            currentReportString.setImageBackground3(Boolean.FALSE);
/*            new DownloadImageTask(mImageReport)
                    .execute(mCurrentReportString.getUrlImages());*/
        
        else if (relLinks.size() == 2)
            currentReportString.setUrlImages(urls.get(0));
            currentReportString.setUrlImages2(urls.get(1));
            currentReportString.setImageBackground3(Boolean.FALSE);
            Log.i(TAG, "first URL : "+currentReportString.getUrlImages());
            Log.i(TAG, "second URL : "+currentReportString.getUrlImages2());
/*            new DownloadImageTask(mImageReport)
                    .execute(mCurrentReportString.getUrlImages());
            new DownloadImageTask(mImageReport2)
                    .execute(mCurrentReportString.getUrlImages2());*/
        
        else if (relLinks.size() == 3)
            currentReportString.setUrlImages(urls.get(0));
            currentReportString.setUrlImages2(urls.get(1));
            currentReportString.setUrlImages3(urls.get(2));
            Log.i(TAG, "first URL : "+currentReportString.getUrlImages());
            Log.i(TAG, "second URL : "+currentReportString.getUrlImages2());
            Log.i(TAG, "third URL : "+currentReportString.getUrlImages3());

/*            new DownloadImageTask(mImageReport2)
                    .execute(mCurrentReportString.getUrlImages2());
            new DownloadImageTask(mImageReport3)
                    .execute(mCurrentReportString.getUrlImages3());*/
        
        else if (relLinks.size() > 3)
            currentReportString.setUrlImages(urls.get(0));
            currentReportString.setUrlImages2(urls.get(1));
            currentReportString.setUrlImages3(urls.get(2));
            Log.i(TAG, "first URL : "+currentReportString.getUrlImages());
            Log.i(TAG, "second URL : "+currentReportString.getUrlImages2());
            Log.i(TAG, "third URL : "+currentReportString.getUrlImages3());
/*            new DownloadImageTask(mImageReport)
                    .execute(mCurrentReportString.getUrlImages());
            new DownloadImageTask(mImageReport2)
                    .execute(mCurrentReportString.getUrlImages2());
            new DownloadImageTask(mImageReport3)
                    .execute(mCurrentReportString.getUrlImages3());*/
        


        return currentReportString;
    

    private boolean isNetworkAvailable() 
        ConnectivityManager manager = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()) 
            isAvailable = true;
        

        return isAvailable;
    
    //Delete HTML Markup
    public String stripHtml(String html) 
        return Html.fromHtml(html).toString();
    

    private void alertUserAboutError() 
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.show(getFragmentManager(), "error_dialog");
    

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) 
        mViewPager.setCurrentItem(tab.getPosition());
    

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) 

    

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) 

    

    //Change Image from ImageView in Asynch with URL.
    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> 
        ImageView bmImage;
        public DownloadImageTask(ImageView bmImage) 
            this.bmImage = bmImage;
        
        protected Bitmap doInBackground(String... urls) 
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try 
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
             catch (Exception e) 
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            
            return mIcon11;
        
        protected void onPostExecute(Bitmap result) 
            bmImage.setImageBitmap(result);
        
    


/*    @Override
    public void onTabSelected(android.support.v7.app.ActionBar.Tab tab,
                              FragmentTransaction fragmentTransaction) 
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    

    @Override
    public void onTabUnselected(android.support.v7.app.ActionBar.Tab tab,
                                FragmentTransaction fragmentTransaction) 
    

    @Override
    public void onTabReselected(android.support.v7.app.ActionBar.Tab tab,
                                FragmentTransaction fragmentTransaction) 
    */

    private void initUI()
        final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(android.support.v7.app.ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(this,
                getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() 
                    @Override
                    public void onPageSelected(int position) 
                        actionBar.setSelectedNavigationItem(position);
                    
                );

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) 
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener((android.support.v7.app.ActionBar.TabListener) this));
        
    

/*    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    */

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jardelcompany.bundoransurfco" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            android:parentActivityName=".MainActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize|stateVisible" >
        </activity>
    </application>

</manifest>

styles.xml:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>
</resources>

SectionsPagerAdapter.java:

   import java.util.Locale;
    import android.content.Context;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;

    public class SectionsPagerAdapter extends FragmentPagerAdapter 

        protected Context mContext;

        public SectionsPagerAdapter(Context context, FragmentManager fm) 
            super(fm);
            mContext = context;
        

        @Override
        public Fragment getItem(int position) 

            switch(position) 
                case 0:
                    return new FirstFragment();
                case 1:
                    return new LoginFragment();
            

            return null;
        

        @Override
        public int getCount() 
            return 2;
        

        @Override
        public CharSequence getPageTitle(int position) 
            Locale l = Locale.getDefault();
            switch (position) 
                case 0:
                    return mContext.getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return mContext.getString(R.string.title_section2).toUpperCase(l);
            
            return null;
        
    

LoginFragment.java:

    import android.os.Bundle;
    import android.support.v4.app.ListFragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class LoginFragment extends ListFragment 

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) 
            View rootView = inflater.inflate(R.layout.activity_login,
                    container, false);

            return rootView;
        

activity_login.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_
                android:layout_
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:background="#FFFFFF"
                tools:context="jardelcompany.bundoransurfco.LoginFragment">
</RelativeLayout>

FirstFragment.java:

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FirstFragment extends ListFragment 

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View rootView = inflater.inflate(R.layout.activity_main,
                container, false);

        return rootView;
    

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:context=".MainActivity"
    android:id="@+id/mainLayout"
    android:background="#FFFFFF"> />

    <view
        android:layout_
        android:layout_
        class="android.support.v4.view.ViewPager"
        android:id="@+id/pager"/>
</RelativeLayout>

Logcat 崩溃:

FATAL EXCEPTION: main
    Process: jardelcompany.bundoransurfco, PID: 2250
    java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

【问题讨论】:

请完整发布您的 MainActivity.java @HeshanSandeepa 已编辑 ;) 如果出现崩溃,请发布 logcat @HeshanSandeepa 谢谢你的帮助,用 logcat 编辑 【参考方案1】:

对于ActionBarActivity,您应该使用getSupportFragmentManager()getSupportActionBar()

【讨论】:

谢谢你的回复,我已经导入了android.support.v7.app.ActionBarActivity;并导入android.support.v7.app.ActionBar;并输入:final android.support.v7.app.ActionBar actionBar = getSupportActionBar(); ???但下一行 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);不起作用,并且 .setTabListener(this));也是红色的…… 请看看我上次的编辑,应用程序崩溃在新的一行:actionBar.addTab(actionBar.newTab() 如错误所示,您的 MainActivity 不是 implements ActionBar.TabListener 哦,没关系,看起来它是由您的错误导入引起的。您应该从支持库中实现 TabListener。 很好,没关系! :) 现在,最后一个错误:您的内容必须有一个 id 属性为“android.R.id.list”的 ListView,我要把 ListView 放在哪里? activity_main.xml ?【参考方案2】:

您的 MainActivity 中的 onResume(Bundle savedInstanceState) 方法永远不会因为错误的参数而被调用,而且您也不会调用 super.onResume()。正确的方法是:

@Override
protected void onResume() 

    super.onResume();
    initUI();

查看这里的一些文档:http://developer.android.com/training/basics/activity-lifecycle/pausing.html#Resume

【讨论】:

我用这段代码测试了我的应用程序崩溃:致命异常:主进程:jardelcompany.bundoransurfco,PID:1386 java.lang.RuntimeException:无法恢复活动jardelcompany.bundoransurfco/jardelcompany.bundoransurfco。 MainActivity:java.lang.NullPointerException 原因:jardelcompany.bundoransurfco.MainActivity.initUI(MainActivity.java:342) at jardelcompany.bundoransurfco.MainActivity.onResume(MainActivity.java:169) 处的 java.lang.NullPointerException 不调用超级方法就不会调用 现在您的 initUI 方法已执行,它在第 342 行崩溃 @udduk 是的,就是这一行:actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 所以 getActionBar 似乎返回 null。尝试调用 getSupportActionBar()【参考方案3】:

如下更新你的代码,

@Override    
protected void onResume() 
    super.onResume();
    initUI();

【讨论】:

请最后看一下我编辑的代码:我的应用在 actionBar.addTab(actionBar.newTab() 上崩溃 用import android.support.v7.app.ActionBar替换你的actionbar导入;而不是 import app.ActionBar; 你是对的!最后一个错误:您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView ...我需要在哪里放置 ListView ?在activity_main.xml 中? 你试图把列表视图放在哪里? 我不知道,我没有 ListView... 也许我需要在我的 XML 中创建它?因为我使用的是自定义视图: 所以也许我需要在 中实现 不?真的很抱歉,我是初学者……

以上是关于Fragment 和 FragmentList 不显示 Android Studio的主要内容,如果未能解决你的问题,请参考以下文章

java FragmentList_One.java

填充 ListView 很慢,Fragment 加载不顺畅

setUserVisibleHint的使用.执行顺序和viewPager.setOffscreenPageLimit不管用还是默认会加载第二个fragment

让ViewGroup中Fragment可见时才加载和不重复加载的方法

android实现tab个数少且固定,fragment不同时切换处理

创建Fragment和传递数值