-1

我有可扩展列表视图,每个父级都有 1 个子布局,当我将 onClickListener 分配给 childView 内的 ImageView 时,应用程序运行但在 imageView 所在的片段打开时崩溃

代码:

public class FragmentA extends Fragment{

    private List<Category> catList;
    ExpandableListView exList;
    ImageView del,info,collapse;
    ExpandableAdapter exAdpt;

    public FragmentA() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_a, null);
        exList = (ExpandableListView) v.findViewById(R.id.expandablelist);
        //exList.setIndicatorBounds(5, 5);
        //exList.setIndicatorBounds(0, 20);
        //exList.setDivider(null);
        exList.setDividerHeight(3);
        exList.setGroupIndicator(null);

        del = (ImageView) v.findViewById(R.id.del_icon);
        info = (ImageView) v.findViewById(R.id.inf_icon);
        collapse = (ImageView) v.findViewById(R.id.collapse_icon);

        collapse.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
            }
        });

        return v;
    }

错误日志:

07-28 12:15:13.937: E/art(23655): Unrecognized option -XX:mainThreadStackSize=24K
07-28 12:15:14.007: E/cutils-trace(23655): Error opening trace file: No such file or directory (2)
07-28 12:15:14.728: E/art(23669): Unrecognized option -XX:mainThreadStackSize=24K
07-28 12:15:14.798: E/cutils-trace(23669): Error opening trace file: No such file or directory (2)
07-28 12:15:16.690: E/AndroidRuntime(23685): FATAL EXCEPTION: main
07-28 12:15:16.690: E/AndroidRuntime(23685): Process: com.example.androidhive, PID: 23685
07-28 12:15:16.690: E/AndroidRuntime(23685): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
07-28 12:15:16.690: E/AndroidRuntime(23685):    at com.example.androidhive.FragmentA.onCreateView(FragmentA.java:48)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.View.measure(View.java:16497)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.View.measure(View.java:16497)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.View.measure(View.java:16497)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.View.measure(View.java:16497)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.Choreographer.doFrame(Choreographer.java:544)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.os.Handler.handleCallback(Handler.java:733)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.os.Looper.loop(Looper.java:136)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at android.app.ActivityThread.main(ActivityThread.java:5017)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at java.lang.reflect.Method.invoke(Native Method)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-28 12:15:16.690: E/AndroidRuntime(23685):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

适配器 :

public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
                  (Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.item_layout, parent, false);
    }

    TextView itemName = (TextView) v.findViewById(R.id.itemName);
    TextView itemDescr = (TextView) v.findViewById(R.id.itemDescr);
    ImageView itemImg = (ImageView) v.findViewById(R.id.itemImg);
    ImageView itemBarcode = (ImageView) v.findViewById(R.id.itemBarcode);
    ImageView collapse = (ImageView) v.findViewById(R.id.collapse_icon);        


    ItemDetail det = catList.get(groupPosition).getItemList().get(childPosition);

    itemName.setText(det.getName());
    itemDescr.setText(det.getDescr());
    itemImg.setImageResource(det.getImgId());
    itemBarcode.setImageResource(det.getBarImg());

    return v;
}
4

2 回答 2

0

我找到了解决方案,将 onClickListener 分配给 ExpandableListView 适配器类中的 imageView,在 getChildView 中,它工作正常。

于 2014-07-30T12:31:29.270 回答
0

您必须检查此视图是否必须存在于您正在膨胀的主视图中:

collapse = (ImageView) v.findViewById(R.id.collapse_icon);

您的 logcat 错误表明存在空指针异常:

07-28 12:15:16.690: E/AndroidRuntime(23685): java.lang.NullPointerException: 尝试在空对象上调用虚拟方法 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)'参考

于 2015-06-05T11:20:46.710 回答