簡體   English   中英

無法訪問AsyncTask中的“ findViewById”

[英]Can not access “findViewById” in AsyncTask

https://stackoverflow.com/a/14164318/3575963

在這里,他用

View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);

來自父類。 我的父母也沒有inflater ,這是一張地圖。

從asynctask doitbackground ,我返回了5個arraylist以在postexecute創建textview

但是我做不到,因為我無法獲得活動,所以無法使用findviewbyid。 我有上下文,但是它什么也沒做。

這是我的后postexecute

  protected void onPostExecute(Wrapper wrap){

     TextView name = new TextView (mContext);
     TextView type = new TextView (mContext);
     TextView location = new TextView (mContext);
     TextView distance = new TextView (mContext);

     List<Double> dist = new ArrayList();
     List<String> loc = new ArrayList();
     List<String> nme = new ArrayList();
     List<String> typ = new ArrayList();
     List<Calendar> start = new ArrayList();
     List<Calendar> endd = new ArrayList();

     dist = wrap.getDist();
     loc = wrap.getLocation();
     nme = wrap.getName();
     typ = wrap.getType();
     start = wrap.getsDate();
     endd = wrap.geteDate();
     int idx = -1;
     LinearLayout shw_evnt = (LinearLayout) shw_evnt.findViewById(R.id.);

      for(Double dis:dist){
         idx++;
         name.setText(nme.get(idx));
         type.setText(typ.get(idx));
         location.setText(loc.get(idx));
         distance.setText(dist.get(idx).toString()+" meters");

這部分有毛病

 LinearLayout shw_evnt = (LinearLayout) shw_evnt.findViewById(R.id.);

我嘗試了其他方法,但是沒有用。 我將使用以前未使用過的另一種布局。

show_events.xml:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    android:id="@+id/shwevnt"

</LinearLayout>

這里調用者mapactivity類

 public class MapActivity extends AppCompatActivity  implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
    private int strokeColor = 0xffff0000; //red outline
    private int shadeColor = 0x44ff0000; //opaque red fill
    private int count=0;
    private GoogleMap googleMap;
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    private LocationRequest mLocationRequest;
    private Context mContext;
    private String TAG = "Chic";
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
    private int radius;//in meters
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "inside map oncreaste");
        mContext =  getApplicationContext();
        View myFragmentView = inflater.inflate(R.layout.show_events, container, false);
        super.onCreate(savedInstanceState);
        setUpMapIfNeeded();
        Log.d(TAG, "buildgoogleapi called");
        buildGoogleApiClient();
        Log.d(TAG, "after buildgoogleapi called");
     }//end of oncreate

這里的錯誤

View myFragmentView = inflater.inflate(R.layout.show_events, container, false);

因為我沒有充氣機

我不想從后執行返回到主類或另一個包裝器類。 我想我可以在后執行中做,不是嗎?

LayoutInflater inflater = (LayoutInflater)context.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);

如果我這樣做,我將采用當前視圖,而不是要創建的視圖?

我試過了

private Inflater inflater;
     View myFragmentView = inflater.inflate(R.layout.show_events, container, false);

是的,只需將Activity傳遞給Asynctask

   AsyncTask myAsyncTask = new MyTask(this);

然后,您將在Activitylayout內找到該元素:

public class MyTask extends AsyncTask<String, String, String>{
    public MyActivity activity;

    public MyTask(MyActivity a){
        this.activity = a;
    }
    protected void onPostExecute(String result){
...
...
       LinearLayout shw_evnt = (LinearLayout) activity.findViewById(R.id.shwevnt);
...
...
    }
}

就像丹尼爾(Daniel)所描述的那樣,這是一種不好的做法

我向您推薦一個Interface

例如:

a)創建接口類。

public interface AsyncResponse {
    void processFinish(String output);
}

b)轉到您的AsyncTask類,並將接口AsyncResponse聲明為字段:

public class MyAsyncTask extends AsyncTask{
    public AsyncResponse delegate = null;
    @Override
    protected void onPostExecute(String result) {
         delegate.processFinish(result);
    }
}

c)在您的主要活動中,您需要實現接口AsyncResponse。

public class MainActivity implements AsyncResponse{    
MyAsyncTask asyncTask =new MyAsyncTask();
      @Override
      public void onCreate(Bundle savedInstanceState) {
         //this to set delegate/listener back to this class
         asyncTask.delegate = this;

         //execute the async task 
         asyncTask.execute();
      }
      //this override the implemented method from asyncTask
      void processFinish(String output){
         //Here you will receive the result fired from async class 
         //of onPostExecute(result) method.
           LinearLayout shw_evnt = (LinearLayout) findViewById(R.id.shwevnt);
       }
     }

這是解決方案。

facturas_edittext=(EditText)((Activity)context).findViewById(R.id.mye);

只需將Activity作為參數傳遞給AsyncTask類。 這里 請注意,將上下文存儲為成員變量是錯誤的做法,因為上下文可能會更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM