简体   繁体   English

Android错误:无法启动活动

[英]Android Error: Unable To Start Activity

Here is the code I use to bring up the activity: 这是我用来启动活动的代码:

startActivity(new Intent(getApplicationContext(), Giveaway.class)); 

Here is the Activity that I am bringing up: 这是我要提出的活动:

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;
    }
}

I have declared the Activity in my manifest folder: 我在清单文件夹中声明了活动:

<activity
        android:name=".Giveaway"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Dialog"
        android:screenOrientation="portrait"/>

But I keep getting a java.lang.RuntimeException: Unable to start activity java.lang.NullPointerException error. 但是我一直收到java.lang.RuntimeException:无法启动活动java.lang.NullPointerException错误。 Here is my 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)

You need to post your layout code, but what is most likely happening is that one of these three lines is returning a null value: 您需要发布布局代码,但是最有可能发生的是这三行之一返回空值:

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

In the debugger step through those lines and if one is null, there is your problem because as soon as you try to set the on click listener it is going to fail. 在调试器中逐步浏览这些行,如果其中一个为null,则存在问题,因为一旦您尝试设置点击监听器,它就会失败。

Caused by: java.lang.NullPointerException
     at com.brightdesign.blackops2.Giveaway.onCreate(Giveaway.java:29)

Tells us that there is a NullPointerException in Giveaway.onCreate(), specifically Giveaway.java line 29. Odds are facebook , later , and/or dontshowagain is really null. 告诉我们,Giveaway.onCreate()中有一个NullPointerException,特别是Giveaway.java第29行。赔率是facebooklater和/或dontshowagain是null。 Do you have all three of these defined in giveaway.xml? 您是否在Giveaway.xml中定义了所有这三个?

Try this... 尝试这个...

1. 1。

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

2. This below lines points to the class and the lines which are null. 2.下面的几行指向类和为空的行。

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

Lines: Please check the below four lines, i think you are getting null value here. 行:请检查以下四行,我认为您在这里获得空值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM