仅当附加“setOnItemClickListener”时,应用程序才会崩溃

时间:2014-02-14 05:49:47

标签: android listview android-fragments onitemclicklistener

我在扩展“Fragment”的类中使用自定义列表视图。此自定义列表视图设置为在单击其中一个导航抽屉项目时打开。层次结构是这样的:

MainActivity.java(扩展活动)

  • MyList.java(extends Fragment)

问题是,当我不将项目点击监听器添加到onCreateView()方法时,程序工作正常。但是,当我这样做时,日志cat会抛出指向2个位置的错误 - 一个位于声明fragment标记的部分的XML下面,另一个位于已定义项目单击监听器的MyList.java中。

我已经在MyList.java和XML文件中评论了logcat指向错误的确切位置。

为什么会这样?我希望每个列表项都可以点击,并在点击时执行一些操作。

MainActivity的XML(使用抽屉布局) -

   <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Logcat points error on the beginning of the fragment tag -->

    <fragment 
          class="com.example.test.MyList" 
          android:id="@+id/fragment1"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

    <!-- Logcat points error here at the beginning of the fragment tag -->


    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"        
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>


</android.support.v4.widget.DrawerLayout> 

MyList.java:

import java.util.ArrayList;

import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MyList extends Fragment {

    ListView list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState)
    {

       /* Log cat points here too, at the beginning of setOnItemClickListener */

        list.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                System.out.println("Works!");
            }
            });

    return inflater.inflate(R.layout.listview, container, false);

    }

    @Override
    public void onStart() {

        super.onStart();
        list=(ListView)getView().findViewById(R.id.listView1);
        list.setAdapter(new List1Adapter(getActivity()));
    }

}

    class List1Adapter extends BaseAdapter
    {
        ArrayList<SingleRow> list;
        Context context;
        List1Adapter(Context c)
        {
            context=c;
            list=new ArrayList<SingleRow>();

            Resources res=c.getResources();
            String[] title=res.getStringArray(R.array.titles);
            String[] description=res.getStringArray(R.array.descriptions);

            int[] images={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
            for(int i=0;i<3;i++)
            {
                list.add(new SingleRow(title[i],description[i],images[i]));
            }
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row=inflater.inflate(R.layout.list_rows, parent, false);
            TextView title=(TextView)row.findViewById(R.id.textView1);
            TextView description=(TextView)row.findViewById(R.id.textView2);
            ImageView image=(ImageView)row.findViewById(R.id.imageView1);

            SingleRow temp=list.get(position);

            title.setText(temp.title);
            description.setText(temp.description);
            image.setImageResource(temp.image);

            return row;
        }


    class SingleRow
    {
        String title;
        String description;
        int image;
        SingleRow(String title, String description, int image)
        {
            this.title=title;
            this.description=description;
            this.image=image;
        }
    }

    }

logcat的:

   02-14 10:25:14.248: E/ActivityThread(22106): Failed to inflate
    02-14 10:25:14.248: E/ActivityThread(22106): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    02-14 10:25:14.248: E/ActivityThread(22106):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:320)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.Activity.setContentView(Activity.java:1895)
    02-14 10:25:14.248: E/ActivityThread(22106):    at com.example.test.MainActivity.onCreate(MainActivity.java:40)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.Activity.performCreate(Activity.java:5133)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.os.Handler.dispatchMessage(Handler.java:99)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.os.Looper.loop(Looper.java:137)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.ActivityThread.main(ActivityThread.java:5289)
    02-14 10:25:14.248: E/ActivityThread(22106):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-14 10:25:14.248: E/ActivityThread(22106):    at java.lang.reflect.Method.invoke(Method.java:525)
    02-14 10:25:14.248: E/ActivityThread(22106):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
    02-14 10:25:14.248: E/ActivityThread(22106):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
    02-14 10:25:14.248: E/ActivityThread(22106):    at dalvik.system.NativeStart.main(Native Method)
    02-14 10:25:14.248: E/ActivityThread(22106): Caused by: java.lang.NullPointerException
    02-14 10:25:14.248: E/ActivityThread(22106):    at com.example.test.List.onCreateView(MyList.java:27)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.Fragment.performCreateView(Fragment.java:1695)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.app.Activity.onCreateView(Activity.java:4746)
    02-14 10:25:14.248: E/ActivityThread(22106):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
    02-14 10:25:14.248: E/ActivityThread(22106):    ... 21 more
    02-14 10:25:14.258: D/AndroidRuntime(22106): Shutting down VM
    02-14 10:25:14.258: W/dalvikvm(22106): threadid=1: thread exiting with uncaught exception (group=0x415687c0)
    02-14 10:25:14.268: E/AndroidRuntime(22106): FATAL EXCEPTION: main
    02-14 10:25:14.268: E/AndroidRuntime(22106): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.os.Handler.dispatchMessage(Handler.java:99)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.os.Looper.loop(Looper.java:137)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.ActivityThread.main(ActivityThread.java:5289)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at java.lang.reflect.Method.invoke(Method.java:525)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at dalvik.system.NativeStart.main(Native Method)
    02-14 10:25:14.268: E/AndroidRuntime(22106): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:320)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.Activity.setContentView(Activity.java:1895)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at com.example.test.MainActivity.onCreate(MainActivity.java:40)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.Activity.performCreate(Activity.java:5133)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    ... 11 more
    02-14 10:25:14.268: E/AndroidRuntime(22106): Caused by: java.lang.NullPointerException
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at com.example.test.List.onCreateView(MyList.java:27)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.Fragment.performCreateView(Fragment.java:1695)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.app.Activity.onCreateView(Activity.java:4746)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
    02-14 10:25:14.268: E/AndroidRuntime(22106):    ... 21 more

2 个答案:

答案 0 :(得分:2)

删除此导入

import android.app.Fragment;

并从支持库导入Fragment ..

import android.support.v4.app.Fragment;

你的列表对象在onCreateView方法中也是空的..所以移动这个

list.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            System.out.println("Works!");
        }
        });

onStart方法..

答案 1 :(得分:1)

我认为它可以帮助您获得解决方案。 。

明确将OnItemClickListener添加到ListView

myList.setOnItemClickListener(this);

您还必须确保 Fragment实现OnItemClickListener 类型:

public class MainFiles extends Fragment implements OnItemClickListener

如果你有任何问题,请回复我。