簡體   English   中英

如何停止模擬器中的Android應用崩潰?

[英]How do I stop my android app from crashing in the emulator?

我是學生,這是一項作業。 我按照該程序的說明進行操作,但是當我運行應用程序時,它崩潰了。 基於堆棧跟蹤,我認為問題在於意圖。 但是我不確定。 有人可以檢查我的代碼並解釋出什么問題和原因。

調用另一個活動的主要活動

  package edu.cvtc.android.activitylab;

  import android.os.Bundle;
  import android.app.Activity;
  import android.content.Intent;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;


  public class EnterNameActivity extends Activity implements OnClickListener{

    android.widget.EditText nameField;
    android.widget.Button okButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.name_getter);

        //EditText nameField = (EditText) findViewById(R.id.editText1);
        this.nameField.findViewById(R.id.editText1); 
        //Button okButton =  (Button) findViewById(R.id.button1);
        this.okButton.findViewById(R.id.button1);
        //EditText and Button are used to type cast the variables because
        //findViewById only returns an object. 

        okButton.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_entername, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // Get the text value the user entered
        String tempText =  nameField.getText().toString();

        //Clear the text field if there is data
        if(tempText != ""){

            nameField.setText("");
        }

        //Create an Intent to call another activity
        Intent myIntent = new Intent(this, LayoutMainActivity.class);

        //Passing the user entered name to the main greeting activity
        myIntent.putExtra("name", tempText);
        this.startActivity(myIntent);
    }

  }

其他活動

  package edu.cvtc.android.activitylab;

  import android.os.Bundle;
  import android.app.Activity;
  import android.view.Menu;
  import android.widget.TextView;

  public class LayoutMainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);

        android.os.Bundle temp = this.getIntent().getExtras();

        if(temp != null){
            //Extract the username from the bundle
            String userName = temp.getString("name");

            //get the TextView Id to change the text
            TextView text = (TextView) findViewById(R.id.textView1);

            text.setText("Hello " + userName);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_layout_main, menu);
        return true;
    }

  }

清單

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="edu.cvtc.android.activitylab"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="edu.cvtc.android.activitylab.LayoutMainActivity"
            android:label="@string/app_name" >

        </activity>
        <activity
            android:name="edu.cvtc.android.activitylab.EnterNameActivity"
            android:label="@string/title_activity_enter_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

  </manifest>

調試中的堆棧跟蹤

Thread [<1> main] (Suspended (exception RuntimeException))  
    <VM does not provide monitor information>   
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2261 
    ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 141    
    ActivityThread$H.handleMessage(Message) line: 1256  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 137 
    ActivityThread.main(String[]) line: 5103    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 525  
    ZygoteInit$MethodAndArgsCaller.run() line: 737  
    ZygoteInit.main(String[]) line: 553

如果您需要任何其他XML文件,請告訴我。

我想你在這里做什么是錯誤的:

//EditText nameField = (EditText) findViewById(R.id.editText1);
        this.nameField.findViewById(R.id.editText1); 
        //Button okButton =  (Button) findViewById(R.id.button1);
        this.okButton.findViewById(R.id.button1);
        //EditText and Button are used to type cast the variables because
        //findViewById only returns an object.

只需替換:

this.nameField.findViewById(R.id.editText1); 

this.okButton.findViewById(R.id.button1);

通過:

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

okButton =  (Button) findViewById(R.id.button1);

希望這可以幫助。

您沒有提供完整的堆棧跟蹤信息,因此很難確切地說出是什么導致了您的第一個錯誤,但是這里有很多問題。 我將在代碼中提供一些注釋,希望會對您有所幫助。

首先,您幾乎正確地初始化了View但是您更改了它們。 從更改您的onCreate()

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.name_getter);

    //EditText nameField = (EditText) findViewById(R.id.editText1);
    this.nameField.findViewById(R.id.editText1); // this returns an EditText object so it isn't being applied to a variable. 
    //Button okButton =  (Button) findViewById(R.id.button1);
    this.okButton.findViewById(R.id.button1);
    //EditText and Button are used to type cast the variables because
    //findViewById only returns an object. 

    okButton.setOnClickListener(this);
}

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.name_getter);

    EditText nameField = (EditText) findViewById(R.id.editText1);
    Button okButton =  (Button) findViewById(R.id.button1);

    okButton.setOnClickListener(this);
}

還可以通過更改來更改使用Activty ContextIntent的初始化

  //Create an Intent to call another activity
    Intent myIntent = new Intent(this, LayoutMainActivity.class);

  //Create an Intent to call another activity
    Intent myIntent = new Intent(EnterNameActivity .this, LayoutMainActivity.class);

這是另一個問題

if(tempText != ""){

當您以這種方式比較String時,您將比較對象是否相等,而不是它們所引用的對象。 它應該是

if(!"".equals(tempText)){

這就是說,如果一個空的String不等於tempText的值。 或者,您可能會看到

if (!tempText.equals("")){

但是第一種方法可以防止NPE因為如果tempTextnull ,則第二種方法將獲得NPE ,因為您將在null對象上調用函數

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

        Button okButton =  (Button) findViewById(R.id.button1);

        //EditText and Button are used to type cast the variables because
        //findViewById only returns an object. 

okButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
        // Get the text value the user entered
        String tempText =  nameField.getText().toString();

        //Clear the text field if there is data
        if(tempText != ""){

            nameField.setText("");
        }

        //Create an Intent to call another activity
        Intent myIntent = new Intent(EnterNameActivity.this, LayoutMainActivity.class);

        //Passing the user entered name to the main greeting activity
        myIntent.putExtra("name", tempText);
        startActivity(myIntent);
}

這是處理文本,按鈕和onClicks的更好方法。

*從活動中刪除onClick,並刪除您使用過的onclick方法和按鈕以及edittext導入方法。

試試這個

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);




            //Extract the username from the bundle
            String userName = getIntent().getStringExtra("name"); // UPDATE HERE

            //get the TextView Id to change the text
            TextView text = (TextView) findViewById(R.id.textView1);

            text.setText("Hello " + userName!=null?userName:""); // UPDATE HERE

    }

暫無
暫無

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

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