2013-10-13 136 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 :) 
     } 
    }); 
相關問題