Android 打开设置时应用崩溃
Android App Crashing When Opening Settings
我正在为 class 项目创建一个 android 应用程序,这是我第一次编写任何 android 代码。基本上我有一个主要 activity 和一个偏好 activity。我添加了一个菜单,用户可以在其中访问设置并进行测试,我设置了一条 toast 消息,以便在单击设置时显示。在观看了一些视频并阅读了几篇文章后,我开始实施设置 activity,但是现在当我点击设置时,应用程序崩溃了,我不知道为什么。请帮我。
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText numChunks;
TextView appear;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Start
numChunks = (EditText) findViewById(R.id.chunksInput);
appear = (TextView) findViewById(R.id.display);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(v -> {
int chunks = Integer.parseInt(numChunks.getText().toString());
appear.setText("You chose " + chunks + " chunks");
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.focus_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
//Toast.makeText(this, "Opening settings...", Toast.LENGTH_SHORT).show();
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void openSettings() {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
}
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.preferencePreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Break Time">
<CheckBoxPreference
android:title="Short Break Time"
android:key="short"
android:defaultValue="false" />
<CheckBoxPreference
android:title="Long Break Time"
android:key="long"
android:defaultValue="false" />
</PreferenceCategory>
<PreferenceCategory
android:title="About">
</PreferenceCategory>
</androidx.preferencePreferenceScreen>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omicron.focus">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Focus">
<activity android:name=".SettingsActivity"
android:label="Settings"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
应用程序错误日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.omicron.focus, PID: 14313
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.omicron.focus/com.omicron.focus.SettingsActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.preferencePreferenceScreen
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.preferencePreferenceScreen
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:451)
at android.preference.GenericInflater.inflate(GenericInflater.java:325)
at android.preference.GenericInflater.inflate(GenericInflater.java:271)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:343)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1541)
at com.omicron.focus.SettingsActivity.onCreate(SettingsActivity.java:13)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.preferencePreferenceScreen" on path: DexPathList[[zip file "/data/app/~~jjvjGm-imz3PHV2zESqthg==/com.omicron.focus-3PZT9wHKp29rqZJeJbYAYQ==/base.apk"],nativeLibraryDirectories=[/data/app/~~jjvjGm-imz3PHV2zESqthg==/com.omicron.focus-3PZT9wHKp29rqZJeJbYAYQ==/lib/x86, /system/lib, /system_ext/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:439)
at android.preference.GenericInflater.inflate(GenericInflater.java:325)
at android.preference.GenericInflater.inflate(GenericInflater.java:271)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:343)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1541)
at com.omicron.focus.SettingsActivity.onCreate(SettingsActivity.java:13)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
如果您正在使用 AndroidX,您应该更新您的依赖项:
implementation "androidx.legacy:legacy-preference-v14:1.0.0"
implementation "androidx.preference:preference:1.0.0"
遗产是旧的 com.android.support:preference-v14 而另一个是 com.android.support:preference-v7.
如果您不使用 AndroidX 但 Android 支持库,请不要将 AndroidX 小部件导入您的 XML。
因此,如果您查看错误日志,就可以了解导致问题的原因:
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.preferencePreferenceScreen
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.preferencePreferenceScreen" on path: ......
您需要更改此行
<androidx.preferencePreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
到以前的任何内容或依赖项。
解决这个问题的最简单方法是创建一个新的 activity,然后复制粘贴,然后删除 activity。当我升级我的 gradle 之前,它完全操蛋了。
我正在为 class 项目创建一个 android 应用程序,这是我第一次编写任何 android 代码。基本上我有一个主要 activity 和一个偏好 activity。我添加了一个菜单,用户可以在其中访问设置并进行测试,我设置了一条 toast 消息,以便在单击设置时显示。在观看了一些视频并阅读了几篇文章后,我开始实施设置 activity,但是现在当我点击设置时,应用程序崩溃了,我不知道为什么。请帮我。
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText numChunks;
TextView appear;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Start
numChunks = (EditText) findViewById(R.id.chunksInput);
appear = (TextView) findViewById(R.id.display);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(v -> {
int chunks = Integer.parseInt(numChunks.getText().toString());
appear.setText("You chose " + chunks + " chunks");
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.focus_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
//Toast.makeText(this, "Opening settings...", Toast.LENGTH_SHORT).show();
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void openSettings() {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
}
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.preferencePreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Break Time">
<CheckBoxPreference
android:title="Short Break Time"
android:key="short"
android:defaultValue="false" />
<CheckBoxPreference
android:title="Long Break Time"
android:key="long"
android:defaultValue="false" />
</PreferenceCategory>
<PreferenceCategory
android:title="About">
</PreferenceCategory>
</androidx.preferencePreferenceScreen>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omicron.focus">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Focus">
<activity android:name=".SettingsActivity"
android:label="Settings"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
应用程序错误日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.omicron.focus, PID: 14313
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.omicron.focus/com.omicron.focus.SettingsActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.preferencePreferenceScreen
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.preferencePreferenceScreen
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:451)
at android.preference.GenericInflater.inflate(GenericInflater.java:325)
at android.preference.GenericInflater.inflate(GenericInflater.java:271)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:343)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1541)
at com.omicron.focus.SettingsActivity.onCreate(SettingsActivity.java:13)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.preferencePreferenceScreen" on path: DexPathList[[zip file "/data/app/~~jjvjGm-imz3PHV2zESqthg==/com.omicron.focus-3PZT9wHKp29rqZJeJbYAYQ==/base.apk"],nativeLibraryDirectories=[/data/app/~~jjvjGm-imz3PHV2zESqthg==/com.omicron.focus-3PZT9wHKp29rqZJeJbYAYQ==/lib/x86, /system/lib, /system_ext/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:439)
at android.preference.GenericInflater.inflate(GenericInflater.java:325)
at android.preference.GenericInflater.inflate(GenericInflater.java:271)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:343)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1541)
at com.omicron.focus.SettingsActivity.onCreate(SettingsActivity.java:13)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
如果您正在使用 AndroidX,您应该更新您的依赖项:
implementation "androidx.legacy:legacy-preference-v14:1.0.0"
implementation "androidx.preference:preference:1.0.0"
遗产是旧的 com.android.support:preference-v14 而另一个是 com.android.support:preference-v7.
如果您不使用 AndroidX 但 Android 支持库,请不要将 AndroidX 小部件导入您的 XML。
因此,如果您查看错误日志,就可以了解导致问题的原因:
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.preferencePreferenceScreen
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.preferencePreferenceScreen" on path: ......
您需要更改此行
<androidx.preferencePreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
到以前的任何内容或依赖项。
解决这个问题的最简单方法是创建一个新的 activity,然后复制粘贴,然后删除 activity。当我升级我的 gradle 之前,它完全操蛋了。