gpt4 book ai didi

android - 无法在 AsyncTask 中访问 “findViewById”

转载 作者:行者123 更新时间:2023-11-29 15:45:06 26 4
gpt4 key购买 nike

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

在这里,他用了

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

来自父类。我的 parent 也没有 inflater,它是一张 map 。

并且从异步任务 doitbackground 中,我返回了 5 个数组列表以在 postexecute 中创建 textview

但我做不到,因为我无法使用 findviewbyid,因为我无法获得 Activity 。我得到了上下文,但它没有做任何事情。

这是我的执行后

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

因为我没有打气筒

我不想从执行后返回到主类或另一个包装类。我想我可以在 postexecute 中做,不是吗?

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

如果我这样做,我将采用当前 View ,而不是我想要创建的 View ?

我试过了

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) 创建接口(interface)类。

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

b) 转到您的 AsyncTask 类,并将接口(interface) AsyncResponse 声明为一个字段:

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

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

关于android - 无法在 AsyncTask 中访问 “findViewById”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34213217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com