0

这是我用来启动活动的代码:

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)
4

3 回答 3

3

您需要发布您的布局代码,但最有可能发生的是这三行之一正在返回一个空值:

LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
Button later = (Button)findViewById(R.id.later);
Button dontshowagain = (Button)findViewById(R.id.dontshowagain);

在调试器中逐步执行这些行,如果其中一个为空,则存在您的问题,因为一旦您尝试设置 on click 侦听器,它就会失败。

于 2012-07-24T16:56:09.400 回答
0
Caused by: java.lang.NullPointerException
     at com.brightdesign.blackops2.Giveaway.onCreate(Giveaway.java:29)

告诉我们 Giveaway.onCreate() 中存在 NullPointerException,特别是 Giveaway.java 第 29 行。赔率是facebooklater和/或dontshowagain确实为空。您是否在 giveaway.xml 中定义了所有这三个?

于 2012-07-24T16:56:46.747 回答
0

试试这个...

1.

Intent i = new Intent(Your_Activity.this, Another_Activity.class);
startActivity(i);

2.下面这行指向类和空行。

类: com.brightdesign.blackops2/com.brightdesign.blackops2.Giveaway}:java.lang.NullPointerException

行:请检查以下四行,我认为您在这里得到空值。

Uri localuri = Uri.parse("http://www.facebook.com/pages/Bright-Design/366832480049386");



 LinearLayout facebook = (LinearLayout)findViewById(R.id.facebooklayout);
    Button later = (Button)findViewById(R.id.later);
    Button dontshowagain = (Button)findViewById(R.id.dontshowagain);
于 2012-07-24T16:59:33.657 回答