我已經將一個單擊事件放置在片段內的ImageButton上進行一項活動。但該應用程序崩潰,出現以下錯誤。創建片段視圖時發生崩潰
嘗試上的空對象引用
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("123456789","scheme1","current","120000"));
myAccountsCardData.add(new MyAccountsCard("123456789","scheme2","savings","5000"));
}
}
u能張貼fragment_account_info.xml – Raghavendra
存在於你的XML按鈕? –
[什麼是NullPointerException,以及如何解決它?]可能重複(http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – Raghavendra