【问题标题】:App crashes after button click implementation in fragments片段中的按钮单击实现后应用程序崩溃
【发布时间】:2017-01-18 13:18:43
【问题描述】:

我的应用包含导航抽屉。其中有一个项目片段。在那个片段中,我尝试实现三个按钮单击事件来打开 3 个活动。但是,每当我单击导航抽屉中的片段时,应用程序就会因 nullPointerAcception 而崩溃。调试后,它显示以下包含 onClickListener 的代码存在问题:

itemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
                startActivity(itemIntent);
            }
        });

这是我的片段代码。

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


/**
 * A simple {@link Fragment} subclass.
 */
public class ItemsFragment extends Fragment {

    Button itemButton;
    Button categoryButton;
    Button discountButton;

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


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


        itemButton=(Button) container.findViewById(R.id.items_button);
        categoryButton=(Button) container.findViewById(R.id.category_button);
        discountButton=(Button) container.findViewById(R.id.discount_button);

        itemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
                startActivity(itemIntent);
            }
        });

        setHasOptionsMenu(false);
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_items, container, false);
    }

}

// Logcat 错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  java.lang.NullPointerException
                      at com.soumya.possystem.ItemsFragment.onCreateView(ItemsFragment.java:36)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
                      at android.os.Handler.handleCallback(Handler.java:725)
                      at android.os.Handler.dispatchMessage(Handler.java:92)
                      at android.os.Looper.loop(Looper.java:137)
                      at android.app.ActivityThread.main(ActivityThread.java:5041)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:511)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                      at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

标签: android android-fragments nullpointerexception android-button


【解决方案1】:

在膨胀视图和创建视图之前,您试图找到控件的实例。并在 onCreateView 中返回膨胀视图

另一种方法是在onViewCreatedonActivityCreated中获取实例(findviewById)

第二个问题是您试图从 container 获取控件对象,而不是您需要从膨胀视图中获取。

View v=inflater.inflate(R.layout.fragment_items, container, false);

下面我保留了更改的代码。

import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;


    /**
     * A simple {@link Fragment} subclass.
     */
    public class ItemsFragment extends Fragment {

        Button itemButton;
        Button categoryButton;
        Button discountButton;

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


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    View v=inflater.inflate(R.layout.fragment_items, container, false);
    //check in place of container here you need to use inflated view (`v`) instance
            itemButton=(Button) v.findViewById(R.id.items_button);
            categoryButton=(Button) v.findViewById(R.id.category_button);
            discountButton=(Button) v.findViewById(R.id.discount_button);

            itemButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
                    startActivity(itemIntent);
                }
            });

            setHasOptionsMenu(false);
            // Inflate the layout for this fragment
            return v;
        }

    }

【讨论】:

  • 你能指定具体代码在哪里不工作
  • @jiteshmohite 请检查我保存的代码和注释,因为 NullPointerException 导致崩溃
【解决方案2】:

您必须根据要求初始化视图:
Create a Fragment Class

所以你必须这样做
View view = inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
然后
itemButton=(Button) view.findViewById(R.id.items_button);

【讨论】:

    【解决方案3】:
        public class ItemsFragment extends Fragment {
    
        Button itemButton;
        Button categoryButton;
        Button discountButton;
    
        public ItemsFragment() {
            // Required empty public constructor
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
        /** Inflate rootView of fragment **/
        View rootView = inflater.inflate(R.layout.fragment_items, container, false);
    
          //replace "container" with "rootView" **/
            itemButton=(Button) rootView.findViewById(R.id.items_button);
            categoryButton=(Button) rootView.findViewById(R.id.category_button);
            discountButton=(Button) rootView.findViewById(R.id.discount_button);
    
            itemButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
                    startActivity(itemIntent);
                }
            });
    
            setHasOptionsMenu(false);
            // Inflate the layout for this fragment
            return rootView; // Return the view object you inflated
        }
    
    }
    

    【讨论】:

    • 我建议你继续解释这个问题。以便用户了解问题
    【解决方案4】:

    试试这个

        View rootView = inflater.inflate(R.layout.fragment_id, container, false);
        itemButton=(Button) container.findViewById(R.id.items_button);
        categoryButton=(Button) container.findViewById(R.id.category_button);
        discountButton=(Button) container.findViewById(R.id.discount_button);
    .
    .
    .
    
        return rootView;
    

    【讨论】:

      【解决方案5】:

      试试这个:

      public class ItemsFragment extends Fragment {
      
      Button itemButton;
      Button categoryButton;
      Button discountButton;
      
      public ItemsFragment() {
          // Required empty public constructor
      }
      
      
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View v=inflater.inflate(R.layout.fragment_items, container, false);
          setHasOptionsMenu(false);
          return v;
      }
      
       @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
         itemButton=(Button) view.findViewById(R.id.items_button);
          categoryButton=(Button) view.findViewById(R.id.category_button);
          discountButton=(Button) view.findViewById(R.id.discount_button);
      
          itemButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
                  startActivity(itemIntent);
              }
          });
      
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        相关资源
        最近更新 更多