初始化后,static变量为null

时间:2013-08-23 16:09:03

标签: android static android-listfragment

我有一个带有3个标签的操作栏活动。 我有一个主要活动,我将xml发布到webservice并在AsyncTaskj中返回一个xml。我正在初始化AsncTask中主要活动的静态变量。从主活动调用另一个活动,其中Action Bar已初始化。每个操作栏都有一个ListFragment,我使用静态变量。我没有问题从2列表活动访问静态变量,但在第一个ListFragment m中无法访问初始化的静态变量。

修改1

我的主要活动

submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            GenerateXml gx=new GenerateXml();
            String requestXml=gx.generateXml(tv1.getText().toString(),tv2.getText().toString(),tv3.getText().toString(),tv4.getText().toString());
            myNewTask = new MyTask(requestXml);
            myNewTask.execute();
            Intent intent=new Intent(getApplicationContext(),TabActivity.class);
            startActivity(intent);
        }
    });

在我的AsyncTask doInBackground

MainActivity.responseXml=responseXML;

创建标签的活动

public class TabActivity extends Activity {
    MyTask myNewTask;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        actionBar.setDisplayShowTitleEnabled(true);

        /** Creating All Tab */
        Tab tab = actionBar.newTab()
                .setText("All")
                .setTabListener(new CustomTabListener<AllMsgFragment>(this, "All", AllMsgFragment.class));
        //.setIcon(R.drawable.android);

        actionBar.addTab(tab);


        /** Creating Success Tab */
        tab = actionBar.newTab()
                .setText("Success")
                .setTabListener(new CustomTabListener<SuccessMsgFragment>(this, "Success", SuccessMsgFragment.class));
        //.setIcon(R.drawable.apple);


        actionBar.addTab(tab);   

        /** Creating Error Tab */
        tab = actionBar.newTab()
                .setText("Error")
                .setTabListener(new CustomTabListener<ErrorMsgFragment>(this, "error", ErrorMsgFragment.class));
        //.setIcon(R.drawable.apple);

        actionBar.addTab(tab);
    }
}

我的第一个ListFragment:

public class AllMsgFragment extends ListFragment {
    public static String response;
    public ArrayList<HashMap<String, String>> msgDetails;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        response=MainActivity.responseXml;
        XmlToArrayList xmlArray=new XmlToArrayList();
        try {
            msgDetails=xmlArray.arrayListXml(response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        ListAdapter adapter = new SimpleAdapter(getActivity(), msgDetails,
                R.layout.msg_preview,
                new String[] { "ObSystem", "ObName", "Msgstate"}, new int[] {
            R.id.bs, R.id.si, R.id.msgStatus});
        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e("first","1");
        /** Setting the multiselect choice mode for the listview */
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);     
    }
}

0 个答案:

没有答案