簡體   English   中英

為什么我的android應用程序崩潰了?

[英]Why is my android application crashing?

因此,我知道這與我的“ 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>

輸入密碼並按“確定”按鈕后,無論輸出什么,我的代碼都會立即退出。 我已經注釋掉了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>

請發布您的logcat錯誤,但是我的猜測是確保清單文件中包含LevelOneLevelZero 此外,可能有助於更改此設置:

loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE )

至:

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

是否在清單文件中注冊LevelOneLevelZero

這里是一個類似的SO問題的鏈接,該問題顯示了如何將活動添加到清單中。

向AndroidManifest添加新活動?

編輯 :我正在更新我的答案

所以最后我到了你要去哪里錯了

  • 檢查您的狀況
  • 在accessCodeEntered()方法中
  • 的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);
        }
        }

    }
}

嘗試實施此代碼以及您要去哪里。

還是你和我分享麻煩。謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM