4

当我试图从另一个 Api 类调用主要活动的方法“刷新”时,该方法被调用并且它还显示了一些致命错误。并且它没有更改适配器值。任何人都可以给出任何想法来清除它。 ?

package com.example.hotspot;

import com.example.hotspot.HotspotApi;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TextView;

public class HotSpot extends Activity {
    TextView textview;
    ListView listview;
    HotspotAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.hot_spot);
        textview = (TextView) findViewById(R.id.textView1);
        listview = (ListView) findViewById(R.id.listView1);
        adapter = new HotspotAdapter(this);
        listview.setAdapter(adapter);
        new HotspotApi(adapter).execute();

    }

    public void refresh() {

        System.out.println("refresh() is called");
        adapter.notifyDataSetChanged();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.hot_spot, menu);
        return true;
    }

}

热点.java

package com.example.hotspot;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.hotspot.HotspotModel;
import com.example.hotspot.HotspotAdapter;

import android.os.AsyncTask;

public class HotspotApi extends AsyncTask<Void, Integer, Void> implements
        Icommon {

    public Boolean IsServerErr = false;
    private JSONArray response_array;
    String url = "some url";
    HotspotAdapter adapter;
    HotSpot hot;

    public HotspotApi(HotspotAdapter adapter) {

        this.adapter = adapter;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        getresult();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub

        hot=new HotSpot();
        hot.refresh();
        super.onPostExecute(result);

    }

    void getresult() {

        InternetManager manager = new InternetManager(url);
        String category_jsonresponse = manager.URLRequest();
        if (!manager.IsServerConn) {
            IsServerErr = true;
        }
        if (category_jsonresponse != null) {
            System.out.println("Hotspot_jsonresponse" + category_jsonresponse);
            try {
                response_array = new JSONArray(category_jsonresponse);
                for (int i = 1; i < response_array.length(); i++) {
                    JSONObject image_object = response_array.getJSONObject(i);
                    HotspotModel h = new HotspotModel();
                    h.setId(image_object.getString("id") == null ? ""
                            : image_object.getString("id"));
                    h.setContent(image_object.getString("content") == null ? ""
                            : image_object.getString("content"));
                    h.setImg(image_object.getString("img") == null ? ""
                            : image_object.getString("img"));
                    h.setName(image_object.getString("name") == null ? ""
                            : image_object.getString("name"));
                    arraylist.add(h);


                }
                System.out.println("HotspotModelsize() is " + arraylist.size());

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

}

热点适配器.java

package com.example.hotspot;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HotspotAdapter extends BaseAdapter implements Icommon{
    private TextView textview;
    private View view;
    ImageView imageview;
    private LayoutInflater inflater;

    public HotspotAdapter(Context context ){
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arraylist.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arraylist.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {

        if (arg1 == null) {
            view = inflater.inflate(R.layout.custom_layout, null);
        } else {
            view = arg1;
        }

        textview = (TextView) view.findViewById(R.id.txt_content);
        textview.setText(arraylist.get(arg0).getName());




        return view;
    }

}
4

2 回答 2

1

热=新热点();?? 你不能那样用!HotSpot 是一个activity,应该被Framework 调用,例如activitymanager。或者使用 startActivity() 来显示一个活动。

刷新方法 (adapter.notifyDataSetChanged();) 将导致 UI 刷新。但是,hot = new HotSpot() 不会调用 onCreated() 方法,这意味着 UI 没有创建。所以它肯定会导致致命错误。

我永远不会看到有人用 new 运算符调用 Activity。

您应该参考有关如何使用活动和适配器的通用流程。

于 2013-04-17T09:26:17.627 回答
1

在您的HotSpotApi班级中,您正在创建一个新HotSpot活动,这似乎是错误的。我猜你是从互联网上获取 json 数据并将其加载到listview.

解决方案:

在 HotspotApi 中更改以下而不是调用活动方法:

@Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
       super.onPostExecute(result);
       adapter.notfiyDatasetChanged();
    }

希望这会帮助你。

于 2013-04-17T09:26:42.093 回答