当我在“拉伸到填充屏幕”到“缩放到填充屏幕”之间切换时,我在Wizerd创建的一个简单的
Android项目上获得了一个空指针异常,该项目由一个分开的过程中的服务创建.如果将服务放入与主活动相同的进程中,即未指定“android:process”属性,则不会发生此崩溃.虽然只有当我将“android:process”添加到我的测试服务的清单文件时才会发生.
例外是:
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.这只是一个假设.