2013-10-13 116 views
-1

这里是我的代码执行doInBackground时发生错误吗?

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); 

     } 


} 

然后,当我运行:

E/AndroidRuntime(883): FATAL EXCEPTION: AsyncTask #1 
E/AndroidRuntime(883): java.lang.RuntimeException: An error occured while executing 
doInBackground() 
E/AndroidRuntime(883): at android.os.AsyncTask$3.done(AsyncTask.java:278) 

每个帮助是珍贵

+1

你能张贴整个logcat的外部内部,所以我们可以看到确切的错误 –

+1

把你的doInBackground方法中设置断点,并通过它一步到找到错误。 – Kuffs

回答

0

你试图从后台线程访问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; 
    } 
} 

然后你onCreate

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

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

    } 
+0

它不起作用:( –

+0

哦,好吧,我现在看到它,你从'doInBackground'调用另一个'asynctask'。Asynctasks *必须*从UI线程调用,否则它们会抛出'RuntimeException'到这里。 – Jon

0

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

这里也有几个约定问题:

String RollCallID = "14"; 
list = (ListView)findViewById(R.id.listAtStudent); 
ArrayList<Student> rollCalls = new AllBO().getRollCallInfo(RollCallID); 
array = rollCalls; 
  • 变量不应该以一个大写rollCallID

  • XML ID的不应该是骆驼套管R.id.list_as_student

  • 阵列不用于启动在适配器外面,为什么在它之外使用一个变量?
0

使用MainActivity中的Handler对象并发布runnable。要从背景中使用它,你需要使对象成为静态的,你可以在MainActivity之外调用它,或者你可以创建一个Activity的静态实例来访问它。

活动

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 :) 
     } 
    }); 
相关问题