当我在“拉伸(stretch)以填满屏幕”和“缩放以填满屏幕”之间切换时,在 Wizerd 创建的一个简单的 Android 项目上,Android 3.2 模拟器出现空指针异常,该项目在单独的进程中提供服务。如果将服务放入与主 Activity 相同的进程中,即未指定“android:process”属性,则不会发生此崩溃。虽然只有当我将“android:process”添加到我的测试服务的 list 文件时才会发生。
异常(exception)情况是:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.view.WindowManagerImpl.reportNewConfiguration(WindowManagerImpl.java:427)
at android.app.ActivityThread.handleUpdatePackageCompatibilityInfo(ActivityThread.java:2801)
at android.app.ActivityThread.access$2700(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
我的测试代码:
TestActivity.java(由 Wizerd 生成)
package com.test;
import android.app.Activity;
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this, TestService.class);
startService(intent);
}
}
TestService.java(大部分函数为空)
Package com.test;
import android.content.ComponentName;
public class TestService extends Service {
private boolean m_connected = false;
private ServiceConnection m_connInitService = new ServiceConnection() {
public void onServiceConnection(ComponentName className, IBinder service) {
m_connected = true;
}
public void onServiceDisconnected(ComponentName className) {
}
};
public static class TestServiceBinder extends Binder {
}
public IBinder onBind(Intent intent) {
return new TestServiceBinder();
}
public void onDestroy() {
super.onDestroy();
}
public int onStartCommand(Intent intent, int flags, int startId) {
return 1;
}
}
如果我在没有服务的情况下运行测试应用程序,或者在同一进程中运行有服务的屏幕兼容性开关不会导致任何问题。但是我不明白为什么服务在屏幕兼容性切换时会导致系统异常。这是因为服务进程是非UI进程吗?哪个可能会触发 Android 核心代码中的错误?
最佳答案
我在文档中发现了以下内容:
android:process
If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.
因此,如果您指定以小写字母开头的进程名称,您应该有权执行此操作。但是我不知道这些权限是在哪里检查的。可能它们在 WindowManagerImpl 中被选中,但它找不到这个全局进程,因此返回 null。这只是一个假设。
关于android - Android OS 3.2 上 WindowManagerImpl 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7305360/