Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在eclipse emulator上运行应用程序时出错_Java_Android_Eclipse_Emulation - Fatal编程技术网

Java 在eclipse emulator上运行应用程序时出错

Java 在eclipse emulator上运行应用程序时出错,java,android,eclipse,emulation,Java,Android,Eclipse,Emulation,我制作了一个android应用程序,每次我在Emulator中的Eclipse上运行它时,它都会停止。连我都没有错!这是一个非常简单的应用程序。。。但是在模拟器中它崩溃了。我不知道为什么会发生这种情况。在emulator中出现“进程意外停止”或类似的内容 以下是日志: 05-05 09:26:58.699: D/AndroidRuntime(1222): Shutting down VM 05-05 09:26:58.749: W/dalvikvm(1222): threadid=1: thre

我制作了一个android应用程序,每次我在Emulator中的Eclipse上运行它时,它都会停止。连我都没有错!这是一个非常简单的应用程序。。。但是在模拟器中它崩溃了。我不知道为什么会发生这种情况。在emulator中出现“进程意外停止”或类似的内容

以下是日志:

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)
以及舱单:

<?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>

请帮忙

试试看:

//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
而不是你的代码


onCreate()
中使用
btn
时,您收到了
NullPointerException
。这是因为
findViewById()
在当前扩展的布局中查找视图,该视图在调用
setContentView()
之前不存在。当您在方法之外初始化视图时,它们都会得到一个空值,因为调用
setContentView()
onCreate()
尚未被调用。

设置布局内容后,您会发现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

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

    setContentView(R.layout.activity_main);
    final Textview txt = (TextView) findViewById (R.id.textView2); 
    setContentView(R.layout.activity_main);
    final Textview txt = (TextView) findViewById (R.id.textView2);