选择选项卡时,Listfragment崩溃并出错

时间:2014-03-06 06:49:43

标签: android android-listfragment

当我运行我的应用程序时,一切都运行完美,直到我选择第三个选项卡组成我创建的listfragment,因此它显示一个包含数据和图像的列表视图,一旦选中该应用程序只是给出正常的消息,不幸的是应用程序停止工作和logcat发送了很多红色错误。

03-06 01:44:23.200: D/dalvikvm(879): GC_FOR_ALLOC freed 99K, 6% free 3113K/3280K, paused 57ms, total 59ms
03-06 01:44:23.410: D/dalvikvm(879): GC_FOR_ALLOC freed 20K, 5% free 3143K/3280K, paused 20ms, total 21ms
03-06 01:44:23.410: I/dalvikvm-heap(879): Grow heap (frag case) to 3.811MB for 709972-byte allocation
03-06 01:44:23.450: D/dalvikvm(879): GC_FOR_ALLOC freed <1K, 4% free 3836K/3976K, paused 33ms, total 33ms
03-06 01:44:23.530: D/(879): HostConnection::get() New Host Connection established 0xb7a51a38, tid 879
03-06 01:44:23.690: W/EGL_emulation(879): eglSurfaceAttrib not implemented
03-06 01:44:23.700: D/OpenGLRenderer(879): Enabling debug mode 0
03-06 01:44:35.760: D/AndroidRuntime(879): Shutting down VM
03-06 01:44:35.760: W/dalvikvm(879): threadid=1: thread exiting with uncaught exception (group=0xb4aa8ba8)
03-06 01:44:35.810: E/AndroidRuntime(879): FATAL EXCEPTION: main
03-06 01:44:35.810: E/AndroidRuntime(879): Process: com.TIPA.uips, PID: 879
03-06 01:44:35.810: E/AndroidRuntime(879): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:490)
03-06 01:44:35.810: E/AndroidRuntime(879):  at com.TIPA.uips.MainActivity.onTabSelected(MainActivity.java:71)
03-06 01:44:35.810: E/AndroidRuntime(879):  at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
03-06 01:44:35.810: E/AndroidRuntime(879):  at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1105)
03-06 01:44:35.810: E/AndroidRuntime(879):  at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.view.View.performClick(View.java:4438)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.view.View$PerformClick.run(View.java:18422)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.os.Handler.handleCallback(Handler.java:733)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.os.Handler.dispatchMessage(Handler.java:95)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.os.Looper.loop(Looper.java:136)
03-06 01:44:35.810: E/AndroidRuntime(879):  at android.app.ActivityThread.main(ActivityThread.java:5017)
03-06 01:44:35.810: E/AndroidRuntime(879):  at java.lang.reflect.Method.invokeNative(Native Method)
03-06 01:44:35.810: E/AndroidRuntime(879):  at java.lang.reflect.Method.invoke(Method.java:515)
03-06 01:44:35.810: E/AndroidRuntime(879):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-06 01:44:35.810: E/AndroidRuntime(879):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-06 01:44:35.810: E/AndroidRuntime(879):  at dalvik.system.NativeStart.main(Native Method)
03-06 01:44:39.230: I/Process(879): Sending signal. PID: 879 SIG: 9

我在本课程中更改的主要内容是使用extends Fragmentextends ListFragment我相信这会产生错误,但不确定如何?

package com.TIPA.uips;

import com.TIPA.uips.R;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FriendsList extends ListFragment {

     public class MyListAdapter extends ArrayAdapter<String> {

      Context myContext;

      public MyListAdapter(Context context, int textViewResourceId,
        String[] objects) {
       super(context, textViewResourceId, objects);
       myContext = context;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
       //return super.getView(position, convertView, parent);

       LayoutInflater inflater = 
         (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View row=inflater.inflate(R.layout.row, parent, false);
       TextView label=(TextView)row.findViewById(R.id.carrottop);
       label.setText(month[position]);
       ImageView icon=(ImageView)row.findViewById(R.id.dp);

       //Customize your icon here
       icon.setImageResource(R.drawable.dp);

       return row;
      }

     }

     String[] month ={
       "^Smithy", 
       "^Kreso", 
       "^Chen", 
       "^Dave"
     };

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      /*
      ListAdapter myListAdapter = new ArrayAdapter<String>(
        getActivity(),
        android.R.layout.simple_list_item_1,
        month);
      setListAdapter(myListAdapter);
      */
      MyListAdapter myListAdapter = 
        new MyListAdapter(getActivity(), R.layout.row, month);
      setListAdapter(myListAdapter);
     }

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState) {
      return inflater.inflate(R.layout.friends_list, container, false);
     }

     @Override
     public void onListItemClick(ListView l, View v, int position, long id) {
      Toast.makeText(
        getActivity(), 
        getListView().getItemAtPosition(position).toString(), 
        Toast.LENGTH_LONG).show();
     }

    }

