我目前正在开发一个具有一个活动和多个片段的应用程序。片段之间的导航应该使用工具栏按钮和后退按钮完成。
但是我遇到了一些错误,我不知道为什么。
这是MainActivity.java:
package com.geapps.nottaptests.testone;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.main_container, OneFragment.newInstance(0), "One").commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_add)
{
getSupportFragmentManager().beginTransaction().replace(R.id.main_container, TwoFragment.newInstance(0), "Two").addToBackStack(null).commit();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed()
{
if (getSupportFragmentManager().getBackStackEntryCount() > 0)
{
getSupportFragmentManager().popBackStack();
getSupportFragmentManager().beginTransaction().commit();
}
else
{
super.onBackPressed();
}
}
}
和OneFragment.java:
package com.geapps.nottaptests.testone;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OneFragment extends Fragment
{
public static OneFragment newInstance(int index)
{
OneFragment fragment = new OneFragment();
Bundle args = new Bundle();
args.putInt("int", index);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
}
TwoFragment.java与OneFragment.java完全相同。
当我按下TwoFragment.java中的后退按钮时,我收到以下错误:
Thread [<1> main] (Suspended (exception ClassNotFoundException))
<VM does not provide monitor information>
PathClassLoader(BaseDexClassLoader).findClass(String) line: 61
PathClassLoader(ClassLoader).loadClass(String, boolean) line: 501
PathClassLoader(ClassLoader).loadClass(String) line: 461
BackStackRecord.getEnterTransition(Fragment, boolean) line: 1056
BackStackRecord.configureTransitions(int, BackStackRecord$TransitionState, boolean, SparseArray, SparseArray) line: 1138
BackStackRecord.beginTransition(SparseArray, SparseArray, boolean) line: 1029
BackStackRecord.popFromBackStack(boolean, BackStackRecord$TransitionState, SparseArray, SparseArray) line: 883
FragmentManagerImpl.popBackStackState(Handler, String, int, int) line: 1541
FragmentManagerImpl$2.run() line: 493
FragmentManagerImpl.execPendingActions() line: 1489
FragmentManagerImpl$1.run() line: 454
Handler.handleCallback(Message) line: 615
FragmentActivity$1(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 137
ActivityThread.main(String[]) line: 4921
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 1038
ZygoteInit.main(String[]) line: 805
NativeStart.main(String[]) line: not available [native method]
答案 0 :(得分:0)
我通过制作自己的自定义后台堆栈解决了这个问题。唯一的问题是我必须小心不要多次添加相同的片段。
BackStack.java:
package com.geapps.nottaptests.testone;
import java.util.ArrayList;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
public class BackStack
{
private FragmentManager fragmentManager;
private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
public BackStack(FragmentManager fragmentManager)
{
this.fragmentManager = fragmentManager;
}
public void add(Fragment fragment)
{
fragments.add(fragment);
fragmentManager.beginTransaction().replace(R.id.main_container, this.get()).commit();
}
public void pop()
{
fragments.remove(fragments.size() - 1);
fragmentManager.beginTransaction().replace(R.id.main_container, this.get()).commit();
}
public Fragment get()
{
return fragments.get(fragments.size() - 1);
}
public int getNumber()
{
return fragments.size();
}
}
这里正在使用:
BackStack backStack = new BackStack(getSupportFragmentManager());
...
Fragment fragment = OneFragment.newInstance(0);
backStack.add(fragment);
...
Fragment fragment = TwoFragment.newInstance(0);
backStack.add(fragment);
...
backStack.pop()