Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当Listview中的项目溢出时,我的应用程序崩溃_Android_Listview_Android Asynctask - Fatal编程技术网

Android 当Listview中的项目溢出时,我的应用程序崩溃

Android 当Listview中的项目溢出时,我的应用程序崩溃,android,listview,android-asynctask,Android,Listview,Android Asynctask,每当listview中的项目溢出时,我尝试构建的应用程序就会崩溃。 当项目受到限制时,它可以正常工作(>>List tmlist=transactionDAO.getAllTransactions(); >>>列表=新的ArrayList(); >>>int i=1; >>>List cm=categoryDAO.getAllCategories(); >>>对于(TransactionModal tm:tmlist){ >>>Long fromId=tm.getFromId(); >>>Lon

每当listview中的项目溢出时,我尝试构建的应用程序就会崩溃。 当项目受到限制时,它可以正常工作(>>List tmlist=transactionDAO.getAllTransactions(); >>>列表=新的ArrayList(); >>>int i=1; >>>List cm=categoryDAO.getAllCategories(); >>>对于(TransactionModal tm:tmlist){ >>>Long fromId=tm.getFromId(); >>>Long-toId=tm.getToId(); >>>事务t=新事务(Integer.toString(i)、categoryDAO.getCategoryName(fromId)、categoryDAO.getCategoryName(toId)、tm.getComment()、tm.getTransactionDate().toString()、Integer.toString(tm.getAmount()); >>>CategoryModal cmc=categoryDAO.getCategoryById(tm.getFromId()); >>>字符串to=cmc.getCategoryName(); >>>列表。添加(t); >>>i++; >>> } >>>tractivity.createTransactionList(列表); 打破 案例(“转换操作”): 打破 } 返回st; } } 这是交易活动

27    public void createTransactionList(List<Transaction> list){
28        Transaction header = new Transaction("SR","From","To","Comment","Date","Amount");
29        List<Transaction> temp = new ArrayList<>();
30        temp.add(header);
31        temp.addAll(list);
32        ListView transactionListView = findViewById(R.id.transactions_listView);
33        List<Transaction> transactionsList = temp;
34        TransactionListAdapter tLAdapter = new TransactionListAdapter(this,R.layout.transaction_list_adapter,transactionsList);
35        transactionListView.setAdapter(tLAdapter);
36    }
27公共作废createTransactionList(列表){
28交易标题=新交易(“SR”、“自”、“至”、“备注”、“日期”、“金额”);
29 List temp=new ArrayList();
30温度添加(收割台);
31临时添加(列表);
32 ListView transactionListView=findViewById(R.id.transactions\u ListView);
33列表事务列表=临时;
34 TransactionListAdapter tLAdapter=新TransactionListAdapter(this,R.layout.transaction\u list\u适配器,TransactionList);
35 transactionListView.setAdapter(tLAdapter);
36    }
这是包含listView的conent_transaction.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".TransactionsActivity"
    tools:showIn="@layout/activity_transactions">

    <ListView
        android:id="@+id/transactions_listView"
        android:layout_width="match_parent"
        android:scrollbars="vertical"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

编辑1: 事务列表适配器

class TransactionListAdapter extends ArrayAdapter<Transaction> {


    private Context context;
    private int resource;


    /**
     * Default constructor for transaction adapter
     * @param context
     * @param resource
     * @param objects
     */
    public TransactionListAdapter(@NonNull Context context, int resource, @NonNull List<Transaction> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
    }




    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        String sr = getItem(position).getSr();
        String from = getItem(position).getFrom();
        String to = getItem(position).getTo();
        String comment = getItem(position).getComment();
        String date = getItem(position).getDate();
        String amount = getItem(position).getAmount();

        Transaction transaction = getItem(position);

        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(resource,parent,false);
        TextView tvSr = convertView.findViewById(R.id.tr_list_sr);
        TextView tvTo = convertView.findViewById(R.id.tr_list_to);
        TextView tvFrom = convertView.findViewById(R.id.tr_list_from);
        TextView tvComment = convertView.findViewById(R.id.tr_list_comment);
        TextView tvDate = convertView.findViewById(R.id.tr_list_td);
        TextView tvAmount = convertView.findViewById(R.id.tr_list_amount);
        tvSr.setText(sr);
        tvTo.setText(to);
        tvFrom.setText(from);
        tvComment.setText(comment);
        tvAmount.setText(amount);
        tvDate.setText(date);

        return convertView;
    }
}
class TransactionListAdapter扩展了ArrayAdapter{
私人语境;
私有int资源;
/**
*事务适配器的默认构造函数
*@param上下文
*@param资源
*@param对象
*/
公共TransactionListAdapter(@NonNull上下文上下文,int资源,@NonNull列表对象){
超级(上下文、资源、对象);
this.context=上下文;
这个资源=资源;
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
字符串sr=getItem(position).getSr();
String from=getItem(position).getFrom();
字符串to=getItem(position.getTo();
字符串注释=getItem(position).getComment();
字符串日期=getItem(position).getDate();
字符串金额=getItem(位置).getAmount();
事务处理=获取项目(位置);
LayoutFlater充气机=LayoutFlater.from(上下文);
convertView=充气机。充气(资源,父项,false);
TextView tvSr=convertView.findViewById(R.id.tr_list_sr);
TextView tvTo=convertView.findViewById(R.id.tr_list_to);
TextView-tvFrom=convertView.findViewById(R.id.tr_列表_-from);
TextView TVComent=convertView.findViewById(R.id.tr_list_comment);
TextView tvDate=convertView.findViewById(R.id.tr_list_td);
TextView TVAMUNT=convertView.findViewById(R.id.tr\u列表\u金额);
tvSr.setText(sr);
tvTo.setText(to);
tvFrom.setText(from);
tvecomment.setText(comment);
tvmount.setText(金额);
tvDate.setText(日期);
返回视图;
}
}

溢出:根据您的异常日志,超出屏幕上可查看的特定数量

原因:android.view.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图


发生崩溃是因为您试图在非UI线程中更新视图。请根据异常日志提供使用AsyncTask的代码

原因:android.view.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图


发生崩溃是因为您尝试在非UI线程中更新视图。请在您尝试从AsyncTask的
doInBackground()
内部更新
view
时提供代码,这是不允许的。请按如下方式更新您的
DBClass

class DBClass extends AsyncTask<Void,Void,List<?>> {

