【问题标题】:How do I stop my android app from crashing in the emulator?如何阻止我的 android 应用程序在模拟器中崩溃?
【发布时间】:2013-09-10 02:54:12
【问题描述】:

我是一名学生,这是一项作业。我按照该程序的说明进行操作,但是当我运行我的应用程序时它崩溃了。根据堆栈跟踪,我认为问题出在 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 文件,请告诉我。

【问题讨论】:

  • 所有 logcat 都说设备已断开连接。

标签: android


【解决方案1】:

您没有提供完整的堆栈跟踪,因此很难准确说明是什么导致了您的第一个错误,但这里有很多问题。我将在代码中提供一些 cmets,希望对您有所帮助。

首先,您的Views 的初始化几乎正确,但您更改了它们。将您的 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);
}

还可以通过更改将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 != ""){

当您以这种方式比较 Strings 时,您比较的是对象是否相等,而不是它们引用的对象。应该是

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

这表示,如果一个空的String 不等于tempText 的值。或者你可能会看到

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

但是第一种方法可以防止NPE,因为如果tempTextnull,您将在第二种方法中获得NPE,因为您将在null 的对象上调用函数

【讨论】:

  • 谢谢,我确实发布了堆栈跟踪中出现的所有内容。但是后来我有超过 2 个小时的时间在模拟器上什么都不会运行,现在 logcat 和控制台也没有显示任何东西。在活动上下文中,“this”不是已经提供了上下文吗?
  • 如果可以,在实际设备上进行测试总是更好。至于Context,你在这里应该没问题,但如果你继续为Android开发,你真的需要了解何时使用哪个ContextThis answer 应该对此有所帮助。另外,请特别注意我关于比较Strings 的回答的最后一部分,因为如果您不理解它会在以后得到您
  • 我对 if 语句进行了更改。比较对象和原语之间的区别是那些还没有卡住的事情之一。我阅读了另一篇文章的一部分并将完成它,但我现在明白你的意思了。 “this”可以指的是当我真正想要活动时它所处的方法。规则太多,大脑空间太小!
  • 继续努力。突然间它会开始点击,你会没事的
【解决方案2】:

我想你在这里做的是错误的:

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

希望这会有所帮助。

【讨论】:

  • 谢谢,这解决了问题,现在一切正常。如果我这样做了,它会起作用吗: this.nameField = (EditText) findViewById(R.id.editText1);
【解决方案3】:
        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);
}

这是处理编辑文本、按钮和 onClick 的更好方法。

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

【讨论】:

  • 您为什么建议更改onClick() 的处理方式?恕我直言,OP的做法很好而且更干净。问题似乎与onClick() 的处理方式无关,而与变量的初始化有关
【解决方案4】:

试试这个方法

@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

    }

【讨论】:

    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2019-07-06
    相关资源
    最近更新 更多