0

I am implementing a function that call an intent from my Extra.class. Instead of doing inter = new intent(this, class); several time i want a custom one as shown below. but this produce an error log when i call for example Extra.toClass(context, Act.class);

import android.content.Context; import android.content.Intent;

public class Extra extends Activity {

    private static Context context;
    private static Class<?> activity;
    private static Intent intx;

    public static void toClass(Context text,Class<?> a){

        text = context;
        a = activity;

        intx = new Intent(text,a);
        context.startActivity(intx);

    }
}

this is my log

05-31 09:56:26.128: E/AndroidRuntime(1545): FATAL EXCEPTION: main
05-31 09:56:26.128: E/AndroidRuntime(1545): java.lang.RuntimeException: Unable to start activity ComponentInfo{.app/.app.CompteCompagnon}: java.lang.NullPointerException
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.os.Looper.loop(Looper.java:130)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at java.lang.reflect.Method.invoke(Method.java:507)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at dalvik.system.NativeStart.main(Native Method)
05-31 09:56:26.128: E/AndroidRuntime(1545): Caused by: java.lang.NullPointerException
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.content.ComponentName.<init>(ComponentName.java:75)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.content.Intent.<init>(Intent.java:2863)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at .app.util.extra.Extra.toClass(Extra.java:18)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at .app.CompteCompagnon.onCreate(CompteCompagnon.java:44)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-31 09:56:26.128: E/AndroidRuntime(1545):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-31 09:56:26.128: E/AndroidRuntime(1545):     ... 11 more
05-31 09:56:26.132: E/(136): Dumpstate > /data/log/dumpstate_app_error
05-31 09:56:32.742: E/(279): onResume() check 0
05-31 09:56:32.742: E/(279): onResume() check 1
05-31 09:56:32.742: E/Launcher(279): setWindowOpaque()
05-31 09:56:32.796: E/(279): onResume() check 2, mRestoring : false
05-31 09:56:32.800: E/(279): onResume() check 3
05-31 09:56:32.800: E/(279): onResume() check 4
05-31 09:56:32.800: E/(279): onResume() check 5
4

3 回答 3

1

请删除静态变量。它们会导致内存泄漏。

public class Extra extends Activity {

    public static void toClass(Context text,Class<?> a){

        Intent intx = new Intent(text,a);
        text.startActivity(intx);

    }
}

无论如何,您的问题是您以错误的方式分配它们。

于 2013-05-31T06:04:00.713 回答
1

从日志中可以看到调用时抛出了NPE intx = new Intent(text,a);。所以可能是text或者anull。原因是text = context将未初始化的属性分配给context参数text。你想要它的另一种方式。将其更改为context = text(和activity = a)。

于 2013-05-31T06:04:23.243 回答
1

试试看:

public static void toClass(Context text,Class<?> a){
        this.context=text;
        this.activity=a;
        intx = new Intent(text,a);
        context.startActivity(intx);
    }

因为您没有将 toClass方法参数值分配给类变量

于 2013-05-31T06:00:59.917 回答