如何阻止我的Android应用程序在模拟器中崩溃?

时间:2013-09-10 02:54:13

标签: android

我是学生,这是一项任务。我按照这个程序的指示,但是当我运行我的应用程序时,它崩溃了。基于堆栈跟踪我相信问题出在Intent上。但我不确定。有人可以检查我的代码并解释错误和原因。

调用其他活动的主要活动

  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文件,请告诉我们。

4 个答案:

答案 0 :(得分:1)

我想你在做什么是错的:

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

希望这有帮助。

答案 1 :(得分:1)

您没有提供完整的堆栈跟踪,因此很难准确说出导致您出现第一个错误的原因,但此处存在许多问题。我将在代码中提供一些希望有用的评论。

首先,你几乎已经初步确定了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);
}

同时更改<{1}}的初始化,以通过更改

来使用Intent Activty
Context

  //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("")){ ,因为如果NPEtempText,您将以第二种方式获得null,因为您将调用函数一个NPE

的对象

答案 2 :(得分:0)

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

这是处理edittexts,按钮和onClicks的更好方法。

*从活动中删除onClick,并删除onclick方法和按钮以及您使用此方法的edittext导入方法。

答案 3 :(得分:0)

试试这种方式

@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

    }