Android:应用程序在打开后立即关闭

Dra*_*cir 5 android

我正在修补Netbeans上的android并做了一个简单的数学应用程序.在某个地方,应用程序突然强制关闭它一旦打开(在模拟器上)...我试着评论承载主要活动的类中的所有代码,但无济于事.由于它没有给我一个特定的错误信息,我觉得有点丢失.

出现这种情况的原因是什么?

这是请求的布局文件:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView  android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""/>
        <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/number" />
        <EditText android:id="@+id/n"
          android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                        android:numeric="decimal"/>
                <Button android:id="@+id/calc"
                  android:text="@string/calculate"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

建议我检查logcat,这是错误列表

E/AndroidRuntime(  881): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  881): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.me.mathdroid/org.me.mathdroid.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(  881):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
E/AndroidRuntime(  881):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime(  881):        at android.app.ActivityThread.access$2100(ActivityThread.java:116)
E/AndroidRuntime(  881):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
E/AndroidRuntime(  881):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  881):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  881):        at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime(  881):        at java.lang.reflect.Method.invokeNative(NativeMethod)
E/AndroidRuntime(  881):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  881):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime(  881):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime(  881):        at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(  881): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  881):        at android.app.Activity.findViewById(Activity.java:1610)
E/AndroidRuntime(  881):        at org.me.mathdroid.MainActivity.<init>(MainActivity.java:22)
E/AndroidRuntime(  881):        at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(  881):        at java.lang.Class.newInstance(Class.java:1472)
E/AndroidRuntime(  881):        at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
E/AndroidRuntime(  881):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
E/AndroidRuntime(  881):        ... 11 more
Run Code Online (Sandbox Code Playgroud)

Dra*_*cir 3

我已经弄清楚问题是什么,在主要活动中,我在设置 contentView 之前使用 R 类中的对象设置 Edittext 和 TextView 对象。抱歉,添麻烦了。

  • 我遇到了类似的问题,您可以将工作代码粘贴到这里吗?我不太明白你的意思。 (7认同)