Java 创建片段视图时崩溃

Java 创建片段视图时崩溃,java,android,nullpointerexception,android-view,Java,Android,Nullpointerexception,Android View,我已经在一个片段中的ImageButton上放置了一个点击事件,用于对一个活动的意图。但应用程序崩溃,出现以下错误 尝试在空对象引用上调用虚拟方法“void android.widget.ImageButton.setOnClickListener(android.view.view$OnClickListener)” AccountFragment.java public class AccountInfoFragment extends Fragment { private

我已经在一个片段中的ImageButton上放置了一个点击事件,用于对一个活动的意图。但应用程序崩溃,出现以下错误

尝试在空对象引用上调用虚拟方法“void android.widget.ImageButton.setOnClickListener(android.view.view$OnClickListener)”

AccountFragment.java

    public class AccountInfoFragment extends Fragment {
    private ArrayList<MyAccountsCard> myAccountsCardData;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view =  inflater.inflate(R.layout.fragment_account_info, container, false);
        RecyclerView myAccountView=(RecyclerView) view.findViewById(R.id.my_accounts_view);


        myAccountView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL, false));

        initializeData();

        MyAccountsCardAdapter myAccountsCardAdapter= new MyAccountsCardAdapter(myAccountsCardData);
        myAccountView.setAdapter(myAccountsCardAdapter);
        final ImageButton iButtonShare = (ImageButton)view.findViewById(R.id.shareButton);
        iButtonShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext().getApplicationContext(),AccountShareActivity.class);
                startActivity(i);
            }
        });

        return view;
    }

    private void initializeData(){
        myAccountsCardData= new ArrayList<>();
        myAccountsCardData.add(new MyAccountsCard("12345678901234","scheme1","current","120000"));
        myAccountsCardData.add(new MyAccountsCard("12345678901234","scheme2","savings","5000"));
    }

}
公共类AccountInfoFragment扩展了片段{
私人ArrayList MyAccountScardata;
公共帐户信息片段(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u account\u info,container,false);
RecyclerView myAccountView=(RecyclerView)view.findViewById(R.id.my_accounts_视图);
myAccountView.setLayoutManager(新的LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
初始化数据();
MyAccountsCardAdapter MyAccountsCardAdapter=新的MyAccountsCardAdapter(MyAccountScardata);
setAdapter(myAccountsCardAdapter);
最终ImageButtonShare=(ImageButton)视图。findViewById(R.id.shareButton);
iButtonShare.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意图i=新意图(getContext().getApplicationContext(),AccountShareActivity.class);
星触觉(i);
}
});
返回视图;
}
private void initializeData(){
MyAccountScardata=新的ArrayList();
添加(新的MyAccountsCard(“1234567890134”,“scheme1”,“current”,“120000”));
添加(新的MyAccountsCard(“1234567890134”,“scheme2”,“savings”,“5000”);
}
}

iButtonShare对象为空。是否确实已将此按钮添加到xml布局
R.layout.fragment\u account\u info
?此布局中的按钮是否具有正确的id(
shareButton
)?

iButtonShare对象为空。是否确实已将此按钮添加到xml布局
R.layout.fragment\u account\u info
?此布局中的按钮是否具有正确的id(
shareButton

片段中的getActivity()返回该片段所在的活动 当前与关联

首先添加getActivity()而不是getApplicationContext()

当应用程序尝试使用 具有空值的对象引用

确保您的ImageButton具有正确的id(R.id.shareButton)

片段中的getActivity()返回该片段所在的活动 当前与关联

首先添加getActivity()而不是getApplicationContext()

当应用程序尝试使用 具有空值的对象引用


确保您的ImageButton具有正确的id(R.id.shareButton)

您的id不正确,这是错误

final ImageButton iButtonShare = (ImageButton)view.findViewById(R.id.shareButton);
我认为您使用了错误的id。请检查您是否使用了imageView或imageButton

现在,当您开始新活动时,请尝试以下操作

Intent i = new Intent(getActivity().getApplicationContext(),AccountShareActivity.class);
        startActivity(i);

您的id不正确,这是错误

final ImageButton iButtonShare = (ImageButton)view.findViewById(R.id.shareButton);
我认为您使用了错误的id。请检查您是否使用了imageView或imageButton

现在,当您开始新活动时,请尝试以下操作

Intent i = new Intent(getActivity().getApplicationContext(),AccountShareActivity.class);
        startActivity(i);

Can u post fragment_account_info.xml该按钮是否存在于您的xml中?Can u post fragment_account_info.xml该按钮是否存在于您的xml中?图像按钮是否存在于卡中?图像按钮是否存在于卡中,该卡是使用recyclerview放置的。实际上,图像按钮位于卡中,使用recyclerview放置卡的位置。