[英]How do I stop my android app from crashing in the emulator?
I am a student and this is an assignment. 我是学生,这是一项作业。 I followed the directions for this program but when I run my app it crashes. 我按照该程序的说明进行操作,但是当我运行应用程序时,它崩溃了。 Based off of the stack trace I believe the issue is with the Intent. 基于堆栈跟踪,我认为问题在于意图。 But I do not know for sure. 但是我不确定。 Could someone examine my code and explain what is wrong and why. 有人可以检查我的代码并解释出什么问题和原因。
The main activity that calls another 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);
}
}
The other 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;
}
}
The manifest 清单
<?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>
Stack trace from the debug 调试中的堆栈跟踪
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
If you need any of the other XML files please let me know. 如果您需要任何其他XML文件,请告诉我。
What you are doing here is wrong I suppose: 我想你在这里做什么是错误的:
//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.
Just replace: 只需替换:
this.nameField.findViewById(R.id.editText1);
and 和
this.okButton.findViewById(R.id.button1);
By: 通过:
nameField = (EditText) findViewById(R.id.editText1);
and 和
okButton = (Button) findViewById(R.id.button1);
Hope this helps. 希望这可以帮助。
You didn't provide the full stacktrace so its hard to say exactly what is causing your first error but there are many issues here. 您没有提供完整的堆栈跟踪信息,因此很难确切地说出是什么导致了您的第一个错误,但是这里有很多问题。 I will provide some comments in the code that hopefully will help. 我将在代码中提供一些注释,希望会对您有所帮助。
First, you almost had your initialization of your View
s correct but you changed them. 首先,您几乎正确地初始化了View
但是您更改了它们。 Change your onCreate()
from 从更改您的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);
}
to 至
@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);
}
Also change your initialization of the Intent
to use the Activty
Context
by changing 还可以通过更改来更改使用Activty
Context
的Intent
的初始化
//Create an Intent to call another activity
Intent myIntent = new Intent(this, LayoutMainActivity.class);
to 至
//Create an Intent to call another activity
Intent myIntent = new Intent(EnterNameActivity .this, LayoutMainActivity.class);
Here is another problem 这是另一个问题
if(tempText != ""){
when you compare String
s this way you compare if the objects are equal but not what they reference. 当您以这种方式比较String
时,您将比较对象是否相等,而不是它们所引用的对象。 It should be 它应该是
if(!"".equals(tempText)){
This says, if an empty String
is not equal to the value of tempText
. 这就是说,如果一个空的String
不等于tempText
的值。 Alternatively you may see 或者,您可能会看到
if (!tempText.equals("")){
but the first way will protect against a NPE
because if tempText
is null
you will get a NPE
in the second way since you would be calling a function on an object that is null
但是第一种方法可以防止NPE
因为如果tempText
为null
,则第二种方法将获得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);
}
this is better way to deal with edittexts, buttons, and onClicks. 这是处理文本,按钮和onClicks的更好方法。
*remove onClick from activity, and remove onclick method and button and edittext importing methods that u used this. *从活动中删除onClick,并删除您使用过的onclick方法和按钮以及edittext导入方法。
Try this way 试试这个
@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.