0

我试图创建一个新活动..

我真的不知道我该如何解决这个问题..

在 Android Studio 上它根本没有显示任何错误,但是当我尝试运行它时 LogCat 显示如下:

10-19 21:21:34.427  10423-10423/? E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iklikla.eightgame/com.iklikla.eightgame.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2330)
    at android.app.ActivityThread.access$700(ActivityThread.java:154)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5368)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1041)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.iklikla.eightgame.MainActivity.onCreate(MainActivity.java:31)
    at android.app.Activity.performCreate(Activity.java:5284)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2244)
    ... 11 more

这是我的 MainActivity.class:

package com.iklikla.eightgame;

    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.widget.Button;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button infos = (Button) findViewById(R.string.infos);
    infos.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent toAnotherActivity = new Intent(v.getContext(), InfoActivity.class);
            startActivityForResult(toAnotherActivity, 0);
        }
    });
}
}

还有我的 InfoActivity.class:

package com.iklikla.eightgame;

import android.os.Bundle;
import android.app.Activity;

public class InfoActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_infos);
}
}
4

1 回答 1

1
Button infos = (Button) findViewById(R.string.infos);

R.string.info 是资源中字符串的 id。

你不是说 R. id .infos 吗?

于 2013-10-19T20:09:50.937 回答