0

嗨朋友们,我知道关于这个话题有很多问题,但我无法从他们那里得到任何结果。我正在使用我的 ClassIsInternalParser 解析 xml 数据扩展默认处理程序。我在内部类 PostAsync extends AsyncTask 的活动中使用此类

但原因是我无法将我在 PostAsync 类中收集的数据返回给主要活动。它只设置 null

这是我的代码

package com.example.uiexercisesplash;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ClassIsInternalListViewActivity extends Activity implements  OnClickListener{  



TextView tv, tv2;
Button back;

String[][] array = new String[10][3];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.classisinternal_listview);

    new PostAsync().execute();

 tv=(TextView) findViewById(R.id.tatar);
 tv2= (TextView) findViewById(R.id.tatar2);

  tv2.setText(array[0][0]);   //Why this sets null!!

 back= (Button) findViewById(R.id.back);
 back.setOnClickListener(this);

}   

class PostAsync extends AsyncTask<Void, Void,String[][]>{

    ProgressDialog pd;
    ClassIsInternalParser  parser;

    @Override
    protected void onPreExecute() {
        //we can set up variables here
        pd = ProgressDialog.show(ClassIsInternalListViewActivity.this,
"Classisinternal","Loading last post...",true,false);   
    }

    protected void onPostExecute(String[][] result) {

        //in this way it sets correctly 
        tv.setText(result[0][0]);  


         array=result;

    pd.dismiss();

        pd.cancel();
    }

    protected String[][] doInBackground(Void... params) {
        parser = new ClassIsInternalParser();
        parser.get();

        return parser.dataArray;
    }
    }


@Override
public void onClick(View v) {
            finish();   
    }

}
4

2 回答 2

0

tv2.setText(array[0][0]); 为空,因为您在设置值之前获取数组值,即 tv2.setText(array[0][0]);在完成 Asynctask 之前执行。

onPostExecute 因此,在方法中执行此步骤为

protected void onPostExecute(String[][] result) {

        //in this way it sets correctly 
        tv.setText(result[0][0]);  
        tv2.setText(result[0][2]);

         array=result;
         tv2.setText(array[0][0]);// add here
    pd.dismiss();

        pd.cancel();
    }
于 2014-07-14T10:18:46.453 回答
0

尝试创建 PostAsync 对象并 execute()从该对象调用。在该调用get()方法之后检索您的返回数组,如下所示:

PostAsync obj=new PostAsync(this);
obj.execute();
String[][] array=obj.get();
tv2.setText(array[0][0]);

于 2014-07-14T10:38:06.243 回答