无法在AsyncTask中访问"findViewById"

1 android android-asynctask android-activity

/sf/answers/991502291/

在这里,他用过

View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
Run Code Online (Sandbox Code Playgroud)

来自父类.我inflater在父母身上也没有,这是一张地图.

而从的AsyncTask doitbackground,我回到5周的ArrayList,使textviewpostexecute.

但我做不到因为我不能使用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");
Run Code Online (Sandbox Code Playgroud)

这部分是错误的

 LinearLayout shw_evnt = (LinearLayout) shw_evnt.findViewById(R.id.);
Run Code Online (Sandbox Code Playgroud)

我尝试了其他的东西,但它没有用.我将使用之前未使用的其他布局.

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>
Run Code Online (Sandbox Code Playgroud)

这里是来电者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
Run Code Online (Sandbox Code Playgroud)

这里有错误

View myFragmentView = inflater.inflate(R.layout.show_events, container, false);
Run Code Online (Sandbox Code Playgroud)

因为我没有充气

我不想从postexecute返回到主类或另一个包装类.我想我可以在postexecute里面做,不能吗?

LayoutInflater inflater = (LayoutInflater)context.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);
Run Code Online (Sandbox Code Playgroud)

如果我这样做,我将采取当前的观点,而不是我想创建的视图?

我试过了

private Inflater inflater;
     View myFragmentView = inflater.inflate(R.layout.show_events, container, false);
Run Code Online (Sandbox Code Playgroud)

Jor*_*sys 5

是的,只是传递ActivityAsynctask:

   AsyncTask myAsyncTask = new MyTask(this);
Run Code Online (Sandbox Code Playgroud)

然后你会发现里面的元素layoutActivity:

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);
...
...
    }
}
Run Code Online (Sandbox Code Playgroud)

像丹尼尔介绍这将是一个不好的做法.

我向你推荐一个界面.

例如:

a)创建接口类.

public interface AsyncResponse {
    void processFinish(String output);
}
Run Code Online (Sandbox Code Playgroud)

b)转到AsyncTask类,并将接口AsyncResponse声明为字段:

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

c)在您的主Activity中,您需要实现接口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);
       }
     }
Run Code Online (Sandbox Code Playgroud)