2013-05-31 57 views
0

我正在實現一個從我的Extra.class中調用intent的函數。而不是做inter = new intent(這個,class);幾次我想要一個自定義的如下所示。但是當我調用例如Extra.toClass(context,Act.class)時,這會產生一個錯誤日誌;來自Extra.class的內容功能android

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

    } 
} 

這是我的日誌

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 

回答

1

請刪除靜態變量。他們會造成內存泄漏。

public class Extra extends Activity { 

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

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

    } 
} 

無論如何,你的問題是你是以錯誤的方式分配他們。

1

嘗試,因爲:

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

,因爲你不分配toClass方法的參數值類變量

1

從日誌中,您可以看到在致電intx = new Intent(text,a);時引發了NPE。所以可能是textanull。原因是text = context將未初始化的屬性context分配給參數text。你想以另一種方式。將其更改爲context = text(和activity = a)。