这是我用来启动活动的代码:
startActivity(new Intent(getApplicationContext(), Giveaway.class));
这是我提出的活动:
public class Giveaway extends Activity implements OnClickListener{
static SharedPreferences settings;
SharedPreferences.Editor prefEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.giveaway);
settings = getSharedPreferences("firsttime", 0);
LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
facebook.setOnClickListener(this);
later.setOnClickListener(this);
dontshowagain.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.facebooklayout:
Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");
startActivity(new Intent("android.intent.action.VIEW", localuri));
break;
case R.id.later:
finish();
break;
case R.id.dontshowagain:
finish();
prefEditor = settings.edit();
prefEditor.putBoolean("showgiveaway", false);
prefEditor.commit();
break;
}
}
我已经在清单文件夹中声明了 Activity:
<activity
android:name=".Giveaway"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog"
android:screenOrientation="portrait"/>
但我不断收到 java.lang.RuntimeException: Unable to start activity java.lang.NullPointerException 错误。这是我的日志:
07-24 12:43:59.082: E/AndroidRuntime(7039): FATAL EXCEPTION: main
07-24 12:43:59.082: E/AndroidRuntime(7039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brightdesign.blackops2/com.brightdesign.blackops2.Giveaway}: java.lang.NullPointerException
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.os.Looper.loop(Looper.java:123)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-24 12:43:59.082: E/AndroidRuntime(7039): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 12:43:59.082: E/AndroidRuntime(7039): at java.lang.reflect.Method.invoke(Method.java:521)
07-24 12:43:59.082: E/AndroidRuntime(7039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-24 12:43:59.082: E/AndroidRuntime(7039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-24 12:43:59.082: E/AndroidRuntime(7039): at dalvik.system.NativeStart.main(Native Method)
07-24 12:43:59.082: E/AndroidRuntime(7039): Caused by: java.lang.NullPointerException
07-24 12:43:59.082: E/AndroidRuntime(7039): at com.brightdesign.blackops2.Giveaway.onCreate(Giveaway.java:29)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-24 12:43:59.082: E/AndroidRuntime(7039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)