进入新活动 Android 时出错
Posted
技术标签:
【中文标题】进入新活动 Android 时出错【英文标题】:Error when going into new activity Android 【发布时间】:2015-09-09 23:46:12 【问题描述】:我正在尝试开始新的设置活动。不幸的是,我收到了这些错误:
06-24 01:18:17.347: E/androidRuntime(16017): FATAL EXCEPTION: main
06-24 01:18:17.347: E/AndroidRuntime(16017): Process: com.stoper.moneystopwatch, PID: 16017
06-24 01:18:17.347: E/AndroidRuntime(16017): java.lang.RuntimeException: Unable to start activity ComponentInfocom.stoper.moneystopwatch/com.stoper.moneystopwatch.Settings: android.view.InflateException: Binary XML file line #1: Error inflating class RelativeLayout
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.ActivityThread.access$800(ActivityThread.java:144)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.os.Handler.dispatchMessage(Handler.java:106)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.os.Looper.loop(Looper.java:136)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.ActivityThread.main(ActivityThread.java:5212)
06-24 01:18:17.347: E/AndroidRuntime(16017): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 01:18:17.347: E/AndroidRuntime(16017): at java.lang.reflect.Method.invoke(Method.java:515)
06-24 01:18:17.347: E/AndroidRuntime(16017): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-24 01:18:17.347: E/AndroidRuntime(16017): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
06-24 01:18:17.347: E/AndroidRuntime(16017): at dalvik.system.NativeStart.main(Native Method)
06-24 01:18:17.347: E/AndroidRuntime(16017): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class RelativeLayout
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.preference.GenericInflater.inflate(GenericInflater.java:317)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
06-24 01:18:17.347: E/AndroidRuntime(16017): at com.stoper.moneystopwatch.Settings$MyPreferenceFragment.onCreate(Settings.java:28)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.Fragment.performCreate(Fragment.java:1683)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.BackStackRecord.run(BackStackRecord.java:684)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.Activity.performStart(Activity.java:5249)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
06-24 01:18:17.347: E/AndroidRuntime(16017): ... 11 more
06-24 01:18:17.347: E/AndroidRuntime(16017): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.RelativeLayout" on path: DexPathList[[zip file "/data/app/com.stoper.moneystopwatch-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.stoper.moneystopwatch-1, /vendor/lib, /system/lib]]
06-24 01:18:17.347: E/AndroidRuntime(16017): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-24 01:18:17.347: E/AndroidRuntime(16017): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-24 01:18:17.347: E/AndroidRuntime(16017): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.preference.GenericInflater.createItem(GenericInflater.java:375)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
06-24 01:18:17.347: E/AndroidRuntime(16017): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
06-24 01:18:17.347: E/AndroidRuntime(16017): ... 23 more
我的活动开始:
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
Intent i= new Intent(Main.this, Settings.class);
startActivity(i);
return true;
我的设置类:
public class Settings extends PreferenceActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
public static class MyPreferenceFragment extends PreferenceFragment
@Override
public void onCreate(final Bundle savedInstanceState)
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
我的 Android 清单:
<activity
android:name=".Settings"
android:label="@string/action_settings" >
</activity>
我已经尝试清理和重建项目。我正在使用 Eclipse。
如果有人能帮助我解决这个问题,那就太好了,因为我已经和它斗争了好几个小时了。
【问题讨论】:
【参考方案1】:考虑到您的 logcat,您的偏好 xml 中存在问题。
android.view.InflateException: Binary XML file line #1: Error inflating class RelativeLayout
您的 xml 不应包含任何 RelativeLayout 或任何常用的小部件,除了偏好特定标签 as described in here.
对于一个小测试,从R.xml.settings
中删除所有 xml 并粘贴
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="first_preferencescreen">
<CheckBoxPreference
android:key="wifi enabled"
android:title="WiFi" />
<PreferenceScreen
android:key="second_preferencescreen"
android:title="WiFi settings">
<CheckBoxPreference
android:key="prefer wifi"
android:title="Prefer WiFi" />
</PreferenceScreen> </PreferenceScreen>
然后再试一次。
【讨论】:
现在完美运行。谢谢你的帮助。我不知道我不能在这里使用RelativeLayout。 太好了!很乐意为您提供帮助【参考方案2】:您不能直接替换 android.R.id.content 布局,您需要有自己的布局作为子布局。那就是在 Xml 中有一个粗略的布局(比如 act_settings.xml),在你的 oncreate 中使用 setContentView(R.layout.act_settings);
并在你的片段事务中替换这个布局的根视图
【讨论】:
以上是关于进入新活动 Android 时出错的主要内容,如果未能解决你的问题,请参考以下文章