Android 应用程序在打开时关闭
大家好,我是应用程序新手,对此很抱歉:D 这个应用程序的想法是创建一个包含值的列表,选择一个随机值,从而选择一个列表项。您可以将列表保存到文本文件中。问题是,每当我打开我的应用程序时,它都会强制关闭。我不知道发生了什么,但确实有一个日志猫:Android 应用程序在打开时关闭,android,Android,大家好,我是应用程序新手,对此很抱歉:D 这个应用程序的想法是创建一个包含值的列表,选择一个随机值,从而选择一个列表项。您可以将列表保存到文本文件中。问题是,每当我打开我的应用程序时,它都会强制关闭。我不知道发生了什么,但确实有一个日志猫: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.frostbytedev.randomgenie/com.frostbytedev.randomgenie.M
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.frostbytedev.randomgenie/com.frostbytedev.randomgenie.Menu}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.frostbytedev.randomgenie.Menu.onCreate(Menu.java:19)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
这是我的Java:
package com.frostbytedev.randomgenie;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity implements View.OnClickListener{
Button OpenList, NewList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
OpenList=(Button)findViewById(R.id.bOpenList);
NewList=(Button)findViewById(R.id.bNewList);
OpenList.setOnClickListener(this);
NewList.setOnClickListener(this);
}
@Override
public void onClick(View view) {
}
}
和XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:weightSum="100"
tools:context=".Menu">
<Button
android:layout_weight="50"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New List"
android:id="@+id/button"
android:layout_below="@+id/bNewList"
android:layout_centerHorizontal="true"/>
<Button
android:layout_weight="50"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open a List"
android:id="@+id/bOpenList"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
</LinearLayout>
在XML中,将第一个按钮的ID设置为@+ID/bNewList。您的onCreate找不到该ID,因为您的第一个按钮的ID设置为“button”。以上答案应能解决此问题
但是为了将来的参考,LogCat对于调试来说是非常好的
如果你双击这一行
at com.frostbytedev.randomgenie.Menu.onCreate(Menu.java:19)
在LogCat中,它将引导您找到出现问题的代码行,这是菜单类中onCreate函数在第19行的NullPointerException
换句话说,NullPointerException意味着您设置的值为NULL,并且不能为NULL