Android:Intent setClass导致NullPointer异常

时间:2011-12-08 02:46:50

标签: android android-intent nullpointerexception

我想使用来自TcpClient的Intent转到PreCamera。这导致java.lang.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)

我看到有人说这是因为我初始化mContext太早了,它应该是在OnCreate()方法之后的init或更高版本。但是这个方法后如何初始化?我是Android Dev的初学者。

TcpClient是扩展Activity。我用:

mContext = this;
Intent intent = new Intent(TcpClient.this, PreCamera.class); 

这不是很好。

3 个答案:

答案 0 :(得分:0)

为什么不试试这个......

Intent intent = new Intent(TcpClient.this, PreCamera.class);


this.mContext.startActivity(intent);

答案 1 :(得分:0)

如果要从TcpClient活动启动PreCamera活动: 试试这个

TcpClient.this.startActivity(intent);

答案 2 :(得分:0)

编辑:已更改,因为TcpClient扩展了Activity:

简单地说:

Intent intent = new Intent(this, PreCamera.class);
startActivity(intent);

你的其他方式可行,只需要更多代码:

Intent intent = new Intent();
intent.setClass(this, PreCamera.class);
startActivity(intent);