二进制XML文件行#:使用ArrayAdapter和AsyncTask使类<unknown>出现错误

时间:2016-11-11 04:48:15

标签: java android android-asynctask android-arrayadapter

所以我收到此错误消息:

...
android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
...
MainActivity.java上的

public void onWindowFocusChanged(boolean hasFocus) {
    ...
    loadBalanceTable(inThePast, inTheFuture);
    ...
}

private void loadBalanceTable(Calendar startDate, Calendar endDate) {
    AsyncBalanceLoader asyncBalanceLoader = new AsyncBalanceLoader(getApplicationContext(), this);
    asyncBalanceLoader.execute(startDate, endDate);
}

AsyncBalanceLoader.java

public class AsyncBalanceLoader extends AsyncTask<Calendar, Integer, ArrayList<Balance>> {

    private Context context;
    private AppCompatActivity activity;
    private ListView balanceView;

    public AsyncBalanceLoader(Context ctx, AppCompatActivity act) {
        context = ctx;
        activity = act;
    }

    @Override
    protected ArrayList<Balance> doInBackground(Calendar... date) {
        ...
        ArrayList<Balance> arrayListBalance = new ArrayList<Balance>();
        ...
        return arrayListBalance;
    }

    @Override
    protected void onPostExecute(ArrayList<Balance> arrayListBalance) {
        BalanceArrayAdapter balanceArrayAdapter = new BalanceArrayAdapter(context, arrayListBalance);
        balanceView.setAdapter(balanceArrayAdapter);
    }
}

BalanceArrayAdapter.java

public class BalanceArrayAdapter extends ArrayAdapter {

    public BalanceArrayAdapter(Context context, ArrayList<Balance> balance) {
        super(context, 0, balance);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.single_balance_line, parent, false);
        }
        ...
        return convertView;

    }
}

当BalanceArrayAdapter.java在线

时出现问题
convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.single_balance_line, parent, false);

当在用户界面线程上完成所有这些操作时,我没有遇到任何问题。

有关正在发生的事情的任何想法? 非常感谢提前!

1 个答案:

答案 0 :(得分:0)

我通过剖析错误代码

找出导致错误的原因
...
android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class <unknown>
...

奇怪的是,当整个进程在UI线程下运行时(在程序的先前版本上),这个xml布局文件正在被充分膨胀;然而,当它在onPostExecute()扩展类AsyncTask的{​​{1}}上膨胀时,虽然也有UI线程,但我收到了错误。这是有问题的xml文件:

AsyncBalanceLoader

第二个<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1"> ... </LinearLayout> ... </LinearLayout> </LinearLayout> 无效,第一个{a.k.a.根视图)只包含一个孩子,无视LinearLayout组视图的目的。

所以,我真的不知道每个人说的“错误”在哪里,但这就是原因。

我改为:

LinearLayout

希望有人稍后会解释真正原因的深入。