【问题标题】:Why is my android application crashing?为什么我的安卓应用程序崩溃了?
【发布时间】:2012-06-06 17:06:12
【问题描述】:

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

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>

【问题讨论】:

  • 您是否将 LevelOne Activity 添加到清单中?

标签: android android-intent runtime-error


【解决方案1】:

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

所以我终于知道你哪里错了

  • 检查您的 if 条件
  • 在 accessCodeEntered() 方法中
  • Commander2.java Activity

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

    }
}

尝试实现此代码以及哪里出错了。

还有什么问题请与我分享谢谢。

【讨论】:

  • 不幸的是,它仍然崩溃,我确实将清单文件更改为您拥有的文件,将 SDK 版本更改为 10。
  • 确保您也应该将“target=android-8”这一行上的“default.properties”文件更改为 10。
  • 是的,您可以更改它,现在只有我将我的应用 API 10 更改为 8,我已经看到了您发布的清单,我想知道您为什么没有这个“ 到你的活动 "
  • 更改所有这些内容后,请确保以这种方式清理您的项目 [选择项目 --> 转到“项目”菜单-->单击“清理”选项->然后再次运行应用程序]
  • 我已经给你发送了更新的源代码,检查邮件,我得到的是你在 if 条件 >> 在 accessCodeEntered() 方法 >> of Commander2.java Activity .... 添加 getText( ) 方法之前 toString 到 loginPassword ...你得到了吗?
【解决方案2】:

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

loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE )

到:

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

【讨论】:

    【解决方案3】:

    你有没有在清单文件中注册LevelOneLevelZero

    【讨论】:

      【解决方案4】:

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

      Add a new activity to the AndroidManifest?

      【讨论】:

      • 好吧,我的清单中没有它,所以谢谢,但现在它给了我另一个错误:“找不到启动器活动!启动只会同步设备上的应用程序包!”这是我的清单文件:
      • 您仍然收到该错误吗? (看起来你不应该通过查看你的清单)或者你只是得到'无法解决目标'?如果是后者,请转到您的项目属性并更改构建目标。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      • 2010-09-20
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多