Android 常用错误及解决方法
1、Android模拟器报错
emulator: ERROR: Could not load OpenGLES emulation library:
lib64OpenglRender.so: cannot open shared object file: No such file or
directory
emulator: WARNING: Could not initialize OpenglES emulation, using software renderer.
emulator: WARNING: Could not initialize OpenglES emulation, using software renderer.
解决方法:
从目录 SDK\tools\lib 将下列文件复制到目录 SDK\tools
lib64EGL_translator.so
lib64GLES_CM_translator.so
lib64GLES_V2_translator.so
lib64OpenglRender.so
libEGL_translator.so
libGLES_CM_translator.so
libGLES_V2_translator.so
libOpenglRender.so
2、启动模拟器报错
emulator: ERROR: unknown virtual device name: 'WVGA'
emulator: could not find virtual device named 'WVGA'
emulator: could not find virtual device named 'WVGA'
解决方法:
设置环境变量ANDROID_SDK_HOME,指向目录 .android/avd 的父目录。
3、使用ARM模拟器启动后进入应用列表,稍后报错
android emulator Unfortunately Launcher has stopped
解决方法:
当前安装的模拟器版本不稳定,换一个版本。或者在Windows XP中重新安装一个。
4、findViewById 出现 java.lang.NullPointerException
W/dalvikvm( 716): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 716): FATAL EXCEPTION: main
E/AndroidRuntime( 716): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cn.uc.uzone.messagecenter/cn.uc.uzone.messagecenter.MessageCenter}: java.lang.NullPointerException
E/AndroidRuntime( 716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
E/AndroidRuntime( 716): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 716): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 716): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 716): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 716): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 716): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 716): at java.lang.reflect.Method.invokeNative(NativeMethod)
E/AndroidRuntime( 716): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 716): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 716): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 716): at android.app.Activity.findViewById(Activity.java:1647)
E/AndroidRuntime( 716): at cn.uc.uzone.messagecenter.MessageCenter.<init>(MessageCenter.java:22)
E/AndroidRuntime( 716): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 716): at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime( 716): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime( 716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
E/AndroidRuntime( 716): ... 11 more
E/AndroidRuntime( 716): FATAL EXCEPTION: main
E/AndroidRuntime( 716): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cn.uc.uzone.messagecenter/cn.uc.uzone.messagecenter.MessageCenter}: java.lang.NullPointerException
E/AndroidRuntime( 716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
E/AndroidRuntime( 716): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 716): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 716): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 716): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 716): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 716): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 716): at java.lang.reflect.Method.invokeNative(NativeMethod)
E/AndroidRuntime( 716): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 716): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 716): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 716): at android.app.Activity.findViewById(Activity.java:1647)
E/AndroidRuntime( 716): at cn.uc.uzone.messagecenter.MessageCenter.<init>(MessageCenter.java:22)
E/AndroidRuntime( 716): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 716): at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime( 716): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime( 716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
E/AndroidRuntime( 716): ... 11 more
代码:
public class MessageCenter extends Activity {
/**
* 日志分类。
*/
private String logCategory = "MessageList";
/**
* 发送消息按钮。
*/
private Button sendMsgBtn = (Button) findViewById(R.id.button_send_msg); // 报错的代码行
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_center);
// 发达消息按钮绑定事件
this.sendMsgBtn.setOnClickListener(new SendMsgBtnListener());
}
... ... ...
}
原因:
当在onCreate方法中使用setContentView(R.layout.main)时,那么才可以使用findVIewById这个方法。
这时候还要注意一点,就是setContentView(R.layout.main)一定要在findViewById之前,否则还是会报java.lang.NullPointerException的。