Android应用程序保持“关闭”,但logcat中没有显示错误

时间:2015-04-23 02:36:15

标签: android logcat

这是我的主要课程,它将把用户名转发给我的GoalActivity课程。我无法弄清问题在哪里。它因我不明原因而不断崩溃。我已经按照各种教程,我无法弄清楚这个问题。我似乎正确检索用户名,然后将其转换为字符串。然后创建一个Intent并用密钥传递用户名值。

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();
    }
}

这是我的GoalActivity类,它接收来自MainActivity类的信息。

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>

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

解决。这是一个非常粗心的错误。我更新了Manifest中的所有其他活动,但更新了GoalActivity.java的activity标记。谢谢大家的帮助!