Ich habe ein Layout FrameLayout und ein LinearLayout in MainActivity. Das Linearlayout besteht aus 7 Bildern. Ich ändere Fragmente beim Klicken auf diese Bilder. In einem der Fragmente erhalte ich Werte von json und füge sie in 2 Spinner ein. Das erste Mal, wenn ich das Fragment von diesem Fragment zu irgendeinem anderen Fragment ändere, funktioniert es gut. Aber wenn ich zum zweiten Mal zu diesem Fragment zurückkehre und dann das Fragment ändere, stürzt die App mit einer NPE ab. Was mache ich in meiner MainActivity falsch?NullPointerException in FragmentTransaction
Logcat:
FATAL EXCEPTION: main
Process: com.rishta.rishtabliss, PID: 26506
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:228)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:178)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:163)
at com.rishta.rishtabliss.fragment.FragmentTwo$1.onResponse(FragmentTwo.java:81)
at com.rishta.rishtabliss.fragment.FragmentTwo$1.onResponse(FragmentTwo.java:64)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activity.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/llfooter"
android:background="#ffffff"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="45dp">
<ImageView
android:id="@+id/img1"
android:layout_weight="1"
android:src="@drawable/number_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"/>
<ImageView
android:id="@+id/img2"
android:layout_weight="1"
android:src="@drawable/number_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"/>
<ImageView
android:id="@+id/img3"
android:layout_weight="1"
android:src="@drawable/number_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"/>
<ImageView
android:id="@+id/img4"
android:layout_weight="1"
android:src="@drawable/number_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"/>
<ImageView
android:id="@+id/img5"
android:layout_weight="1"
android:src="@drawable/number_five"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"/>
<ImageView
android:id="@+id/img6"
android:layout_weight="1"
android:src="@drawable/number_six"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"/>
<ImageView
android:id="@+id/img7"
android:layout_weight="1"
android:src="@drawable/number_seven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"/>
</LinearLayout>
<FrameLayout
android:id="@+id/frame_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="@+id/llfooter" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Hauptaktivität:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.frame_main) != null) {
if (savedInstanceState != null) {
return;
}
FragmentOne firstFragment = new FragmentOne();
getSupportFragmentManager().beginTransaction().add(R.id.frame_main, firstFragment).commit();
}
} //End OnCreate Method
public void selectFrag(View view) {
FragmentManager fm = getSupportFragmentManager();
Fragment currentFragment = fm.findFragmentById(R.id.frame_main);
Fragment newFragment = null;
switch (view.getId()) {
case R.id.img1:
if (currentFragment instanceof FragmentOne) {
return;
}
newFragment = new FragmentOne();
break;
case R.id.img2:
if (currentFragment instanceof FragmentTwo) {
return;
}
newFragment = new FragmentTwo();
break;
case R.id.img3:
if (currentFragment instanceof FragmentThree) {
return;
}
newFragment = new FragmentThree();
break;
case R.id.img4:
if (currentFragment instanceof FragmentFour) {
return;
}
newFragment = new FragmentFour();
break;
case R.id.img5:
if (currentFragment instanceof FragmentFive) {
return;
}
newFragment = new FragmentFive();
break;
case R.id.img6:
if (currentFragment instanceof FragmentSix) {
return;
}
newFragment = new FragmentSix();
break;
case R.id.img7:
if (currentFragment instanceof FragmentSeven) {
return;
}
newFragment = new FragmentSeven();
break;
}
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frame_main, newFragment);
fragmentTransaction.commit();
}
}
Fragment:
public class FragmentTwo extends Fragment {
private Spinner religion, subreligion, mtoungue;
private List<String> religionlist;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
religion = (Spinner)view.findViewById(R.id.spinner_religion);
fetchlist();
}
private void fetchlist() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, URLUtils.SIGNUP_COTAIN_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("ResponceFragmentTwo","***********************"+response);
try {
religionlist = new ArrayList<String>();
JSONObject jo = new JSONObject(response);
JSONArray arr = jo.getJSONArray("all_religion");
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
religionlist.add(obj.getString("name"));
}
religion.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, religionlist));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
}
Nein, ist firstFragment nicht null, wie ich bin immer die erstes Fragment beim Öffnen der App – Monojit
können Sie bitte 'R.layout.activity_main' hinzufügen. – apelsoczi
Welche Zeilen sind Zeilennummer 81 und 64 in 'FragmentTwo'? – PPartisan