为什么我的Android应用程序崩溃了?

时间:2012-06-06 17:06:13

标签: android android-intent runtime-error

所以我知道它与我的“startActivity(myIntent)”代码行有关。这是我的第一个活动文件

public class Commander2 extends Activity {
    /** Called when the activity is first created. */

    private static final String TAG = "AccessCodeEntry";
    private static final String LEVEL_ONE_ACCESS_CODE = "derp";

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.accesscodeentry );
    }

protected void onDestroy() {
    super.onDestroy();
}

public void accessCodeEntered( View v ) {
    EditText loginPassword = (EditText) findViewById( R.id.editText1 );
    if( loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE ) ) {
        Intent myIntent = new Intent( Commander2.this, LevelOne.class );
        startActivity( myIntent );
    } else {
        Intent myIntent = new Intent( Commander2.this, LevelZero.class );
        startActivity( myIntent );
    }
}
}

这也是XML文件。

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="accessCodeEntered"
        android:text="OK" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="19dp"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText1"
        android:layout_alignLeft="@+id/editText1"
        android:text="Enter Access Code, or leave blank for Level 0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

我的代码只要输入密码就退出,然后按OK按钮,无论输出是什么。我已经注释掉了startActivity(myIntent)行,代码完成而没有崩溃。有谁知道我做错了什么?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="my.eti"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
    <activity android:name=".Commander2"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity android:name=".LevelOne"
              android:label="@string/app_name">
        <intent-filter>                
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".LevelZero"
              android:label="@string/app_name">
        <intent-filter>                
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

4 个答案:

答案 0 :(得分:1)

请发布您的logcat错误,但我的猜测是确保LevelOneLevelZero在您的清单文件中。此外,它可能有助于改变这一点:

loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE )

为:

loginPassword.getText().toString().equals( LEVEL_ONE_ACCESS_CODE )

答案 1 :(得分:1)

让您在清单文件中注册 LevelOne LevelZero

答案 2 :(得分:1)

以下是类似SO问题的链接,该问题显示了如何将活动添加到清单。

Add a new activity to the AndroidManifest?

答案 3 :(得分:1)

  

编辑:我正在更新我的回答

所以最后我到了你错的地方

  • 检查你的条件
  • 在accessCodeEntered()方法
  • of Commander2.java活动

 if(loginPassword.toString().equals(LEVEL_ONE_ACCESS_CODE)){}

这里你没有得到密码的价值并尝试使用它 所以我添加了“getText()”方法,其工作正常,如此


if(loginPassword.getText().toString().equals(LEVEL_ONE_ACCESS_CODE)){}

Commander2.java


package my.eti;

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

public class Commander2 extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    private static final String TAG = "AccessCodeEntry";
    private static final String LEVEL_ONE_ACCESS_CODE = "derp";

    private Button btnOK;
    private EditText passTxt;

    private String strPass;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accesscodeentry);

        passTxt = (EditText) findViewById(R.id.editText1);

        btnOK =(Button) findViewById(R.id.button1);
        btnOK.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(v==btnOK){

            /*... here is the problem we havent added getText() to get value from EditText...*/
        if(loginPassword.getText().toString().equals(LEVEL_ONE_ACCESS_CODE)){
            Intent myIntent = new Intent( Commander2.this, LevelOne.class );
            Toast.makeText(getBaseContext(), "if condition is true..", Toast.LENGTH_SHORT).show();
            startActivity( myIntent );
        }
        else{
            Intent myIntent = new Intent( Commander2.this, LevelZero.class );
            Toast.makeText(getBaseContext(), "else condition is true..", Toast.LENGTH_SHORT).show();

            startActivity(myIntent);
        }
        }

    }
}

尝试实现此代码以及出错的地方。

仍然有任何麻烦与我分享谢谢。

相关问题