-1

我制作了一个 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>

请帮忙!!!

4

2 回答 2

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你在使用btnin时得到了一个onCreate()。这是因为在当前膨胀的布局中查找视图,该布局在被调用findViewById()之前不存在。setContentView()当您在方法之外初始化您的视图时,它们都得到一个空值onCreate(),因为调用setContentView()的 尚未被调用。

于 2013-05-05T10:07:56.347 回答
0

设置布局内容后,找到ids

   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。

我更新了我的答案,如果按以下方式进行操作,则必须将文本视图设为最终视图,因为您将在单击按钮时使用文本视图。这是不必要的,因此将变量声明为类变量。

    setContentView(R.layout.activity_main);
    final Textview txt = (TextView) findViewById (R.id.textView2); 
于 2013-05-05T10:08:05.860 回答