2

我已经调试了几个小时,但仍然没有运气。我使用 Android 片段制作了一个 RSS 阅读器。它在运行 Android 的设备上运行良好,但在 Android设备4.0.x to 4.1.x上启动时崩溃。4.2.x

我真的很感激我能得到的任何帮助。

日志猫:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.impsycho.androidpakistan/com.impsycho.androidpakistan.ItemListActivity}: 

android.view.InflateException: Binary XML file line #1: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5039)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
    at android.app.Activity.setContentView(Activity.java:1881)
    at com.impsycho.androidpakistan.ItemListActivity.onCreate(ItemListActivity.java:13)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
Caused by: java.lang.NullPointerException
    at com.impsycho.androidpakistan.ItemListFragment$GetAllThePosts.onPreExecute(ItemListFragment.java:157)
    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
    at android.os.AsyncTask.execute(AsyncTask.java:534)
    at com.impsycho.androidpakistan.ItemListFragment.onCreate(ItemListFragment.java:54)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:835)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
    ... 20 more
Sending signal. PID: 30286 SIG: 9

主要活动:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class ItemListActivity extends FragmentActivity implements ItemListFragment.Callbacks {
    private boolean mTwoPane;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list);
        getActionBar().setDisplayShowTitleEnabled(false);

        if (findViewById(R.id.item_detail_container) != null) {
            mTwoPane = true;
            ((ItemListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.item_list))
                    .setActivateOnItemClick(true);
        }
    }
    ...

activity_item_list.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_list"
    android:name="com.impsycho.androidpakistan.ItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ItemListActivity" />

activity_item_twopane.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".ItemListActivity" >

    <fragment
        android:id="@+id/item_list"
        android:name="com.impsycho.androidpakistan.ItemListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2.5" />

    <FrameLayout
        android:id="@+id/item_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="5" />

</LinearLayout>
4

1 回答 1

1

好的,所以我知道出了什么问题。

我专注于错误的错误。该错误实际上是在我的方法GetAllThePosts()中,该方法在应用程序启动时调用,因此布局无法膨胀。它甚至不是错误,因为它在所有其他 Android 版本中都运行良好。

view出于某种原因,当应用程序在 Android 4.2.x 中启动时,您无法更改菜单项的

负责的代码是:

private class GetAllThePosts extends AsyncTask<Void, Void, Void> {
        protected void onPreExecute() {
            RefreshMenuButton.setActionView(R.layout.action_progress);
            RefreshMenuButton.expandActionView();

            ...

我刚刚添加了一个条件语句来检查我们是否在 Android 4.2 或更高版本上。如果我们是,它不会第一次运行。

于 2013-01-18T02:39:34.247 回答