我制作了一个 android 应用程序,每次我在模拟器中的 Eclipse 上运行它时它都会停止。即使我没有错误!这是一个非常简单的应用程序......但在模拟器中它崩溃了。我不知道为什么会发生这种情况。在模拟器中出现“进程意外停止”或类似的东西......
这是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>
请帮忙!!!