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

时间:2009-10-25 17:40:54

标签: 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>

根据建议,我检查了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

6 个答案:

答案 0 :(得分:4)

这是在手机上还是在模拟器中?我看的第一个地方是日志。在Eclipse中转到Window&gt;显示视图&gt;其他...&gt; Android&gt; logcat中。

您可以通过

在代码中添加自己的调试日志消息
Log.d("Debug", "Informative Log Message.");

如果您可以在此处发布代码,也会有所帮助。发布展示错误行为的最小工作代码示例。

答案 1 :(得分:3)

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

答案 2 :(得分:2)

当我弄乱布局文件时,通常会发生这种情况。尝试在Eclipse中打开主布局文件,看看它是否仍然可以呈现它。如果它不能,那可能是你的麻烦的原因。如果你愿意,可以在这里发布,也许我们可以帮助你。

答案 3 :(得分:2)

尝试运行
adb logcat
adb可以在android sdk的tools目录中找到,它会在应用程序加载期间显示异常,这些异常是无法捕获的。

答案 4 :(得分:0)

您是否在res / values文件夹中创建了strings.xml文件?您需要声明正在使用的字符串:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Test App</string>
    <string name="number">Number</string>
    <string name="calculate">Calculate</string>
</resources>

我建议使用Eclipse而不是Netbeans。然后,您可以使用ADT插件创建布局,以帮助您避免出现问题。

答案 5 :(得分:0)

这样做的一个相当不错的方法是简单地添加一个普通的编辑文本,然后添加:

        android:background="@null"
        android:editable="false"
        android:cursorVisible="false"

对此 然后只需在课堂上设置编辑文本的文本就可以了。

相关问题