Handler handler=new Handler(MainActivity.this.getMainLooper()){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Object s;
if ((s=msg.obj)!=null) {
fill(s.toString());
}
}
};
12-04 06:53:07.007: E/AndroidRuntime(1346): FATAL EXCEPTION: main
12-04 06:53:07.007: E/AndroidRuntime(1346): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.webtest/com.example.webtest.MainActivity}: java.lang.NullPointerException
12-04 06:53:07.007: E/AndroidRuntime(1346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
12-04 06:53:07.007: E/AndroidRuntime(1346): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-04 06:53:07.007: E/AndroidRuntime(1346): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-04 06:53:07.007: E/AndroidRuntime(1346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-04 06:53:07.007: E/AndroidRuntime(1346): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 06:53:07.007: E/AndroidRuntime(1346): at android.os.Looper.loop(Looper.java:137)
12-04 06:53:07.007: E/AndroidRuntime(1346): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-04 06:53:07.007: E/AndroidRuntime(1346): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 06:53:07.007: E/AndroidRuntime(1346): at java.lang.reflect.Method.invoke(Method.java:511)
12-04 06:53:07.007: E/AndroidRuntime(1346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-04 06:53:07.007: E/AndroidRuntime(1346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-04 06:53:07.007: E/AndroidRuntime(1346): at dalvik.system.NativeStart.main(Native Method)
12-04 06:53:07.007: E/AndroidRuntime(1346): Caused by: java.lang.NullPointerException
12-04 06:53:07.007: E/AndroidRuntime(1346): at android.content.ContextWrapper.getMainLooper(ContextWrapper.java:96)
12-04 06:53:07.007: E/AndroidRuntime(1346): at com.example.webtest.MainActivity.<init>(MainActivity.java:71)
12-04 06:53:07.007: E/AndroidRuntime(1346): at java.lang.Class.newInstanceImpl(Native Method)
12-04 06:53:07.007: E/AndroidRuntime(1346): at java.lang.Class.newInstance(Class.java:1319)
12-04 06:53:07.007: E/AndroidRuntime(1346): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
12-04 06:53:07.007: E/AndroidRuntime(1346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
12-04 06:53:07.007: E/AndroidRuntime(1346): ... 11 more
------解决思路----------------------
亲,你再认真看下,你自己看看哪个代码是先初始化的.
------解决思路----------------------
按照您说的代码 我重新改过 把handler 放到onCreate里面
然后下面就是线程调用某个方法 返回的是一串字符串
在线程调用handler时 报了如下错误12-04 10:10:01.911: E/AndroidRuntime(1099): FATAL EXCEPTION: Thread-106
12-04 10:10:01.911: E/AndroidRuntime(1099): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-04 10:10:01.911: E/AndroidRuntime(1099): at android.os.Handler.<init>(Handler.java:121)
12-04 10:10:01.911: E/AndroidRuntime(1099): at com.example.webtest.MainActivity$2.run(MainActivity.java:67)
12-04 10:10:01.911: E/AndroidRuntime(1099): at java.lang.Thread.run(Thread.java:856)
线程里直接用主线程的handler?
不对吧。线程是要发消息给主线程的,例如在主线程ui的修改!
你可以在new 线程的时候,给予线程一个构造函数,传入主线程的handler
例如:
public MyThread(Handler handler)//handler是主线程的
{
this.handler=handler;//获取主线程的handler
}
//之后在想自己想要的地方发消息给主线程
Message msg = handler.obtainMessage(OtherMessage.GET_SUCCESS); //消息是自定义的,这里发的是空消息
handler.sendMessage(msg); //发送消息,然后去主线程处理这个消息
还有其他方法。。如果还是不懂可以去网上找例子