android意图上的nullpointer

时间:2013-06-14 08:42:27

标签: android android-intent nullpointerexception

不知何故,当我尝试使用startActivity执行我的意图时,我得到一个nullpointer。唯一的问题是我在不同的班级中有相同的陈述。并且没有任何问题正确执行。这是导致错误的代码段:

public class GetPreferences extends Activity {

String result, Gebruikersnaam, Wachtwoord, LicentieCode, DeviceId, GebGUID;
FeatureSettings settings;
StringEntity seParams;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.frmlogin);
    Intent startApp = new Intent(this, actMain.class);
    startApp.putExtra("afterLogin", 1);
    startActivity(startApp);
}

这是错误日志:

06-14 09:51:41.596: E/AndroidRuntime(28368): FATAL EXCEPTION: main
06-14 09:51:41.596: E/AndroidRuntime(28368): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.koeriers.tools/com.koeriers.erasmus.actMain}: java.lang.NullPointerException
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.app.ActivityThread.access$700(ActivityThread.java:151)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.os.Looper.loop(Looper.java:137)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.app.ActivityThread.main(ActivityThread.java:5293)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at java.lang.reflect.Method.invoke(Method.java:511)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at dalvik.system.NativeStart.main(Native Method)
06-14 09:51:41.596: E/AndroidRuntime(28368): Caused by: java.lang.NullPointerException
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at com.koeriers.erasmus.actMain.<init>(actMain.java:76)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at java.lang.Class.newInstanceImpl(Native Method)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at java.lang.Class.newInstance(Class.java:1319)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
06-14 09:51:41.596: E/AndroidRuntime(28368):    ... 11 more

这是我的清单中类的定义:

<activity
        android:name="com.koeriers.erasmus.actMain"
        android:label="@string/app_name" >
    </activity>

我知道我的actMain类甚至没有加载,因为我添加了一个Log.e语句。而这甚至没有被执行。所以它必须是startActivity部分,但我只是看不出有什么问题。所以我希望你们能帮助我。

提前致谢。

@edit

这是我的actMain.class

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TLogFile.appendLog("i", "ACTMAIN", "OnCreate Started", true);

    SetDefaultUncaughtExceptionHandler();

    settings = new FeatureSettings(getApplicationContext());
    TTools.DisableTitleBar(this);
    setContentView(R.layout.frmmain);
    TLogFile.appendLog("i", "actMain", "onCreate");

    pbMain = (ProgressBar) findViewById(R.id.pbMain);

    HeaderIcon = (ImageView)findViewById(R.id.HeaderIcon);
    HeaderIcon.setBackgroundColor(Color.parseColor(configfeatures.HeaderIconColor));

    lblHeader = (TextView) findViewById(R.id.lblHeader);
    lblHeader.setText("Order overzicht");
    lblHeader.setBackgroundColor(Color.parseColor(configfeatures.HeaderStateColor));

    linear = (LinearLayout)findViewById(R.id.llMain);
    linear.setBackgroundColor(Color.parseColor(configfeatures.BackgroundColor));

    if (configfeatures.Foto) {
        btnPicture = (ImageButton) findViewById(R.id.btnPicture);
        btnPicture.setVisibility(View.VISIBLE);
        btnPicture.setOnClickListener(this);
    } else {
        btnPicture = (ImageButton) findViewById(R.id.btnPicture);
        btnPicture.setVisibility(View.GONE);
    }

    btnLogout = (ImageButton) findViewById(R.id.btnLogout);
    btnLogout.setOnClickListener(this);

    btnTSync = (ImageButton) findViewById(R.id.btnTSync);
    btnTSync.setOnClickListener(this);

    Boolean bJustLoggedIn = false;
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        bJustLoggedIn = extras.getInt("afterLogin") == 1;
    } else {
        // do nothing
    }

    if (bJustLoggedIn) {
        StartSyncing();
    }

    if (configfeatures.LocatieGegevens){
        StartLocationService();
    }

    // kijken of er een register actie is geweest
    if ((this.getIntent().getExtras() != null)
            && (this.getIntent().getExtras().getString("pushType") != null)
            && (this.getIntent().getExtras().getString("pushType").contains("1"))) {
        TPushHandler pushHandler = new TPushHandler();
        pushHandler.CheckPushMessages(this.getIntent(), this);
    }

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    if(configfeatures.LocatieGegevens){
        StartLocationThread();
    }

2 个答案:

答案 0 :(得分:2)

根据StackTrace,你的actMain类第76行有一个NPE。 <init>提示异常在创建对象时引发,因此在调用onCreate()方法之前。可能在构造函数中或初始化全局变量。

Caused by: java.lang.NullPointerException
06-14 09:51:41.596: E/AndroidRuntime(28368):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
06-14 09:51:41.596: E/AndroidRuntime(28368):    at com.koeriers.erasmus.actMain.<init>(actMain.java:76)

答案 1 :(得分:0)

您在getApplicationContext的初始化中呼叫actMain。 (在构造函数或成员声明中)。

你做不到。 上下文在调用onCreate之前未初始化。将通话移至getApplicationContextonCreate()