大家好,我是应用程序的新手,对此感到抱歉:D
这个应用程序的想法是创建一个带有值的列表,选择一个随机值,从而选择一个列表项。您可以将列表保存到文本文件中。问题是,每当我打开我的应用程序时,它就会强制关闭。我不知道发生了什么,但确实有一个日志猫:
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>