我是android和java的初学者,目前我正在研究一个在我的主要活动中使用两个片段和两个按钮的项目,并且我已经实现了OnClickListener来调用这些按钮。单击按钮它将显示相应的片段,我也使用addtobackstack();
方法阻止它们退出应用程序,问题是当我单击后退按钮,片段被销毁并移动到主要活动,当我点击按钮再次启动相同的片段应用程序崩溃..
主要活动中的代码,
public class Login_Register extends Activity implements OnClickListener {
Login login = new Login();
Register register = new Register();
FragmentManager fmanager = getFragmentManager();
ImageButton LoginB, RegisterB;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setWindowAnimations(0);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_register);
LoginB = (ImageButton) findViewById(R.id.login_button);
RegisterB = (ImageButton) findViewById(R.id.Reg_button);
LoginB.setOnClickListener(this);
RegisterB.setOnClickListener(this);
}
@Override
public void onClick(View v) {
FragmentTransaction transaction = fmanager.beginTransaction();
switch (v.getId()) {
case R.id.login_button:
transaction.add(R.id.Parent, login, "Loginf");
transaction.addToBackStack("Login");
transaction.commit();
break;
case R.id.Reg_button:
transaction.add(R.id.Parent, register, "registerf");
transaction.addToBackStack("registerf");
transaction.commit();
break;
default:
break;
}
// TODO Auto-generated method stub
}
}
片段名称是Login and Register,其代码(仅限登录片段):
public class Login extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View login=inflater.inflate(R.layout.login, container, false);
// TODO Auto-generated method stub
return login;
}
}
Logcat o / p:
08-21 12:14:27.517: D/OpenGLRenderer(10662): Enabling debug mode 0
08-21 12:14:31.860: D/AndroidRuntime(10662): Shutting down VM
08-21 12:14:31.860: W/dalvikvm(10662): threadid=1: thread exiting with uncaught exception (group=0x41674d40)
08-21 12:14:31.868: E/AndroidRuntime(10662): FATAL EXCEPTION: main
08-21 12:14:31.868: E/AndroidRuntime(10662): Process: com.example.apeonomy, PID: 10662
08-21 12:14:31.868: E/AndroidRuntime(10662): android.view.InflateException: Binary XML file line #74: Error inflating class fragment
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-21 12:14:31.868: E/AndroidRuntime(10662): at com.digiapes.apeonomy.Login.onCreateView(Login.java:17)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.app.Fragment.performCreateView(Fragment.java:1700)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.app.BackStackRecord.run(BackStackRecord.java:684)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.os.Handler.handleCallback(Handler.java:733)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.os.Handler.dispatchMessage(Handler.java:95)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.os.Looper.loop(Looper.java:136)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.app.ActivityThread.main(ActivityThread.java:5086)
08-21 12:14:31.868: E/AndroidRuntime(10662): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 12:14:31.868: E/AndroidRuntime(10662): at java.lang.reflect.Method.invoke(Method.java:515)
08-21 12:14:31.868: E/AndroidRuntime(10662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-21 12:14:31.868: E/AndroidRuntime(10662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-21 12:14:31.868: E/AndroidRuntime(10662): at dalvik.system.NativeStart.main(Native Method)
08-21 12:14:31.868: E/AndroidRuntime(10662): Caused by: java.lang.IllegalArgumentException: Binary XML file line #74: Duplicate id 0x7f040026, tag null, or parent id 0x7f040020 with another fragment for com.digiapes.apeonomy.Face_Goog
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.app.Activity.onCreateView(Activity.java:4808)
08-21 12:14:31.868: E/AndroidRuntime(10662): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
08-21 12:14:31.868: E/AndroidRuntime(10662): ... 20 more
08-21 12:14:33.963: I/Process(10662): Sending signal. PID: 10662 SIG: 9
答案 0 :(得分:0)
transaction.replace(R.id.Parent, new Login());
transaction.addToBackStack(null);
transaction.commit();
和
transaction.add(R.id.Parent, new Register());
transaction.addToBackStack(null);
transaction.commit();
答案 1 :(得分:0)
由于您的堆栈跟踪清楚地表明您的问题在于布局的膨胀:
08-21 12:14:31.868: E/AndroidRuntime(10662): android.view.InflateException: Binary XML file line #74: Error inflating class fragment
所以我猜这是你问题的根源:
View login=inflater.inflate(R.layout.login, container, false);
首先尝试:
View login=inflater.inflate(R.layout.login, null, false);
有时错误的来源可能是inflater(我记得这是一个常见的错误),所以尽量让自己有一个新的inflater:
:
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.login, null, false);
或return View.inflate(getActivity(), R.layout.login)
;