我有一个导航抽屉,滑动菜单中的每个声音都对应一个片段类,这是我的 LoginFragment 菜单声音:
public class LoginFragment extends Fragment {
Button btnLogin;
Button btnLinkToRegister;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
//@Override
//public void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
//setContentView(R.layout.fragment_login);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_login, container, false);
}
@Override
public void onStart(){
super.onStart();
initControls();
}
// Importing all assets like buttons, text fields
private void initControls(){
inputEmail = (EditText) getView().findViewById(R.id.loginEmail);
inputPassword = (EditText) getView().findViewById(R.id.loginPassword);
btnLogin = (Button) getView().findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) getView().findViewById(R.id.btnLinkToRegisterScreen);
loginErrorMsg = (TextView) getView().findViewById(R.id.login_error);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getActivity());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getActivity());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getActivity(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
//finish();
}else{
// Error in login
loginErrorMsg.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
// Link to Register Screen
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getActivity(),
RegistrazioneActivity.class);
startActivity(i);
//finish();
}
});
}
}
该片段有一个指向注册活动的链接,如上所述,注册活动也有一个返回登录片段的链接。问题是登录页面不是我可以从意图开始的活动,而是一个片段。这是注册活动的代码:
public class RegistrazioneActivity extends FragmentActivity {
Button btnRegister;
Button btnLinkToLogin;
EditText inputFullName;
EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registrazione);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
inputEmail = (EditText) findViewById(R.id.registerEmail);
inputPassword = (EditText) findViewById(R.id.registerPassword);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
registerErrorMsg = (TextView) findViewById(R.id.register_error);
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(name, email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully registred
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Registration Screen
finish();
}else{
// Error in registration
registerErrorMsg.setText("Error occured in registration");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
// Link to Login Screen
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
LoginFragment fragment = new LoginFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// This method unfortunately don't work
transaction.replace(view.getId(), fragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
}
这里有 XML:fragment_login.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:id="@+id/fragment_login">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dip" >
<!-- View Title Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="LOGIN"
android:textSize="25dip"
android:textStyle="bold"
android:gravity="center" />
<!-- Email Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email" />
<!-- Email TextField -->
<EditText
android:id="@+id/loginEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- Password Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="Password" />
<!-- Password TextField -->
<EditText
android:id="@+id/loginPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true" />
<!-- Error message -->
<TextView android:id="@+id/login_error"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#e30000"
android:padding="10dip"
android:textStyle="bold"/>
<!-- Login Button -->
<Button
android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:text="Login" />
<!-- Link to Registration Screen -->
<Button
android:id="@+id/btnLinkToRegisterScreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="@null"
android:text="Non ho un account. Registrami!"
android:textColor="#21dbd4"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
registrazione.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:id="@+id/registrazione">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dip" >
<!-- View Title Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="REGISTRAZIONE"
android:textSize="25dip"
android:textStyle="bold"
android:gravity="center" />
<!-- Name Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Nome Completo" />
<!-- Name TextField -->
<EditText
android:id="@+id/registerName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- Email Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email" />
<!-- Email TextField -->
<EditText
android:id="@+id/registerEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- Password Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="Password" />
<!-- Password TextField -->
<EditText
android:id="@+id/registerPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true" />
<!-- Error message -->
<TextView android:id="@+id/register_error"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#e30000"
android:padding="10dip"
android:textStyle="bold"/>
<!-- Login Button -->
<Button
android:id="@+id/btnRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:text="Registrami" />
<!-- Link to Login Screen -->
<Button
android:id="@+id/btnLinkToLoginScreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="@null"
android:text="Già registrato. Fai il login!"
android:textColor="#21dbd4"
android:textStyle="bold" />
</LinearLayout>
日志猫:
06-02 17:35:49.403: E/FragmentManager(2634): No view found for id 0x7f090009 (it.sii.android.sporting:id/fragment_login) for fragment LoginFragment{42a8ece8 #0 id=0x7f090009 fragment_screen}
06-02 17:35:49.403: E/FragmentManager(2634): Activity state:
06-02 17:35:49.403: D/FragmentManager(2634): Local FragmentActivity 42a6b4c0 State:
06-02 17:35:49.403: D/FragmentManager(2634): mCreated=truemResumed=true mStopped=false mReallyStopped=false
06-02 17:35:49.403: D/FragmentManager(2634): mLoadersStarted=true
06-02 17:35:49.403: D/FragmentManager(2634): Active Fragments in 42a6b748:
06-02 17:35:49.403: D/FragmentManager(2634): #0: LoginFragment{42a8ece8 #0 id=0x7f090009 fragment_screen}
06-02 17:35:49.403: D/FragmentManager(2634): mFragmentId=#7f090009 mContainerId=#7f090009 mTag=fragment_screen
06-02 17:35:49.403: D/FragmentManager(2634): mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
06-02 17:35:49.403: D/FragmentManager(2634): mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
06-02 17:35:49.403: D/FragmentManager(2634): mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
06-02 17:35:49.403: D/FragmentManager(2634): mRetainInstance=false mRetaining=false mUserVisibleHint=true
06-02 17:35:49.403: D/FragmentManager(2634): mFragmentManager=FragmentManager{42a6b748 in RegistrazioneActivity{42a6b4c0}}
06-02 17:35:49.403: D/FragmentManager(2634): mActivity=it.sii.android.sporting.RegistrazioneActivity@42a6b4c0
06-02 17:35:49.403: D/FragmentManager(2634): Added Fragments:
06-02 17:35:49.403: D/FragmentManager(2634): #0: LoginFragment{42a8ece8 #0 id=0x7f090009 fragment_screen}
06-02 17:35:49.403: D/FragmentManager(2634): FragmentManager misc state:
06-02 17:35:49.403: D/FragmentManager(2634): mActivity=it.sii.android.sporting.RegistrazioneActivity@42a6b4c0
06-02 17:35:49.403: D/FragmentManager(2634): mContainer=android.support.v4.app.FragmentActivity$2@42a6b7c0
06-02 17:35:49.403: D/FragmentManager(2634): mCurState=5 mStateSaved=false mDestroyed=false
06-02 17:35:49.403: D/FragmentManager(2634): View Hierarchy:
06-02 17:35:49.403: D/FragmentManager(2634): com.android.internal.policy.impl.PhoneWindow$DecorView{42a6c2e0 V.E..... ... 0,0-1080,1776}
06-02 17:35:49.403: D/FragmentManager(2634): com.android.internal.widget.ActionBarOverlayLayout{42a6c920 V.ED.... ... 0,0-1080,1776 #1020319 android:id/action_bar_overlay_layout}
06-02 17:35:49.403: D/FragmentManager(2634): android.widget.FrameLayout{42a6d530 V.E..... ... 0,219-1080,1776 #1020002 android:id/content}
06-02 17:35:49.403: D/FragmentManager(2634): android.widget.ScrollView{42a74a20 VFED.V.. ... 0,0-1080,1557 #7f09000f app:id/registrazione}
06-02 17:35:49.403: D/FragmentManager(2634): android.widget.LinearLayout{42a75790 V.E..... ... 0,0-1080,1343}
06-02 17:35:49.403: D/FragmentManager(2634): android.widget.TextView{42a75aa8 V.ED.... ... 30,30-1050,131}
06-02 17:35:49.403: D/FragmentManager(2634): android.widget.TextView{42a761a8 V.ED.... ... 30,161-1050,218}
06-02 17:35:49.403: D/FragmentManager(2634): android.widget.EditText{42a768f0 VFED..CL F.. 30,218-1050,335 #7f090010 app:id/registerName}
06-02 17:35:49.403: D/FragmentManager(2634): android.widget.TextView{42a77ae8 V.ED.... ... 30,335-1050,392}
06-02 17:35:49.403: D/FragmentManager(2634): android.widget.EditText{42a781d8 VFED..CL ... 30,392-1050,509 #7f090011 app:id/registerEmail}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.TextView{42a793d0 V.ED.... ... 30,554-1050,611}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.EditText{42a79ac8 VFED..CL ... 30,611-1050,728 #7f090012 app:id/registerPassword}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.TextView{42a7b338 V.ED.... ... 30,728-1050,845 #7f090013 app:id/register_error}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.Button{42a7ba28 VFED..C. ... 30,905-1050,1049 #7f090014 app:id/btnRegister}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.Button{42a7c618 VFED..C. ..P 30,1169-1050,1313 #7f090015 app:id/btnLinkToLoginScreen}
06-02 17:35:49.413: D/FragmentManager(2634): com.android.internal.widget.ActionBarContainer{42a6d930 V.ED.... ... 0,75-1080,219 #102031a android:id/action_bar_container}
06-02 17:35:49.413: D/FragmentManager(2634): com.android.internal.widget.ActionBarView{42a6de38 V.E..... ... 0,0-1080,144 #102031b android:id/action_bar}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.LinearLayout{42a6e3a8 V.....C. ... 25,0-714,144}
06-02 17:35:49.413: D/FragmentManager(2634): com.android.internal.widget.ActionBarView$HomeView{42a6f438 V.E..... ... 0,0-120,144}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.ImageView{42a6f7d0 G.ED.... ... 0,0-0,0 #102025b android:id/up}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.ImageView{42a6fb78 V.ED.... ... 12,24-108,120 #102002c android:id/home}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.LinearLayout{42a70fc0 V.E..... ... 120,0-689,144}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.ImageView{42a712d8 G.ED.... ... 0,0-0,0 #102025b android:id/up}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.LinearLayout{42a71740 V.E..... ... 0,36-545,108}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.TextView{42a71a60 V.ED.... ... 0,0-545,72 #1020267 android:id/action_bar_title}
06-02 17:35:49.413: D/FragmentManager(2634): android.widget.TextView{42a72b60 G.ED.... ... 0,0-0,0 #1020268 android:id/action_bar_subtitle}
06-02 17:35:49.413: D/FragmentManager(2634): com.android.internal.view.menu.ActionMenuView{42a889e8 V.ED.... ... 1080,0-1080,144}
06-02 17:35:49.413: D/FragmentManager(2634): com.android.internal.widget.ActionBarContextView{42a73768 G.E..... ... 0,0-0,0 #102031c android:id/action_context_bar}
06-02 17:35:49.413: D/FragmentManager(2634): com.android.internal.widget.ActionBarContainer{42a73d38 G.ED.... ... 0,0-0,0 #102031d android:id/split_action_bar}
06-02 17:35:49.413: D/AndroidRuntime(2634): Shutting down VM
06-02 17:35:49.413: W/dalvikvm(2634): threadid=1: thread exiting with uncaught exception (group=0x4189be48)
06-02 17:35:49.413: E/AndroidRuntime(2634): FATAL EXCEPTION: main
06-02 17:35:49.413: E/AndroidRuntime(2634): Process: it.sii.android.sporting, PID: 2634
06-02 17:35:49.413: E/AndroidRuntime(2634): java.lang.IllegalArgumentException: No view found for id 0x7f090009 (it.sii.android.sporting:id/fragment_login) for fragment LoginFragment{42a8ece8 #0 id=0x7f090009 fragment_screen}
06-02 17:35:49.413: E/AndroidRuntime(2634): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
06-02 17:35:49.413: E/AndroidRuntime(2634): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-02 17:35:49.413: E/AndroidRuntime(2634): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-02 17:35:49.413: E/AndroidRuntime(2634): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-02 17:35:49.413: E/AndroidRuntime(2634): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
06-02 17:35:49.413: E/AndroidRuntime(2634): at android.os.Handler.handleCallback(Handler.java:733)
06-02 17:35:49.413: E/AndroidRuntime(2634): at android.os.Handler.dispatchMessage(Handler.java:95)
06-02 17:35:49.413: E/AndroidRuntime(2634): at android.os.Looper.loop(Looper.java:136)
06-02 17:35:49.413: E/AndroidRuntime(2634): at android.app.ActivityThread.main(ActivityThread.java:5105)
06-02 17:35:49.413: E/AndroidRuntime(2634): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 17:35:49.413: E/AndroidRuntime(2634): at java.lang.reflect.Method.invoke(Method.java:515)
06-02 17:35:49.413: E/AndroidRuntime(2634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
06-02 17:35:49.413: E/AndroidRuntime(2634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
06-02 17:35:49.413: E/AndroidRuntime(2634): at dalvik.system.NativeStart.main(Native Method)
06-02 17:35:51.763: I/Process(2634): Sending signal. PID: 2634 SIG: 9
06-02 17:35:51.863: D/HyLog(4738): I : /data/font/config/sfconfig.dat, No such file or directory (2)
06-02 17:35:51.863: D/HyLog(4738): I : /data/font/config/dfactpre.dat, No such file or directory (2)
06-02 17:35:51.863: D/HyLog(4738): I : /data/font/config/sfconfig.dat, No such file or directory (2)
06-02 17:35:51.923: D/BubblePopupHelper(4738): isShowingBubblePopup : false
06-02 17:35:51.923: D/BubblePopupHelper(4738): isShowingBubblePopup : false
06-02 17:35:51.963: I/Adreno-EGL(4738): <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build: ()
06-02 17:35:51.963: I/Adreno-EGL(4738): OpenGL ES Shader Compiler Version: E031.24.00.02
06-02 17:35:51.963: I/Adreno-EGL(4738): Build Date: 01/20/14 Mon
06-02 17:35:51.963: I/Adreno-EGL(4738): Local Branch: PMH2-KK_3.5-RB1-AU61-554722-586267-set2
06-02 17:35:51.963: I/Adreno-EGL(4738): Remote Branch:
06-02 17:35:51.963: I/Adreno-EGL(4738): Local Patches:
06-02 17:35:51.963: I/Adreno-EGL(4738): Reconstruct Branch:
06-02 17:35:51.983: D/OpenGLRenderer(4738): Enabling debug mode 0
06-02 17:35:52.013: D/BubblePopupHelper(4738): isShowingBubblePopup : false
06-02 17:35:52.013: D/BubblePopupHelper(4738): isShowingBubblePopup : false
06-02 17:35:52.013: D/BubblePopupHelper(4738): isShowingBubblePopup : false
06-02 17:35:52.013: D/BubblePopupHelper(4738): isShowingBubblePopup : false
06-02 17:35:52.073: D/BubblePopupHelper(4738): isShowingBubblePopup : false
06-02 17:35:52.073: D/BubblePopupHelper(4738): isShowingBubblePopup : false
06-02 17:35:52.073: D/BubblePopupHelper(4738): isShowingBubblePopup : false
06-02 17:35:52.073: D/BubblePopupHelper(4738): isShowingBubblePopup : false
帮我。另外,有没有更好的方法来组织所有的类、片段、活动等?