1

我对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>

由于受欢迎的需求,这里是我的 Adventure 课程中的 java 代码。第 15 行是按钮 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");
        }

    }


}
4

3 回答 3

2

无论您正在设置什么视图都返回null您是否仔细检查了 ID 是否正确并且setContentView(R.layout.xxx)指向正确的文件?

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

编辑

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

于 2012-05-29T09:35:30.243 回答
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);

与设置布局之前一样,您不能初始化作为该布局一部分的按钮。

于 2012-05-29T09:51:23.517 回答
0
Button chkCmd = (Button) findViewById(R.id.bResults);

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

setContentView(R.layout.main);
chkCmd = (Button) findViewById(R.id.bResults);
于 2012-05-29T09:53:59.907 回答