gpt4 book ai didi

Android setContentView(View) 抛出与appcompat-7库相关的NullPointerException

转载 作者:太空狗 更新时间:2023-10-29 14:41:37 25 4
gpt4 key购买 nike

我已经开发 Android 应用程序很长时间了,我遇到了很多问题,这些问题最终都得到了解决,但在我的上一个项目中,我遇到了一个与 appcompat-7 库相关的非常奇怪的问题我猜测。

NullPointerException 在调用 setContentView(View) 时大约有 10% 的频率被抛出。我的意思是每次执行都不会抛出异常。存在不确定性行为。

不确定,我想我错过了关于 WindowDecorView 的关键点。

AppCompatDelegateImplV9 类的方法 applyFixedSizeWindow 中的 cfl.setDecorPadding(windowDecor.getPaddingLeft()... 行抛出异常。

我还在问题末尾附加了异常的完整堆栈跟踪。

我认为这无关紧要,但说我使用 Android Studio 3.0 很有用。

欢迎所有建议和意见。

Activity 代码:

public class MainActivity extends AppCompatActivity {
...

private static final int HIDE_NAVBAR_DELAY = 500;

private static void hideNavBarAndActionBar(final Window _window) {
_window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LOW_PROFILE);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final View view = LayoutInflater.from(this).inflate(R.layout.screen_splash, null, false);
setContentView(view);

final Window window = getWindow();

// to hide bars after volume control popup or similar popup appears and disappers
window.getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
hideNavBarAndActionBar(window);
}
}
});
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);

//to hide of bars after activity window is focused (example case: resume activity by bringing ti from background to foreground)
if(hasFocus) {
final Window window = getWindow();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
hideNavBarAndActionBar(window);
}
}, HIDE_NAVBAR_DELAY);
}
}
...

模块的build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
aaptOptions {
cruncherEnabled = false
}
}

dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-ads:11.4.2'
implementation 'com.google.android.gms:play-services-auth:11.4.2'
}

异常的堆栈跟踪:

FATAL EXCEPTION: main
Process: my_package_name:my_process_name, PID: 25792
java.lang.RuntimeException: Unable to start activity ComponentInfo{my_package_name/my_package_name.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplV9.applyFixedSizeWindow(AppCompatDelegateImplV9.java:537)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:331)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:275)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:144)
at my_package_name.MainActivity.onCreate(MainActivity.java:68)
at android.app.Activity.performCreate(Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250) 
at android.app.ActivityThread.access$800(ActivityThread.java:139) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5105) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 
at dalvik.system.NativeStart.main(Native Method) 

最佳答案

我已经在我的 android studio 3 中测试了你的代码,我的 gradle 运行良好:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
defaultConfig {
applicationId "com.asb.test.testapplication"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

关于Android setContentView(View) 抛出与appcompat-7库相关的NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47142518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com