ListFragment android.view.InflateException

时间:2016-07-05 22:57:40

标签: android android-fragments

首先,我知道这是一个非常常见的例外,但在此问之前我尽力寻找答案。 这里是logcat(抱歉这个网站的新格式很糟糕):

  E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.rome.codzombiesinfo, PID: 6315
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rome.codzombiesinfo/com.example.rome.codzombiesinfo.MainActivity}: android.view.InflateException: Binary XML file line #44: Binary XML file line #44: Error inflating class fragment
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
     at android.app.ActivityThread.-wrap11(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5422)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: android.view.InflateException: Binary XML file line #44: Binary XML file line #44: Error inflating class fragment
     at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
     at com.example.rome.codzombiesinfo.MainActivity.onCreate(MainActivity.java:37)
     at android.app.Activity.performCreate(Activity.java:6251)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5422) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class fragment
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
     at com.example.rome.codzombiesinfo.MainActivity.onCreate(MainActivity.java:37) 
     at android.app.Activity.performCreate(Activity.java:6251) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5422) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
     at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
     at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1046)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
     at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
     at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
     at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
     at com.example.rome.codzombiesinfo.MainActivity.onCreate(MainActivity.java:37) 
     at android.app.Activity.performCreate(Activity.java:6251) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5422) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

这里是片段类:

public class ListviewFragment extends ListFragment {
    static ListView mainListview;
    public static Context context;
    static Article [] articlesToUse;
    static Bitmap [] thumbnailImages;
    static String [] titles;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //Inflate the fragment layout file
        View view= inflater.inflate(R.layout.listview_fragment, container, false);
        mainListview = (ListView) view.findViewById(R.id.listView);
        context = getActivity();
        mainListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent articleIntent= new Intent(ListviewFragment.this.getActivity(), MainArticle.class);
                final String bodyOfArticle = articlesToUse[position].getBody();
                final String titleOfArticle = articlesToUse[position].getTitle();
                articleIntent.putExtra("bodyOfArticle", bodyOfArticle);
                articleIntent.putExtra("titleOfArticle", titleOfArticle);
                startActivity(articleIntent);
            }
        });
        return view;
    }
    public static void setListView(Article [] articles){
        if(articles == null)
            Toast.makeText(context, R.string.error_connection, Toast.LENGTH_LONG).show();
        else {
            titles = new String[articles.length];
            thumbnailImages = new Bitmap[articles.length];
            articlesToUse = new Article[articles.length];
            for (int i = 0; i < articles.length; i++) {
                titles[i] = articles[i].getTitle();
                thumbnailImages[i] = articles[i].getThumbnailImage();
                articlesToUse[i] = new Article(articles[i]);//
            }
            CustomAdapter customAdapter = new CustomAdapter(context, titles, thumbnailImages);
            mainListview.setAdapter(customAdapter);
        }
    }

} 

我认为这就是问题所在,但是如果你想看到我的mainactivity.java或主要的xml文件告诉我,我明天醒来时就会把它添加好。谢谢你的帮忙! (或至少尝试)

1 个答案:

答案 0 :(得分:0)

Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

通常情况下,使用ListFragment时,您不会覆盖onCreateView(),因为ListFragment会为您处理。如果您执行覆盖onCreateView(),并且返回了一些您要膨胀的布局,那么 布局必须 ListView android:id="@android:id/list"。你的没有。