gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:34:30 25 4
gpt4 key购买 nike

我是一名学生,这是一项作业。我遵循了该程序的说明,但是当我运行我的应用程序时它崩溃了。基于堆栈跟踪,我认为问题出在 Intent 上。但我不确定。有人可以检查我的代码并解释什么是错误的以及为什么。

调用另一个 Activity 的主要 Activity

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

}

其他 Activity

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

}

list

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

最佳答案

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

首先,您几乎正确地初始化了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);
}

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

但第一种方法将防止 NPE,因为如果 tempTextnull,您将获得 NPE在第二种方式中,因为您将在 null

的对象上调用函数

关于android - 如何阻止我的 Android 应用程序在模拟器中崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18710027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com