[英]How do I not get nullpointer exception on my fragment?
因此,每當我打開此片段時,我都會得到一個空指針異常。 能否請你幫忙?
public class ProfileFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
Bundle bundle = getArguments();
String eka;
if(bundle.containsKey("Name:")) {
eka = bundle.getString("Name:");
String toka;
toka = bundle.getString("Country");
TextView ekat = (TextView) getView().findViewById(R.id.nimit);
TextView tokat = (TextView) getView().findViewById(R.id.maat);
ekat.setText(eka);
tokat.setText(toka);
}
return inflater.inflate(R.layout.fragment_profile, container, false);
}
}
這是我從中發送信息的活動
public class MainActivity extends AppCompatActivity {
EditText edit, editt, edittt, editttt;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.nimi);
editt = (EditText) findViewById(R.id.maa);
edittt = (EditText) findViewById(R.id.salasana);
editttt = (EditText) findViewById(R.id.syntymäaika);
btn = (Button) findViewById(R.id.nappi);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String eka = edit.getText().toString();
String toka = editt.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("Name:", eka);
bundle.putString("Country", toka);
ProfileFragment profileFragment = new ProfileFragment();
profileFragment.setArguments(bundle);
edit.setVisibility(View.GONE);
editt.setVisibility(View.GONE);
edittt.setVisibility(View.GONE);
editttt.setVisibility(View.GONE);
btn.setVisibility(View.GONE);
}
});
我得到這個錯誤:
java.lang.NullPointerException:嘗試在空對象引用上調用虛擬方法'boolean android.os.Bundle.containsKey(java.lang.String)'
因此,只要我打開片段,應用程序就會崩潰。 由於某種原因,它不會將信息從活動發送到片段。 謝謝您的幫助。
根據我所看到的,您缺少片段事務,因此我猜您正在靜態顯示片段,並在xml文件中對其進行定義(以確保需要發布activity_layout.xml文件)。在activity_main.xml
,需要有一個FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".login.AuthenticationActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
然后在View.OnClickListener
您可以執行
Bundle bundle = new Bundle();
bundle.putString("Name:", eka);
bundle.putString("Country", toka);
ProfileFragment profileFragment = new ProfileFragment();
profileFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
edit.setVisibility(View.GONE);
//Continue with your code
同樣好的做法是在onCreate()
獲取參數
覆蓋onCreate()
並執行以下操作
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if(bundle.containsKey("Name:")) {
eka = bundle.getString("Name:"); // make this field to use in onCreateView()
toka = bundle.getString("Country"); // make this field to use in onCreateView()
然后在onCreateView()
您將需要以下內容
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
TextView ekat = (TextView) view.findViewById(R.id.nimit);
TextView tokat = (TextView) view.findViewById(R.id.maat);
ekat.setText(eka);
tokat.setText(toka);
}
在onCreateView()
返回之前,您不能使用getView()
。
將return
語句上方的所有內容移到onViewCreated()
。
通過使用:public static String eka =“”; 公共靜態字符串toka =“”;
您的活動並通過以下方式訪問它:MainActivity.eka MainActivity.toka
它將停止崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.