Android应用程序关闭创建?

时间:2012-05-29 09:31:51

标签: android fatal-error

我对android很新,应用程序在创建后立即关闭。任何帮助将不胜感激!

关于应用程序的一点点,我正在创建基于文本的冒险,我已经将所有代码编码到java中,并且我知道java代码有效,因为我已经创建了JavaGui并且正在为它工作。我现在正试图将这个java游戏带到android。

这是我的错误日志

05-29 09:19:01.989: E/AndroidRuntime(539): FATAL EXCEPTION: main
05-29 09:19:01.989: E/AndroidRuntime(539): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.coalbolttproductions.russell/com.coalbolttproductions.russell.AdventureToCandyIslandActivity}: java.lang.NullPointerException
05-29 09:19:01.989: E/AndroidRuntime(539):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
05-29 09:19:01.989: E/AndroidRuntime(539):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-29 09:19:01.989: E/AndroidRuntime(539):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-29 09:19:01.989: E/AndroidRuntime(539):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-29 09:19:01.989: E/AndroidRuntime(539):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 09:19:01.989: E/AndroidRuntime(539):  at android.os.Looper.loop(Looper.java:137)
05-29 09:19:01.989: E/AndroidRuntime(539):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-29 09:19:01.989: E/AndroidRuntime(539):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 09:19:01.989: E/AndroidRuntime(539):  at java.lang.reflect.Method.invoke(Method.java:511)
05-29 09:19:01.989: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-29 09:19:01.989: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-29 09:19:01.989: E/AndroidRuntime(539):  at dalvik.system.NativeStart.main(Native Method)
05-29 09:19:01.989: E/AndroidRuntime(539): Caused by: java.lang.NullPointerException
05-29 09:19:01.989: E/AndroidRuntime(539):  at android.app.Activity.findViewById(Activity.java:1794)
05-29 09:19:01.989: E/AndroidRuntime(539):  at com.coalbolttproductions.russell.AdventureToCandyIslandActivity.<init>(AdventureToCandyIslandActivity.java:15)
05-29 09:19:01.989: E/AndroidRuntime(539):  at java.lang.Class.newInstanceImpl(Native Method)
05-29 09:19:01.989: E/AndroidRuntime(539):  at java.lang.Class.newInstance(Class.java:1319)
05-29 09:19:01.989: E/AndroidRuntime(539):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-29 09:19:01.989: E/AndroidRuntime(539):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
05-29 09:19:01.989: E/AndroidRuntime(539):  ... 11 more

这是我的main.xml,我认为是我的问题的真正原因

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:padding="25dp">

    <EditText
        android:id="@+id/etCommands"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Type a Command"
        android:password="true" />

    <Button
        android:id="@+id/bResults"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Try Command" />

    <TextView
        android:id="@+id/tvResults"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TextView" />

</LinearLayout>

由于受欢迎的需求,这里是我冒险类的java代码。 第15行是Button chkCmd

public class AdventureToCandyIslandActivity extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */

    Button chkCmd = (Button) findViewById(R.id.bResults);
    EditText input;
    TextView display;

    Game gameModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Run the methods
        createVars();
        chkCmd.setOnClickListener(this);

    }

    private void createVars(){
        gameModel = new Game();
        input = (EditText) findViewById(R.id.etCommands);
        display = (TextView) findViewById(R.id.tvResults);
        display.setText(gameModel.printWelcome());
        display.setGravity(Gravity.LEFT);
        display.setTextColor(Color.WHITE);

    }

    public void onClick(View v) {
        String inputLine = input.getText().toString();
        Command command = Command.getCommand(inputLine);
        String result = gameModel.processCommand(command);
        if(result.equals("EXIT")) {
            System.exit(0);
        } else {
            display.append("--------------------------------\n" + result + "\n");
        }

    }


}

3 个答案:

答案 0 :(得分:2)

您正在设置的任何视图是返回null您是否重复检查过ID是否正确且setContentView(R.layout.xxx)指向正确的文件?

此外,如果您为纵向和横向定义了不同的布局,如果视图的ID不匹配,则会导致类似的问题.AdventureToCandyIslandActivity.java见第15行

修改

Button chkCmd = (Button) findViewById(R.id.bResults); 你已经在类加载器中初始化了按钮,如果你在与其他视图相同的位置初始化它就可以了。

答案 1 :(得分:0)

更改

Button chkCmd = (Button) findViewById(R.id.bResults);
EditText input;
TextView display;

Game gameModel;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

至此

Button chkCmd;
EditText input;
TextView display;

Game gameModel;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    chkCmd = (Button) findViewById(R.id.bResults);

在设置布局之前,您无法初始化作为该布局一部分的按钮。

答案 2 :(得分:0)

Button chkCmd = (Button) findViewById(R.id.bResults);

您在设置ContentView之前使用了 findViewById(R.id.bResults),因此请在删除后使用以下代码:

setContentView(R.layout.main);
chkCmd = (Button) findViewById(R.id.bResults);