Android 在方法外部使用字符串[]
我试图在Android 在方法外部使用字符串[],android,methods,scope,Android,Methods,Scope,我试图在onPostExecute(String)方法之外使用String[]mtake。它在函数中为我提供了适当的值,但在onCreate()方法中没有任何值,simple会使应用程序崩溃。感谢您的帮助。提前谢谢。我已经尝试在全局和类内部声明它为publicstatic。静态也不起作用 @SuppressLint({ "CutPasteId", "SimpleDateFormat", "SdCardPath" }) public class MainActivity extends Acti
onPostExecute(String)
方法之外使用String[]mtake
。它在函数中为我提供了适当的值,但在onCreate()
方法中没有任何值,simple会使应用程序崩溃。感谢您的帮助。提前谢谢。我已经尝试在全局和类内部声明它为publicstatic。静态也不起作用
@SuppressLint({ "CutPasteId", "SimpleDateFormat", "SdCardPath" })
public class MainActivity extends Activity implements View.OnClickListener {
public String[] mtake;
public class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
return response.toString();
}
public void onPostExecute (String result) {
mtake = result.split("#");
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Handler handIn = new Handler();
final Timer T2 = new Timer();
T2.scheduleAtFixedRate(new TimerTask() {
public void run() {
handIn.post(new Runnable() {
public void run() {
txt = (TextView) findViewById(R.id.Text);
txt.setText(mtake[0]);
}
});
}
},0, 20000);
}
@SuppressLint({“CutPasteId”、“SimpleDataFormat”、“SdCardPath”})
公共类MainActivity扩展活动实现View.OnClickListener{
公共字符串[]mtake;
公共类下载WebPagetTask扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
字符串响应=”;
for(字符串url:url){
DefaultHttpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(url);
试一试{
HttpResponse execute=client.execute(httpGet);
InputStream内容=execute.getEntity().getContent();
BufferedReader buffer=新的BufferedReader(新的InputStreamReader(内容));
字符串s=“”;
而((s=buffer.readLine())!=null){
响应+=s;
}
}
捕获(例外e){
e、 printStackTrace();
}
}
返回response.toString();
}
public void onPostExecute(字符串结果){
mtake=result.split(“#”);
}
}
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
最终处理程序handIn=新处理程序();
最终定时器T2=新定时器();
T2.scheduleAtFixedRate(新TimerTask(){
public void run(){
handIn.post(新的Runnable(){
公开募捐{
txt=(TextView)findViewById(R.id.Text);
txt.setText(mtake[0]);
}
});
}
},0, 20000);
}
您没有用任何东西实例化mTake
数组。在onPostExecute
方法中,您调用的是mTake.equals(result.split(#))
它返回一个布尔值,而不是字符串[]。您的意思是mTake=result.split(#“)
?是的。事实上是这样。抱歉,我没有编辑它…我现在会编辑它,但不会解决问题。mTake仍然作为本地字符串[]变量,我无法在onPostExecute()方法之外访问它