大家好,我在学习 Android 时正在编写一个 name getter 应用程序,当它加载到模拟器中时,我被迫关闭该应用程序。我很新,所以这可能很简单。
namegetter.java
package edu.calpoly.android.lab1;
// tools
import android.app.Activity;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class NameGetter extends Activity implements android.view.View.OnClickListener{
// variables
private android.widget.EditText textEdit_Name;
private android.widget.Button button_Submit;
private String string_Name;
private android.content.Intent intent_GetText;
private android.os.Bundle bundle;
private android.widget.TextView textView_Top;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.name_getter);
// assign local variables
textEdit_Name = (EditText) this.findViewById(R.id.editText_Name);
button_Submit = (Button) this.findViewById(R.id.button_Submit);
textView_Top = (TextView) this.findViewById(R.id.LABEL1);
button_Submit.setOnClickListener(this);
bundle = this.getIntent().getExtras();
bundle.getString(string_Name);
textView_Top.setText(string_Name);
}
public void onClick(View v) {
string_Name = textEdit_Name.getText().toString();
// create intent to pass
intent_GetText= new Intent(this, HelloWorld.class);
// pass to hello world activity
intent_GetText.putExtra(string_Name, textEdit_Name.getText().toString());
// initiate
this.startActivity(intent_GetText);
}
}
list .xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.calpoly.android.lab1"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="12" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="NameGetter"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="HelloWorld" ></activity>
</application>
</manifest>
最佳答案
最可能的原因是 getExtras()
返回 null。
bundle = this.getIntent().getExtras();
bundle.getString(string_Name);
如需更多帮助,我建议收集 logcat (adb logcat
) 并发布实际异常。
关于java - Android 新手,我的简单取名应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6430948/