Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
Java 执行doInBackground时出错?_Java_Android_Listview_Onclicklistener - Fatal编程技术网

Java 执行doInBackground时出错?

Java 执行doInBackground时出错?,java,android,listview,onclicklistener,Java,Android,Listview,Onclicklistener,这是我的密码 package com.example.rollsystems; import java.util.ArrayList; import org.dto.Student; import android.os.AsyncTask; import android.os.Bundle; import android.widget.ListView; import android.widget.TextView; import android.app.Activity; import a

这是我的密码

package com.example.rollsystems;

import java.util.ArrayList;
import org.dto.Student;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import com.example.listview.*;

import com.example.Utils.AllBO;
import com.example.rollsystems.R;

public class RollCallActivity extends Activity {

    ArrayList<Student> array;
    ListStudentAdapter arrayAdapter;
    ListView list;

    TextView txtClassAt;
    TextView txtSubjectAt;
    TextView txtInstructorAt;
    TextView txtTimeAt;
    TextView txtDateAt;
    Context context;

    public class RollCallTask extends AsyncTask<Void, Void, Void> {

        public RollCallActivity activity;

        public RollCallTask(RollCallActivity a)
        {
            activity = a;
        }
        @Override
        protected Void doInBackground(Void... params) {
            //SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("rs", Context.MODE_PRIVATE);
            //String RollCallID = sharedPref.getString("RollCallID", "14");

                String RollCallID = "14";
                list = (ListView)findViewById(R.id.listAtStudent);
                ArrayList<Student> rollCalls = new AllBO().getRollCallInfo(RollCallID);
                array = rollCalls;

            return  null;
        }
        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            arrayAdapter = new ListStudentAdapter(activity, R.layout.list_atstudent, array);
            list.setAdapter(arrayAdapter);
            txtClassAt = (TextView) findViewById(R.id.txtClassAt);
        }

    }

    /** Called when the activity is first created. */

      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.tab_rollcall);

            new RollCallTask(this).execute();
            txtClassAt = (TextView) findViewById(R.id.txtClassAt);

        }


}

每一个帮助都是宝贵的

您试图从后台线程访问UI。您需要在
doInBackground
之外执行此操作

您还必须从UI线程调用
AsyncTask
。未能这样做是运行时错误的原因

public class RollCallTask extends AsyncTask<Void, Void, Void> {

    public RollCallActivity activity;

    public RollCallTask(RollCallActivity a)
    {
        activity = a;
    }        

    @Override
    protected Void doInBackground(Void... params) {
        //SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("rs", Context.MODE_PRIVATE);
        //String RollCallID = sharedPref.getString("RollCallID", "14");

        String RollCallID = "14";
        ArrayList<Student> rollCalls = new AllBO().getRollCallInfo(RollCallID);
        array = rollCalls;

        return  null;
    }
}

异步任务正在访问UI。不要在其中执行
findViewById
,使用活动/片段中的
onCreate

这里还有几个公约问题:

String RollCallID = "14";
list = (ListView)findViewById(R.id.listAtStudent);
ArrayList<Student> rollCalls = new AllBO().getRollCallInfo(RollCallID);
array = rollCalls;
String RollCallID=“14”;
列表=(ListView)findViewById(R.id.listAtStudent);
ArrayList rollCalls=new AllBO().getRollCallInfo(RollCallID);
数组=roll调用;
  • 变量不应以大写的rollCallID开头

  • xml id不应为驼峰式
    R.id.list\u as\u student

  • 数组不在适配器外部使用,为什么要在适配器外部使用变量

使用MainActivity中的Handler对象并发布runnable。要从backgrund使用它,您需要将对象设置为静态对象,您可以在MainActivity外部调用该对象,或者您可以创建活动的静态实例来访问它

活动内部

private static Handler handler;


handler = new Handler();

handler().post(new Runnable() {

    public void run() {
        //ui stuff here :)
    }
});

public static Handler getHandler() {
   return handler;
}
MainActivity.getHandler().post(new Runnable() {

        public void run() {
            //ui stuff here :)
        }
    });
活动之外

private static Handler handler;


handler = new Handler();

handler().post(new Runnable() {

    public void run() {
        //ui stuff here :)
    }
});

public static Handler getHandler() {
   return handler;
}
MainActivity.getHandler().post(new Runnable() {

        public void run() {
            //ui stuff here :)
        }
    });

你能发布整个日志吗,这样我们就可以看到确切的错误在你的doInBackground方法上放置一个断点,然后逐步查找错误。哦,好的,我现在看到了。您正在从
doInBackground
调用另一个
asynctask
。必须从UI线程调用Asynctasks,否则它们将抛出您在此处获得的
RuntimeException