Android应用程序一直“关闭”,但是logcat中没有显示任何错误

4

这是我的主要类,它将把用户名转发到我的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>

尝试关闭并重新打开你的 Eclipse。 - Blaze Tama
我正在使用Android Studio。 - blu_tempo
请在GoalActivity.java中发布传输数据的代码。 - Daniel Nugent
@Daniel,不确定你在问什么,我想要传输的代码从MainActivity.java中的Intent intro开始。 - blu_tempo
顺便问一下,为什么您在单击处理程序中引用编辑文本得这么晚。在onCreate中初始化并引用它们,然后只需在signIn(View View)上设置/放置值即可。 - mike20132013
显示剩余5条评论
2个回答

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");

仍然是同样的问题。它初始化得很好,但是一旦我使用正确的信息登录,它就会崩溃。 - blu_tempo
你需要什么信息?它还在崩溃。两个类的XML代码? - blu_tempo
顺便说一句,当我只让它制作 Toast 时,它运行得非常完美。但是一旦我尝试传输数据,它每次都会崩溃。 - blu_tempo
1
我能想到的唯一方法就是检查 TextView 的 ID 是否正确。如果正确,尝试显示另一个字符串(而不是传输的数据),看看会发生什么。 - Musa
Boo。ID 正确,已经四次核对。已经制作了一个 Toast 显示用户名并且正常工作。 - blu_tempo
显示剩余2条评论

0
解决了。这是一个非常粗心的错误。我已经在清单文件中更新了所有其他活动,但是忘记更新GoalActivity.java的活动标签。感谢大家的帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接