Using Fragment in Class which is sub class of RoboActivity, Crashing application when Fragment Transaction begin;
I am also can't able to use Bottom navigation or Tablayout, maybe it's something relates roboguice.
This is Obd car Scan Application
Code As Below:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SpeedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment selectedFragment = null;
selectedFragment = new SpeedFragment();
AppCompatActivity activity=new AppCompatActivity();
if (!isFinishing() && !isDestroyed()) {
FragmentTransaction ft =activity.getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.fragment_container, selectedFragment);
ft.commit();
}
}
});
}
Log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.quad14.obdnewtry, PID: 7878
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2114)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:637)
at com.quad14.obdnewtry.activity.MainActivity$3.onClick(MainActivity.java:272)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Layout: only Fragment related component are added below
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="80dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonlinear"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speed"
android:id="@+id/speedbtid"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rpm"
android:id="@+id/rpmbtid"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Km"
android:id="@+id/distanbtid"/>
</LinearLayout>
</LinearLayout>
If your activity extends Activity your fragment should be android.app.Fragment. Then use code below
android.app.FragmentTransaction ft =getFragmentManager()
.beginTransaction();
ft.replace(R.id.fragment_container, selectedFragment);
ft.commit();
else if your activity extends AppCompactActivity your fragment should be android.support.v4.app.Fragment.Then this code will work
FragmentTransaction ft =getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.fragment_container, selectedFragment);
ft.commit();
Instead of creating object for activity like AppCompatActivity activity=new AppCompatActivity();
and using it to get fragment manager.
You can get fragment manager from activity context something like this YourActivityname.this.getFragmentManager();
尝试使用
getContext().getSupportFragmentManager().beginTransaction();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.