Android错误:无法启动活动

时间:2012-07-24 16:51:54

标签: java android eclipse error-handling nullpointerexception

以下是我用来启动活动的代码:

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
        android:name=".Giveaway"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Dialog"
        android:screenOrientation="portrait"/>

但是我一直得到一个java.lang.RuntimeException:无法启动活动java.lang.NullPointerException错误。这是我的logcat:

    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)

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分: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中定义了所有这三个?

答案 2 :(得分: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);