2013-10-19 112 views
0

我試圖創建一個新的活動..無法啓動活動 - NullPointExeption

我真的不知道我可以解決這個問題..

在Android工作室它顯示根本沒有錯誤,但是當我嘗試運行它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); 
} 
} 
+0

'在Android Studio上它根本沒有顯示任何錯誤,但是當我嘗試運行它時,LogCat顯示如下。編譯器中的錯誤並不意味着在運行應用程序時沒有錯誤。如果確實如此,那麼沒有應用程序會崩潰! '引發:java.lang.NullPointerException at com.iklikla.eightgame.MainActivity.onCreate(MainActivity.java:31)' - 這顯示了你到底發生了什麼錯誤以及發生了什麼。 onCreate(),第31行。 – Simon

回答

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

R.string.info是你的資源字符串的ID。

難道你的意思是R. ID .infos?

+0

是的,我確實謝謝你! –