我试图从fragment中的另一个布局获取按钮。此fragment具有另一个膨胀的另一个layout.i获取java.lang.NullPointerException。如何解决这个问题呢?

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.add_to_cart_display, container, false);


    myAdapter=new MyAdapter(getActivity());

    Button remove=(Button) view.findViewById(R.id.remove_cart_items);
    RecyclerView recyclerView=(RecyclerView)view.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    ch=new CartHelper(getActivity());
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    rc=new RCadapter(getDataset());
    recyclerView.setAdapter(rc);



    return view;


}


从pgm上方开始,删除按钮不在add_cart_display xml文件中。它位于另一个布局文件中。在这里,我正在访问此button.it给出以下错误。


  10-31 12:38:20.484 13886-13886 / com.infizoom.infishopping
  E / Android运行时:致命异常:主要10-31 12:38:20.484
  13886-13886 / com.infizoom.infishopping E / AndroidRuntime:进程:
  com.infizoom.infishopping,PID:13886 10-31 12:38:20.484
  13886-13886 / com.infizoom.infishopping E / AndroidRuntime:
  java.lang.NullPointerException 10-31 12:38:20.484
  13886-13886 / com.infizoom.infishopping E / AndroidRuntime:at
  com.infizoom.infishopping.add_cart_class.onStart(add_cart_class.java:75)
  10-31 12:38:20.484 13886-13886 / com.infizoom.infishopping
  E / AndroidRuntime:位于
  android.app.Fragment.performStart(Fragment.java:1724)10-31
  12:38:20.484 13886-13886 / com.infizoom.infishopping E / Android运行时:
  在
  android.app.FragmentManagerImpl.moveToState(FragmentManager.java:918)
  10-31 12:38:20.484 13886-13886 / com.infizoom.infishopping
  E / AndroidRuntime:位于
  android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
  10-31 12:38:20.484 13886-13886 / com.infizoom.infishopping
  E / AndroidRuntime:位于
  android.app.BackStackRecord.run(BackStackRecord.java:698)10-31
  12:38:20.484 13886-13886 / com.infizoom.infishopping E / Android运行时:
  在
  android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
  10-31 12:38:20.484 13886-13886 / com.infizoom.infishopping
  E / AndroidRuntime:位于
  android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:443)10-31
  12:38:20.484 13886-13886 / com.infizoom.infishopping E / Android运行时:
  在android.os.Handler.handleCallback(Handler.java:808)10-31
  12:38:20.484 13886-13886 / com.infizoom.infishopping E / Android运行时:
  在android.os.Handler.dispatchMessage(Handler.java:103)10-31
  12:38:20.484 13886-13886 / com.infizoom.infishopping E / Android运行时:
  在android.os.Looper.loop(Looper.java:193)10-31 12:38:20.484
  13886-13886 / com.infizoom.infishopping E / AndroidRuntime:at
  android.app.ActivityThread.main(ActivityThread.java:5341)10-31
  12:38:20.484 13886-13886 / com.infizoom.infishopping E / Android运行时:
  在java.lang.reflect.Method.invokeNative(本地方法)10-31
  12:38:20.484 13886-13886 / com.infizoom.infishopping E / Android运行时:
  在java.lang.reflect.Method.invoke(Method.java:515)10-31 12:38:20.484
  13886-13886 / com.infizoom.infishopping E / AndroidRuntime:at
  com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:825)
  10-31 12:38:20.484 13886-13886 / com.infizoom.infishopping
  E / AndroidRuntime:位于
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)10-31
  12:38:20.484 13886-13886 / com.infizoom.infishopping E / Android运行时:
  在dalvik.system.NativeStart.main(本机方法)


add_to_cart_display.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recyclerView"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    />




移除CardView内的按钮

最佳答案

   final View view = inflater.inflate(R.layout.add_to_cart_display, container, false);
   Button remove=(Button) view.findViewById(R.id.remove_cart_items);


您正在add_to_cart_display中寻找ID为remove_cart_items的Button。但是在布局中可能没有ID为remove_cart_items的Button。那就是你得到空的方式

10-04 20:04