我制作了一个Android应用程序,每次我在Emulator中运行它都会停止。即使我没有错误!它是一个非常简单的应用程序...但在模拟器中它崩溃。我不知道为什么会发生这种情况。在模拟器中出现“进程已意外停止”或类似的内容......
这是LogCat:
05-05 09:26:58.699: D/AndroidRuntime(1222): Shutting down VM
05-05 09:26:58.749: W/dalvikvm(1222): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-05 09:26:58.819: E/AndroidRuntime(1222): FATAL EXCEPTION: main
05-05 09:26:58.819: E/AndroidRuntime(1222): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.create.namestrings/com.create.namestrings.MainActivity}: java.lang.NullPointerException
05-05 09:26:58.819: E/AndroidRuntime(1222): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-05 09:26:58.819: E/AndroidRuntime(1222): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-05 09:26:58.819: E/AndroidRuntime(1222): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-05 09:26:58.819: E/AndroidRuntime(1222): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-05 09:26:58.819: E/AndroidRuntime(1222): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 09:26:58.819: E/AndroidRuntime(1222): at android.os.Looper.loop(Looper.java:137)
05-05 09:26:58.819: E/AndroidRuntime(1222): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-05 09:26:58.819: E/AndroidRuntime(1222): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 09:26:58.819: E/AndroidRuntime(1222): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 09:26:58.819: E/AndroidRuntime(1222): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-05 09:26:58.819: E/AndroidRuntime(1222): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-05 09:26:58.819: E/AndroidRuntime(1222): at dalvik.system.NativeStart.main(Native Method)
05-05 09:26:58.819: E/AndroidRuntime(1222): Caused by: java.lang.NullPointerException
05-05 09:26:58.819: E/AndroidRuntime(1222): at android.app.Activity.findViewById(Activity.java:1839)
05-05 09:26:58.819: E/AndroidRuntime(1222): at com.create.namestrings.MainActivity.<init>(MainActivity.java:16)
05-05 09:26:58.819: E/AndroidRuntime(1222): at java.lang.Class.newInstanceImpl(Native Method)
05-05 09:26:58.819: E/AndroidRuntime(1222): at java.lang.Class.newInstance(Class.java:1319)
05-05 09:26:58.819: E/AndroidRuntime(1222): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-05 09:26:58.819: E/AndroidRuntime(1222): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-05 09:26:58.819: E/AndroidRuntime(1222): ... 11 more
05-05 09:27:51.169: E/Trace(1263): error opening trace file: No such file or directory (2)
这是java文件:
package com.create.namestrings;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText etName = (EditText) findViewById (R.id.editText1);
RadioButton RbtnGirl = (RadioButton) findViewById (R.id.radioButton1);
RadioButton RbtnBoy = (RadioButton) findViewById (R.id.radioButton2);
Button btn = (Button) findViewById (R.id.button1);
TextView txt = (TextView) findViewById (R.id.textView2);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = etName.getText().toString();
Random r = new Random();
if (RbtnGirl.isChecked()){
switch (r.nextInt(6)){
case 0 :
txt.setText("Esti frumoasa, "+ name);
txt.setTextColor(Color.CYAN);
break;
case 1 :
txt.setText("Mi-e greata cand te vad, "+ name);
txt.setTextColor(Color.GREEN);
break;
case 2 :
txt.setText("Esti grasa, "+ name);
txt.setTextColor(Color.BLUE);
break;
case 3 :
txt.setText("Esti puturoasa, "+ name);
txt.setTextColor(Color.YELLOW);
break;
case 4 :
txt.setText("Esti minunata "+ name);
txt.setTextColor(Color.DKGRAY);
break;
case 5 :
txt.setText("Esti la moda, "+ name);
txt.setTextColor(Color.MAGENTA);
break;
}
}else if (RbtnBoy.isChecked()){
switch (r.nextInt(6)){
case 0 :
txt.setText("Esti frumos, "+ name);
txt.setTextColor(Color.CYAN);
break;
case 1 :
txt.setText("Mai,"+name+"!Esti inalt ca bradu' da' prost ca gardu'!");
txt.setTextColor(Color.RED);
break;
case 2 :
txt.setText("Esti gras, "+ name);
txt.setTextColor(Color.GREEN);
break;
case 3 :
txt.setText("Esti puturos, "+ name);
txt.setTextColor(Color.GRAY);
break;
case 4 :
txt.setText("Esti destept "+ name);
txt.setTextColor(Color.WHITE);
break;
case 5 :
txt.setText("Esti la moda, "+ name);
txt.setTextColor(Color.LTGRAY);
break;
}
}else {
txt.setText("Te rog, alege baiat sau fata!");
}
}
});
}
}
和清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.create.namestrings"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.create.namestrings.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请帮助!!!
答案 0 :(得分:1)
尝试:
//Rest is the same
EditText etName;
RadioButton RbtnGirl;
RadioButton RbtnBoy;
Button btn;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById (R.id.editText1);
RbtnGirl = (RadioButton) findViewById (R.id.radioButton1);
RbtnBoy = (RadioButton) findViewById (R.id.radioButton2);
btn = (Button) findViewById (R.id.button1);
txt = (TextView) findViewById (R.id.textView2);
//Rest is the same
而不是你的代码。
在NullPointerException
中使用btn
时,您获得了onCreate()
。这是因为findViewById()
在当前膨胀的布局中查找视图,该布局在调用setContentView()
之前不存在。当您在方法之外初始化视图时,它们都会获得空值,因为调用onCreate()
的{{1}}尚未被调用。
答案 1 :(得分:0)
设置布局内容后,您会找到ID
private EditText etName;
private RadioButton RbtnGirl;
private RadioButton RbtnBoy;
private Button btn;
private TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById (R.id.editText1);
RbtnGirl = (RadioButton) findViewById (R.id.radioButton1);
RbtnBoy = (RadioButton) findViewById (R.id.radioButton2);
btn = (Button) findViewById (R.id.button1);
txt = (TextView) findViewById (R.id.textView2);
......
在将视图设置为布局之前,您找到了id。因此你得到了NullPointerException。
我更新了我的答案,如果按照以下方式执行操作,则必须将文本视图设为最终,因为您将在按钮单击时使用textview。这是不必要的,因此将变量声明为类变量。
setContentView(R.layout.activity_main);
final Textview txt = (TextView) findViewById (R.id.textView2);