这是适配器,允许片段返回每个类的值,认为它不允许它。

package com.TIPA.uips;

import com.TIPA.uips.NewsFeed;
import com.TIPA.uips.Chat;
import com.TIPA.uips.FriendsList;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new NewsFeed();
        case 1:
            // Games fragment activity
            return new Chat();
        case 2:
            // Movies fragment activity
            return new FriendsList();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }

}

干杯

更新:

在带有id / list的xml中实现listview时出现新错误。

03-06 02:47:59.780: D/dalvikvm(858): Not late-enabling CheckJNI (already on)
03-06 02:48:02.290: D/dalvikvm(858): GC_FOR_ALLOC freed 95K, 6% free 3117K/3284K, paused 52ms, total 54ms
03-06 02:48:02.510: D/dalvikvm(858): GC_FOR_ALLOC freed 19K, 5% free 3143K/3284K, paused 31ms, total 32ms
03-06 02:48:02.510: I/dalvikvm-heap(858): Grow heap (frag case) to 3.815MB for 709972-byte allocation
03-06 02:48:02.550: D/dalvikvm(858): GC_FOR_ALLOC freed <1K, 4% free 3836K/3980K, paused 29ms, total 29ms
03-06 02:48:02.640: D/(858): HostConnection::get() New Host Connection established 0xb74f2a38, tid 858
03-06 02:48:02.750: W/EGL_emulation(858): eglSurfaceAttrib not implemented
03-06 02:48:02.760: D/OpenGLRenderer(858): Enabling debug mode 0
03-06 02:48:07.920: D/AndroidRuntime(858): Shutting down VM
03-06 02:48:07.920: W/dalvikvm(858): threadid=1: thread exiting with uncaught exception (group=0xb4a74ba8)
03-06 02:48:07.940: E/AndroidRuntime(858): FATAL EXCEPTION: main
03-06 02:48:07.940: E/AndroidRuntime(858): Process: com.TIPA.uips, PID: 858
03-06 02:48:07.940: E/AndroidRuntime(858): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:490)
03-06 02:48:07.940: E/AndroidRuntime(858):  at com.TIPA.uips.MainActivity.onTabSelected(MainActivity.java:71)
03-06 02:48:07.940: E/AndroidRuntime(858):  at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
03-06 02:48:07.940: E/AndroidRuntime(858):  at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1105)
03-06 02:48:07.940: E/AndroidRuntime(858):  at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.view.View.performClick(View.java:4438)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.view.View$PerformClick.run(View.java:18422)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.os.Handler.handleCallback(Handler.java:733)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.os.Handler.dispatchMessage(Handler.java:95)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.os.Looper.loop(Looper.java:136)
03-06 02:48:07.940: E/AndroidRuntime(858):  at android.app.ActivityThread.main(ActivityThread.java:5017)
03-06 02:48:07.940: E/AndroidRuntime(858):  at java.lang.reflect.Method.invokeNative(Native Method)
03-06 02:48:07.940: E/AndroidRuntime(858):  at java.lang.reflect.Method.invoke(Method.java:515)
03-06 02:48:07.940: E/AndroidRuntime(858):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-06 02:48:07.940: E/AndroidRuntime(858):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-06 02:48:07.940: E/AndroidRuntime(858):  at dalvik.system.NativeStart.main(Native Method)
03-06 02:48:09.880: I/Process(858): Sending signal. PID: 858 SIG: 9

Friends_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

1 个答案:

答案 0 :(得分:3)

您需要在friends_list.xml

中添加ID为listview的列表视图
<ListView android:id="@android:id/list"

适用于ListActivity的任何内容适用于ListFragment。更多信息@

引用文档

  

ListActivity的默认布局由单个布局组成,   屏幕中央的全屏列表。但是,如果你愿意,   您可以通过设置自己的视图布局来自定义屏幕布局   在onCreate()中使用setContentView()。要做到这一点,你自己的观点必须   包含一个ID为“@android:id / list”的ListView对象(或列出if   它在代码中)

因此,如果您在ListFragment中填充布局,则需要在片段布局中使用ID为ListView的{​​{1}}。

编辑:

注意@android:id/list vs android:id="@+id/list"。我建议android:id="@android:id/list"