0
public class Test2 extends Activity {

private ListView lv;
private ImageView logout,leftclosed,rightclosed,back,leftclosedadmin,rightclosedadmin,home;
private JSONObject json;
private ArrayList<Object> Item;
private Context mContext;
private SharedPreferences mSharedPreferencesWrite;
private CommonClass mCommonClass = new CommonClass();
public String category_name,category_id,product_id,product_name;
public StockAdapter adapter;
public ActivityBean bean;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.xstockdetails);
    lv=(ListView)findViewById(R.id.stockdetails_listview);
    lv.setVerticalFadingEdgeEnabled(false);
    Item=new ArrayList<Object>();

    class AsyncLogin extends AsyncTask<Void, Void, String>
    {
        ProcessDialog dialog = null;
        public String doInBackground(Void... params) 
        {
            try {
                String strURL = mContext.getResources().getString(
                        R.string.STOCK_DETAILS);
                ArrayList<NameValuePair> mNameValuePair = new ArrayList<NameValuePair>();
                mNameValuePair
                        .add(new BasicNameValuePair("action", "getstock"));
                mNameValuePair.add(new BasicNameValuePair("collection_by",
                        mSharedPreferencesWrite
                                .getString(Constant.LOGIN_SHARED.LS_USERID, "")
                                .toString().trim()));
                String result = mCommonClass.PostConnection(strURL,
                        mNameValuePair);
                if (result != null) {

                    try {
                        JSONObject jsonstockdetails = new JSONObject(result);
                        if (jsonstockdetails.getString(
                                Constant.COMMON_JSON.TYPE).equals(
                                Constant.COMMON_JSON.OK)) {
                            JSONObject jsonReturnResult = jsonstockdetails
                                    .getJSONObject("ReturnResult");
                            // Log.i("Result", "got result" + jsonReturnResult);
                            // Log.i(Constant.TAG,"Return Result :--- "+jsonReturnResult.names().toString());
                            JSONArray jsonArray = jsonReturnResult.names();

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonOBJName = jsonReturnResult
                                        .getJSONObject(jsonArray.getString(i)
                                                .toString());
                                Log.i("jsonOBJName", "mil gaye in tesing"
                                        + jsonOBJName);
                                category_id = jsonOBJName
                                        .getString("category_id");
                                category_name = jsonOBJName
                                        .getString("category_name");
                                product_id = jsonOBJName
                                        .getString("product_id");
                                product_name = jsonOBJName
                                        .getString("product_name");
                                Log.i("categories", "in testing!!"
                                        + category_id + category_name
                                        + product_id);

                                AddObjectToList(category_id,category_name,product_id,product_name);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return result;
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }


            return null;
        }


        protected void onPostExecute(String result) 
        {
            dialog.cancel();
            adapter=new StockAdapter(Test2.this,Item);
            lv.setAdapter(adapter);

        }


        protected void onPreExecute() 
        {
            super.onPreExecute();
            /*dialog.setMessage("Loading....Please Wait");
            dialog.setCancelable(true);
            dialog.show();*/
            dialog = new ProcessDialog(getApplicationContext(), "Loading..", "");

        }
    }
    if (mCommonClass.CheckNetwork(this)) {
        AsyncLogin al = new AsyncLogin();
        al.execute();
    }else{
        Log.i("Alert!!!", "Server not Found");
    }
}
private void AddObjectToList(String category_id,String category_name,String product_id,String product_name)
{

    bean =new ActivityBean(); 
    bean.setCategoryid(category_id);
    bean.setCategory_name(category_name);
    bean.setProductId(product_id);
    bean.setProductName(product_name);

    Item.add(bean);
}
public class StockAdapter extends BaseAdapter{

    ArrayList<Object> Aitem;
    List<ActivityBean> yourList = null;
    Activity context;
    LayoutInflater li;

    public StockAdapter(Activity parent,ArrayList<Object> item){
        this.context = parent;
        this.Aitem = item;
        this.li=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    class ViewHolder
       {
        Button b1,b3,btndelete;
        TextView tv0,tv1,tv2,tv3;

       }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder hv;
        if(convertView==null)
        {
            hv=new ViewHolder();
            convertView=li.inflate(R.layout.xstockdetailsadapter,null);

            hv.tv0 = (TextView) convertView.findViewById(R.id.categoryname_txt);
            hv.tv1 = (TextView) convertView.findViewById(R.id.textView1);
            hv.tv2 = (TextView) convertView.findViewById(R.id.textView2);
            hv.tv3 = (TextView) convertView.findViewById(R.id.textView3);

            convertView.setTag(hv);
        }else {
            hv=(ViewHolder)convertView.getTag();
            final ActivityBean ab = (ActivityBean) Aitem.get(position);

            hv.tv0.setText(ab.getCategory_name());
            hv.tv1.setText(ab.getCategoryid());
            hv.tv2.setText(ab.getProductId());
            hv.tv3.setText(ab.getProductName());

        }
        return convertView;
    }

}
}

我使用此代码使用 Asynctask 在列表视图中获取数据,但出现以下错误,谁能帮助我?{

08-09 11:59:34.174: E/AndroidRuntime(6180): FATAL EXCEPTION: main
08-09 11:59:34.174: E/AndroidRuntime(6180): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ovte.letsrecycle/com.ovte.letsrecycle.Test2}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.os.Looper.loop(Looper.java:137)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at java.lang.reflect.Method.invoke(Method.java:511)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at dalvik.system.NativeStart.main(Native Method)
08-09 11:59:34.174: E/AndroidRuntime(6180): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.app.Dialog.show(Dialog.java:281)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at com.ovte.letsrecycle.ProcessDialog.<init>(ProcessDialog.java:19)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at com.ovte.letsrecycle.Test2$1AsyncLogin.onPreExecute(Test2.java:143)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.os.AsyncTask.execute(AsyncTask.java:534)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at com.ovte.letsrecycle.Test2.onCreate(Test2.java:149)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.app.Activity.performCreate(Activity.java:5104)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-09 11:59:34.174: E/AndroidRuntime(6180):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-09 11:59:34.174: E/AndroidRuntime(6180):     ... 11 more

}

4

1 回答 1

0

尝试这个:

public void onPreExecute() 
    {
        super.onPreExecute();
        dialog = new ProcessDialog(ActivityName.this, "Loading..", "");
        dialog.setMessage("Loading....Please Wait");
        dialog.setCancelable(true);
        dialog.show();


    }
于 2013-08-09T12:27:10.597 回答