0

Somehow I am getting an nullpointer when trying to execute my intent using startActivity. The only problem is that I am having the same statement in a different class. And that one is executed correctly without any problem. This is the piece of code which is causing the error:

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

this is the error log:

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

and here is the defintion of the class in my manifest:

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

I know that my actMain class isn't even being loaded, because I've added a Log.e statement. And that isn't even being executed. So it must be the startActivity part, but I just can't see what is going wrong. So I hope you guys can help me out.

Thanks in advance.

@edit

here is my 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();
    }
4

2 回答 2

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)
于 2013-06-14T08:56:16.940 回答
0

getApplicationContext调用actMain. (在构造函数或成员声明中)。

你不能这样做。调用未初始化的上下文。onCreate将呼叫getApplicationContext移至onCreate()

于 2013-06-14T08:57:17.987 回答