应用闪退问题:从json信息的解析开始就会闪退

竹畔溪语
  • 3
新手上路,请多包涵

想实现的功能是点击顶部按钮之后按关键字进行搜索,已经可以从服务器收到反馈的json信息,但从json信息的解析开始就会闪退,加载listview也不知道行不行

public abstract class loadlistview{

public ListView plv;
public String js;

public int listlength;
public int listvisit;
public int loadlength;

public void setloadlength(int i){
    this.loadlength=i;
}
public void setjs(String js){this.js=js;}
public void setplv(ListView plv){
    this.plv=plv;
}


public abstract void loadlv();
public abstract void addloadlv();

}

public class HomepageActivity extends AppCompatActivity {

public Button product;
public Button book;
public Button service;
public String weburl;
public String location;
public View loadmore;
public int last_index; 
public int total_index;
public jsonrequest jrq;
public loadproductlist newlpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homepage);
    product = (Button) findViewById(R.id.hp_btn_product);
    book = (Button)findViewById(R.id.hp_btn_book);
    service = (Button)findViewById(R.id.hp_btn_service);
    //loadmore= View.inflate(HomepageActivity.this,R.layout.layout_loadmore,null);
    // 设置刷新视图默认情况下是不可见的,并且不占空间
    //loadmore.setVisibility(View.GONE);

    weburl=this.getString(R.string.cfmheiaskin);
    location="keysearch.php";

    jrq=new jsonrequest();
    jrq.seturl(weburl);
    jrq.setLocation(location);

    newlpl=new loadproductlist();
    newlpl.setloadlength(6);
    newlpl.plv=(ListView) findViewById(R.id.hp_lv_pinfo);
    //newlpl.plv.addFooterView(loadmore,null,false);

    /*
    newlpl.plv.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {
            if(last_index == total_index && (i == AbsListView.OnScrollListener.SCROLL_STATE_IDLE))
            {
                //设置刷新界面可见
                loadmore.setVisibility(View.VISIBLE);
                //加载更多数据
                newlpl.addloadlv();
            }
        }

        @Override
        public void onScroll(AbsListView absListView, int i, int i1, int i2) {
            last_index = i+i1;
            total_index = i2;

        }
    });
    */

    product.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            jrq.setkey("product");
            jrq.imgbtnclick();
            newlpl.setjs(jrq.getresult());
            newlpl.getArray();
            //newlpl.loadlv();
        }
    });

    book.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            jrq.setkey("book");
            jrq.imgbtnclick();
            Toast.makeText(HomepageActivity.this,jrq.getresult(),Toast.LENGTH_SHORT).show();
            //newlpl.setjs(jrq.getresult());
            //newlpl.getArray();
            //newlpl.loadlv();
            //imgdownload idl=new imgdownload();
            //idl.setpath("proimg/phone.jpg",weburl);
            //idl.imgloading();

        }
    });

    service.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            jrq.setkey("service");
            jrq.imgbtnclick();
            Toast.makeText(HomepageActivity.this,jrq.getresult(),Toast.LENGTH_SHORT).show();
            //newlpl.setjs(jrq.getresult());
            //newlpl.getArray();
            //newlpl.loadlv();

        }
    });

}
class loadproductlist extends loadlistview{
    public plvadapter newadapter;
    public proinfo[] pia;
    public List<proinfo> pil;


    public void getArray(){
        final Handler handler=new Handler(){
                @Override
        public void handleMessage(Message msg){
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            if (msg.what == 1) {
                
                pia=(proinfo[]) msg.obj;
                listlength=pia.length;
                pil=new ArrayList<>(listlength);
            }
        }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                String jstring=js;
                if(jstring==null){
                    Toast.makeText(HomepageActivity.this,"数据加载中",Toast.LENGTH_SHORT).show();
                }else{
                    try {
                        JSONArray jarray=new JSONArray(jstring);
                        JSONObject jo;
                        int j=jarray.length();
                        proinfo[] newarray=new proinfo[j];
                        proinfo pi=new proinfo();
                        int i=0;
                        while(i<j){
                            jo=(JSONObject) jarray.get(i);
                            pi.name=jo.getString("p_name");
                            pi.price=jo.getString("price");
                            pi.issuer=jo.getString("p_sellor");
                            pi.imgurl=jo.getString("imgurl");
                            newarray[i]=pi;
                            i=i+1;
                        }
                        Message msg=new Message();
                        msg.obj=newarray;//传递的消息
                        msg.what=1;
                        handler.sendMessage(msg);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    public void loadlv() {

                for(listvisit=0;listvisit<listlength&&listvisit<loadlength;listvisit+=1){
                    pil.add(pia[listvisit]);
                }
                if(listvisit!=0){
                    newadapter= new plvadapter(HomepageActivity.this,pil);
                    plv.setAdapter(newadapter);
                }else{
                    Toast.makeText(HomepageActivity.this,"数据加载失败",Toast.LENGTH_SHORT);
                }


    }

    @Override
    public void addloadlv() {
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {

                        try {
                            //模拟耗时操作
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                for(int j=listvisit;listvisit<listlength&&listvisit<j+loadlength;listvisit+=1){
                                    pil.add(pia[listvisit]);
                                }
                                if(listvisit!=0){
                                    newadapter.updateView(pil);
                                    //加载数据结束后使刷新界面不可见
                                    loadmore.setVisibility(View.GONE);
                                }else{
                                    Toast.makeText(HomepageActivity.this,"数据加载失败",Toast.LENGTH_SHORT);
                                }
                            }
                        });


                    }
                }).start();
    }


}

public class plvadapter extends BaseAdapter{
    List<proinfo> tlist;
    LayoutInflater inflater;
    imgdownload idl;

    public plvadapter(Context context, List<proinfo> list) {
        this.tlist=list;
        this.inflater=LayoutInflater.from(context);
        this.idl=new imgdownload();
    }

    @Override
    public int getCount() {
        return tlist.size();
    }

    @Override
    public Object getItem(int i) {
        return tlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public void updateView(List<proinfo> nowList){
        this.tlist=nowList;
        this.notifyDataSetChanged(); //强制动态刷新数据进而调用getView方法
    }
    //@SuppressLint("InflateParams")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHoder;
        if(view ==null){
            viewHoder=new ViewHolder();
            view=inflater.inflate(R.layout.layout_productbox,null);
            viewHoder.img= view.findViewById(R.id.pbox_img_pro);
            viewHoder.name= view.findViewById(R.id.pbox_txv_name);
            viewHoder.price= view.findViewById(R.id.pbox_txv_price);
            viewHoder.issuer= view.findViewById(R.id.pbox_txv_issuer);
            view.setTag(viewHoder);

        }else {
            viewHoder=(ViewHolder)view.getTag();
        }
        idl.setpath(tlist.get(i).imgurl,weburl);
        idl.imgloading();
        viewHoder.img.setImageBitmap(idl.bmap);
        viewHoder.name.setText(tlist.get(i).name);
        viewHoder.price.setText(tlist.get(i).price);
        viewHoder.issuer.setText(tlist.get(i).issuer);
        return view;
    }
    }
    class  ViewHolder{
        ImageView img;
        TextView name;
        TextView price;
        TextView issuer;
    }
}
回复
阅读 1.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