2015-04-23 39 views
4

這是我的主類,它將用戶名轉發到我的GoalActivity類。我無法弄清楚問題出在哪裏。它對我來說不斷崩潰,原因不明。我跟着各種教程,我無法弄清楚這個問題。我似乎正確地檢索用戶名,然後將其轉換爲字符串。然後創建一個Intent並通過密鑰傳遞用戶名值。Android應用保持「關閉」狀態,但logcat中沒有顯示錯誤

MainActivity.java

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.example.agray.carpediem.LoginDataBaseAdapter; 
import com.example.agray.carpediem.R; 
import com.example.agray.carpediem.SignUPActivity; 

public class MainActivity extends Activity 
{ 
    Button btnSignIn,btnSignUp; 
    LoginDataBaseAdapter loginDataBaseAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //create instance of SQLite Database 
     loginDataBaseAdapter=new LoginDataBaseAdapter(this); 
     loginDataBaseAdapter=loginDataBaseAdapter.open(); 

     //create reference to the buttons used 
     btnSignIn=(Button)findViewById(R.id.buttonSignIN); 
     btnSignUp=(Button)findViewById(R.id.buttonSignUP); 

     // Signup button w/onclick listener 
     btnSignUp.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       // TODO Auto-generated method stub 

       /// Create Intent for SignUpActivity 
       Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class); 
       //start the activity w/intent 
       startActivity(intentSignUP); 
      } 
     }); 
    } 
    // Methods to handleClick Event of Sign In Button 
    public void signIn(View View) 
    { 
     final Dialog dialog = new Dialog(MainActivity.this); 
     dialog.setContentView(R.layout.login); 
     dialog.setTitle("Login"); 

     //get the References of views 
     final EditText editTextUserName= 
       (EditText)dialog.findViewById(R.id.editTextUserNameToLogin); 
     final EditText editTextPassword= 
       (EditText)dialog.findViewById(R.id.editTextPasswordToLogin); 

     Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn); 

     //Signin Button w/ onClickListener 
     btnSignIn.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       //store username and password as strings 
       String userName=editTextUserName.getText().toString(); 
       String password=editTextPassword.getText().toString(); 

       //fetch the Password from the DB for respective username 
       String storedPassword=loginDataBaseAdapter.getSingleEntry(userName); 

       // check if the Stored password matches with Password entered by user 
       if(password.equals(storedPassword)) 
       { 
        Toast.makeText(MainActivity.this, "Congrats: Login is Successful " + userName, 
          Toast.LENGTH_LONG).show(); 
        dialog.dismiss(); 

//     final EditText editTextUserName= 
//       (EditText)dialog.findViewById(R.id.editTextUserNameToLogin); 
//     String userName=editTextUserName.getText().toString(); 
        //create intent that will start the goals activity w/some data 
        Intent intro = new Intent(getApplicationContext(), GoalActivity.class); 

        //put the username into intent 
        intro.putExtra("USER_NAME", userName); 
        startActivity(intro); 
       } 
       else 
       { 
        Toast.makeText(MainActivity.this, "Access Denied: User Name or Password " + 
            "does not match", 
          Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 

     dialog.show(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     // Close The Database 
     loginDataBaseAdapter.close(); 
    } 
} 

下面是從在MainActivity類接收信息我GoalActivity類。

GoalActivity.java

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 

public class GoalActivity extends Activity{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.goals_page); 

     //get the username from the intent 
     String enteredUserName = getIntent().getStringExtra("USER_NAME"); 

     final TextView tv = (TextView)findViewById(R.id.user_name_forwarded); 
     tv.setText(enteredUserName); 
    } 

} 

login.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/editTextUserNameToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="User Name" 
     android:ems="10" > 
     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editTextPasswordToLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textPassword" 
     android:hint="Password" /> 

    <Button 
     android:id="@+id/buttonSignIn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Sign In" /> 

</LinearLayout> 

goals_page.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/welcome_goals" 
     android:textSize="50sp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/user_name_forwarded" 
     android:text="@string/emptyString" 
     android:layout_weight="0.09"/> 




</LinearLayout> 

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.agray.carpediem" > 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="CarpeD" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.agray.carpediem.MainActivity" 
      android:label="CarpeD" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".SignUPActivity"/> 
     <activity android:name=".GoalActivity"/> 
    </application> 

</manifest> 
+0

嘗試關閉並重新打開您的eclipse –

+0

您可以獲取堆棧跟蹤? –

+0

我正在使用Android Studio .. – user3413605

回答

0

試試這個,例如在ActivityA

Intent i = new Intent(ActivityA.this, ActivityB.class); 
i.putExtra("USER_NAME", userNameString); 
startActivity(i); 

ActivityB

Bundle extras = getIntent().getExtras(); 
if (extras == null) { 
    return; 
} 

String USERNAME = extras.getString("USER_NAME"); 
+0

仍然是同樣的問題。它初始化很好,但只要我用正確的信息登錄,它就會崩潰。 – user3413605

+0

將上面的代碼添加到您的'GoalActivity'中的ActivityB中,該代碼將正確地接收來自之前活動的userName字符串。如果出現錯誤,那麼它肯定是來自別的東西,我們需要更多的錯誤細節,所以我們可以幫助 –

+0

你需要什麼信息?它仍然崩潰。這兩個類的xml代碼? – user3413605

0

解決。這是一個非常粗心的錯誤。我已經更新了Manifest中的所有其他活動,但更新了GoalActivity.java的活動標記。謝謝你們的幫助!

相關問題