getActivity 无法解析片段中的方法
getActivity cannot resolve method from fragment
我必须创建一个 ArrayAdapter,但是当我将 getActivity() 放入构造函数时,它显示 cannot resolve method getActivity()
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import android.widget.ArrayAdapter;
import java.net.HttpURLConnection;
import java.lang.Void;
import android.widget.ListView;
import android.os.AsyncTask;
import android.content.Context;
public class HttpURLConnectionExample extends AsyncTask<ListView, Void, String> {
public ListView myList = null;
public void sendGet(ListView lv){
myList = lv;
this.execute();
return;
}
@Override
protected String doInBackground(ListView... arg0) {
String url = "http://192.168.1.9";
URL obj = null;
try{
obj = new URL(url);}
catch(IOException e){}
HttpURLConnection con = null;
try{
con = (HttpURLConnection) obj.openConnection();}
catch(IOException e){}
//con.setRequestProperty("User-Agent", USER_AGENT);
try{
con.setRequestMethod("GET");}
catch(ProtocolException e){}
try{
con.connect();}
catch(IOException e){}
try{
int responseCode = con.getResponseCode();}
catch(IOException e){}
BufferedReader in = null;
try{
in = new BufferedReader(
new InputStreamReader(con.getInputStream()));}
catch(IOException e){}
String inputLine;
StringBuffer response = new StringBuffer();
try{
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}}
catch(IOException e){}
try{
in.close();}
catch(IOException e){}
//print result
System.out.println(response.toString());
return response.toString();
}
@Override
protected void onPostExecute(String result) {
String strParts[] = result.split("@");
ArrayList<dispencer> listAr = new ArrayList<dispencer>();
for(int i=0; i<strParts.length; i++){
listAr.add( new dispencer(strParts[i]) );
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1);
for (int i = 0; i < listAr.size(); ++i) {
adapter.add(listAr.get(i).toString());
}
myList.setAdapter(adapter);
return ;
}
}
我读到我必须使用 getActivity 方法,因为我在片段中而不是在上下文中。
编辑我无法扩展我的 MainActivity Class 因为这个 class 已经扩展了 asyncTask.
首先确保您导入了片段 class 并使您的片段扩展它。
定义类型为
的变量
Context mContext ;
然后像这样用 getActivity() 初始化这个变量
mContext=getActivity();
将此变量传递给您的适配器
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_expandable_list_item_1);
您没有扩展片段 class。所以你需要做以下事情。
当你想在异步任务class中使用上下文时。你需要在class构造函数中传递上下文。
例子
public class MyCustomTask extends AsyncTask<Void, Void, Long> {
private Context mContext;
public MyCustomTask (Context context){
mContext = context;
}
//other methods like onPreExecute etc.
protected void onPostExecute(Long result) {
Toast.makeText(mContext,"Its working", Toast.LENGTH_LONG).show();
} }
并通过以下方式实例化class
MyCustomTask task = new MyCustomTask(context);
task.execute(..);
您正在扩展 AsyncTask
而不是 Fragment
class,而是传递对构造函数的 Context
或 Activity
引用:
public MyTask(Context context)
并在您的任务中存储对它的引用。
我必须创建一个 ArrayAdapter,但是当我将 getActivity() 放入构造函数时,它显示 cannot resolve method getActivity()
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import android.widget.ArrayAdapter;
import java.net.HttpURLConnection;
import java.lang.Void;
import android.widget.ListView;
import android.os.AsyncTask;
import android.content.Context;
public class HttpURLConnectionExample extends AsyncTask<ListView, Void, String> {
public ListView myList = null;
public void sendGet(ListView lv){
myList = lv;
this.execute();
return;
}
@Override
protected String doInBackground(ListView... arg0) {
String url = "http://192.168.1.9";
URL obj = null;
try{
obj = new URL(url);}
catch(IOException e){}
HttpURLConnection con = null;
try{
con = (HttpURLConnection) obj.openConnection();}
catch(IOException e){}
//con.setRequestProperty("User-Agent", USER_AGENT);
try{
con.setRequestMethod("GET");}
catch(ProtocolException e){}
try{
con.connect();}
catch(IOException e){}
try{
int responseCode = con.getResponseCode();}
catch(IOException e){}
BufferedReader in = null;
try{
in = new BufferedReader(
new InputStreamReader(con.getInputStream()));}
catch(IOException e){}
String inputLine;
StringBuffer response = new StringBuffer();
try{
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}}
catch(IOException e){}
try{
in.close();}
catch(IOException e){}
//print result
System.out.println(response.toString());
return response.toString();
}
@Override
protected void onPostExecute(String result) {
String strParts[] = result.split("@");
ArrayList<dispencer> listAr = new ArrayList<dispencer>();
for(int i=0; i<strParts.length; i++){
listAr.add( new dispencer(strParts[i]) );
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1);
for (int i = 0; i < listAr.size(); ++i) {
adapter.add(listAr.get(i).toString());
}
myList.setAdapter(adapter);
return ;
}
}
我读到我必须使用 getActivity 方法,因为我在片段中而不是在上下文中。 编辑我无法扩展我的 MainActivity Class 因为这个 class 已经扩展了 asyncTask.
首先确保您导入了片段 class 并使您的片段扩展它。 定义类型为
的变量Context mContext ;
然后像这样用 getActivity() 初始化这个变量
mContext=getActivity();
将此变量传递给您的适配器
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_expandable_list_item_1);
您没有扩展片段 class。所以你需要做以下事情。
当你想在异步任务class中使用上下文时。你需要在class构造函数中传递上下文。
例子
public class MyCustomTask extends AsyncTask<Void, Void, Long> {
private Context mContext;
public MyCustomTask (Context context){
mContext = context;
}
//other methods like onPreExecute etc.
protected void onPostExecute(Long result) {
Toast.makeText(mContext,"Its working", Toast.LENGTH_LONG).show();
} }
并通过以下方式实例化class
MyCustomTask task = new MyCustomTask(context);
task.execute(..);
您正在扩展 AsyncTask
而不是 Fragment
class,而是传递对构造函数的 Context
或 Activity
引用:
public MyTask(Context context)
并在您的任务中存储对它的引用。