我想從TcpClient使用Intent轉到PreCamera。它會導致java.lang.NullPointer異常。Android:Intent setClass導致NullPointer異常
Intent intent = new Intent();
Log.v("Pic", "context");
intent.setClass(TcpClient.this, PreCamera.class);
Log.v("Pic", "ok");
this.mContext.startActivity(intent);
這是TcpClient的構建體:
public TcpClient(Context context) {
mContext = context;
}
這是異常信息:
12-08 10:56:53.515: V/Pic(2579): context
12-08 10:56:53.519: W/dalvikvm(2579): threadid=13: thread exiting with uncaught exception (group=0x40020ac0)
12-08 10:56:53.519: E/AndroidRuntime(2579): FATAL EXCEPTION: Thread-14
12-08 10:56:53.519: E/AndroidRuntime(2579): java.lang.NullPointerException
12-08 10:56:53.519: E/AndroidRuntime(2579): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
12-08 10:56:53.519: E/AndroidRuntime(2579): at android.content.ComponentName.<init>(ComponentName.java:75)
12-08 10:56:53.519: E/AndroidRuntime(2579): at android.content.Intent.setClass(Intent.java:4920)
12-08 10:56:53.519: E/AndroidRuntime(2579): at com.Image.Collect.TcpClient.takePic(TcpClient.java:330)
12-08 10:56:53.519: E/AndroidRuntime(2579): at com.Image.Collect.TcpClient.commandExecute(TcpClient.java:467)
12-08 10:56:53.519: E/AndroidRuntime(2579): at com.Image.Collect.TcpClient.receive(TcpClient.java:278)
12-08 10:56:53.519: E/AndroidRuntime(2579): at com.Image.Collect.TcpClient.talk(TcpClient.java:179)
12-08 10:56:53.519: E/AndroidRuntime(2579): at com.Image.Collect.InfoCollect$6.run(InfoCollect.java:303)
12-08 10:56:53.539: E/InfoCollect(2579): onPause
這是一些AndroidMainfest.xml信息:
<activity android:name=".TcpClient"
... (other info)
</activity>
<activity android:name=".PreCamera"
... (other info)
</activity>
API是級別8.
當我試試這個:
Intent intent = new Intent(TcpClient.this, PreCamera.class);
它會導致:
12-08 11:31:45.221: W/dalvikvm(2805): threadid=13: thread exiting with uncaught exception (group=0x40020ac0)
12-08 11:31:45.229: E/AndroidRuntime(2805): FATAL EXCEPTION: Thread-14
12-08 11:31:45.229: E/AndroidRuntime(2805): java.lang.NullPointerException
12-08 11:31:45.229: E/AndroidRuntime(2805): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
12-08 11:31:45.229: E/AndroidRuntime(2805): at android.content.ComponentName.<init>(ComponentName.java:75)
我看到有人說這是因爲我的init mContext是太早了,應該以後onCreate()方法來初始化或後來。但是如何在這個方法之後初始化?我是Android Dev的初學者。
TcpClient是extends Activity。我用:
mContext = this;
Intent intent = new Intent(TcpClient.this, PreCamera.class);
它不是很好。
TcpClient的是擴展活動 – pthread
好吧,我已經編輯我的反應。在那種情況下,它更容易。 –