    ....

    @Override
    protected List<?> doInBackground(Void... voids) {

        List mList = null;

        switch(operation) {

            ....

            case("GET_CATEGORIES"):
                try {
                    mList = categoryDAO.getAllCategoryNames();
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            case("GET_TRANSACTIONS"):
                ....

                mList = list;
                break;
        }

        return mList;
    }

    @Override
    protected void onPostExecute(List<?> list) {
        switch(operation) {
            case("GET_CATEGORIES"):
                mActivity.setList(list);
                break;
            case("GET_TRANSACTIONS"):
                tActivity.createTransactionList(list);
                break;
        }
    }
}
class DBClass扩展AsyncTask doInBackground(Void…voids){
列表mList=null;
开关(操作){
....
案例(“获取类别”):
试一试{
mList=categoryDAO.getAllCategoryNames();
}捕获(例外e){
e、 printStackTrace();
}
打破
案例(“获取交易”):
....
mList=列表;
打破
}
返回mList;
}
@凌驾
受保护的void onPostExecute(列表){
开关(操作){
案例(“获取类别”):
mActivity.setList(列表);
打破
案例(“获取交易”):
tractivity.createTransactionList(列表);
打破
}
}
}

您试图从AsyncTask的
doInBackground()
内部更新
视图,这是不允许的。请按如下方式更新您的
DBClass

class DBClass extends AsyncTask<Void,Void,List<?>> {

    ....

    @Override
    protected List<?> doInBackground(Void... voids) {

        List mList = null;

        switch(operation) {

            ....

            case("GET_CATEGORIES"):
                try {
                    mList = categoryDAO.getAllCategoryNames();
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            case("GET_TRANSACTIONS"):
                ....

                mList = list;
                break;
        }

        return mList;
    }

    @Override
    protected void onPostExecute(List<?> list) {
        switch(operation) {
            case("GET_CATEGORIES"):
                mActivity.setList(list);
                break;
            case("GET_TRANSACTIONS"):
                tActivity.createTransactionList(list);
                break;
        }
    }
}
class DBClass扩展AsyncTask doInBackground(Void…voids){
列表mList=null;
开关(操作){
....
案例(“获取类别”):
试一试{
mList=categoryDAO.getAllCategoryNames();
}捕获(例外e){
e、 printStackTrace();
}
打破
案例(“获取交易”):
....
mList=l
class DBClass extends AsyncTask<Void,Void,List<?>> {

    ....

    @Override
    protected List<?> doInBackground(Void... voids) {

        List mList = null;

        switch(operation) {

            ....

            case("GET_CATEGORIES"):
                try {
                    mList = categoryDAO.getAllCategoryNames();
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            case("GET_TRANSACTIONS"):
                ....

                mList = list;
                break;
        }

        return mList;
    }

    @Override
    protected void onPostExecute(List<?> list) {
        switch(operation) {
            case("GET_CATEGORIES"):
                mActivity.setList(list);
                break;
            case("GET_TRANSACTIONS"):
                tActivity.createTransactionList(list);
                break;
        }
    }
}