2013-07-01 40 views
1

顯示列表行中的按鈕,並希望允許用戶使用該按鈕將列表項添加到購物車,但每當我加載列表獲取錯誤不幸的應用程序已停止,如果我不在我的代碼沒有得到任何錯誤使用按鈕空指針異常按鈕setOnClickListener(新的OnClickListener()

所以在這裏,我的問題是什麼是錯在我的代碼,而我試圖用按鈕的工作

這是行,在那裏我得到的錯誤。?:

mImgAddCart.setOnClickListener(new OnClickListener() { 

logcat的:

07-01 05:57:03.733: E/AndroidRuntime(779): FATAL EXCEPTION: main 
07-01 05:57:03.733: E/AndroidRuntime(779): java.lang.NullPointerException 
07-01 05:57:03.733: E/AndroidRuntime(779): at com.example.sample.ItemsActivity$MyAsyncTask.onPostExecute(ItemsActivity.java:191) 
07-01 05:57:03.733: E/AndroidRuntime(779): at com.example.sample.ItemsActivity$MyAsyncTask.onPostExecute(ItemsActivity.java:1) 
07-01 05:57:03.733: E/AndroidRuntime(779): at android.os.AsyncTask.finish(AsyncTask.java:631) 
07-01 05:57:03.733: E/AndroidRuntime(779): at android.os.AsyncTask.access$600(AsyncTask.java:177) 
07-01 05:57:03.733: E/AndroidRuntime(779): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
07-01 05:57:03.733: E/AndroidRuntime(779): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-01 05:57:03.733: E/AndroidRuntime(779): at android.os.Looper.loop(Looper.java:137) 
07-01 05:57:03.733: E/AndroidRuntime(779): at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-01 05:57:03.733: E/AndroidRuntime(779): at java.lang.reflect.Method.invokeNative(Native Method) 
07-01 05:57:03.733: E/AndroidRuntime(779): at java.lang.reflect.Method.invoke(Method.java:511) 
07-01 05:57:03.733: E/AndroidRuntime(779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-01 05:57:03.733: E/AndroidRuntime(779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-01 05:57:03.733: E/AndroidRuntime(779): at dalvik.system.NativeStart.main(Native Method) 
07-01 05:57:07.172: E/Trace(806): error opening trace file: No such file or directory (2) 

LazyAdapter.java:

 public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 
    String itemTitle, itemCost; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.listrow_items, null); 

     final TextView title = (TextView)vi.findViewById(R.id.title); 
     final TextView cost = (TextView)vi.findViewById(R.id.cost); 
     ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); 


     HashMap<String, String> item = new HashMap<String, String>(); 
     item = data.get(position); 

     // Setting all values in listview 
     title.setText(item.get(ItemsActivity.KEY_TITLE)); 
     cost.setText(item.get(ItemsActivity.KEY_COST)); 
     imageLoader.DisplayImage(item.get(com.example.sample.ItemsActivity.KEY_THUMB_URL), thumb_image); 

     //Button mImgAddCart 
     Button mImgAddCart = (Button) vi.findViewById(R.id.btn_add_to_order); 
      mImgAddCart.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        itemTitle = title.getText().toString(); 
        itemCost = cost.getText().toString();     

        if (Constants.sItem_Detail.size() <= 0) { 
         HashMap<String, String> sTempObj = new HashMap<String, String>(); 
         sTempObj.put(KEY_TITLE, itemTitle); 
         sTempObj.put(KEY_COST, itemCost); 
         Constants.sItem_Detail.add(sTempObj);    
         }     

        AlertDialog.Builder alertdialog = new AlertDialog.Builder(
          ItemsActivity.this); 
        alertdialog.setTitle(getResources() 
          .getString(R.string.app_name)); 
        alertdialog.setMessage("Item Added to Cart"); 

        alertdialog.setPositiveButton("OK", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int which) 
           { 
            activity.finish(); 
           } 
          });  
        alertdialog.show();    
       } 
      }); 

     return vi; 
    } 
} 

線:

sTempObj.put(KEY_TITLE, itemTitle); 

獲取:

KEY_TITLE cannot be resolved to a variable 

線:

AlertDialog.Builder alertdialog = new AlertDialog.Builder(ItemsActivity.this); 

獲取:

No enclosing instance of the type ItemsActivity is accessible in scope 

線:

alertdialog.setTitle(getResources().getString(R.string.app_name)); 

獲取:

The method getResources() is undefined for the type new View.OnClickListener(){} 
+1

使用活動上下文中'初始化ListView和Button實例onPostExecute',而不是'MyAsyncTask'背景和確保你正在爲'ItemsActivity'設置正確的佈局你的asynctask是一個活動的內部類或者一個seperata .java文件嗎?你在哪裏有ListView和Button的xml –

+0

? – Raghunandan

+0

mImgAddCart.setOnClickListener(新View.OnClickListener(){} –

回答

3

LazyAdapter類的代碼應該是這樣的:

public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 
    String itemTitle, itemCost; 
    TextView title, cost; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.listrow_items, null); 

     title = (TextView)vi.findViewById(R.id.title); 
     cost = (TextView)vi.findViewById(R.id.cost); 
     ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); 


     HashMap<String, String> item = new HashMap<String, String>(); 
     item = data.get(position); 

     // Setting all values in listview 
     title.setText(item.get(ItemsActivity.KEY_TITLE)); 
     cost.setText(item.get(ItemsActivity.KEY_COST)); 
     imageLoader.DisplayImage(item.get(com.example.sample.ItemsActivity.KEY_THUMB_URL), thumb_image); 

     //Button mImgAddCart 
     Button mImgAddCart = (Button) vi.findViewById(R.id.btn_add_to_order); 
      mImgAddCart.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        itemTitle = title.getText().toString(); 
        itemCost = cost.getText().toString();     

        if (Constants.sItem_Detail.size() <= 0) { 
         HashMap<String, String> sTempObj = new HashMap<String, String>(); 
         sTempObj.put(com.example.sample.ItemsActivity.KEY_TITLE, itemTitle); 
         sTempObj.put(com.example.sample.ItemsActivity.KEY_COST, itemCost); 
         Constants.sItem_Detail.add(sTempObj);    
         }     

     return vi; 
    } 
} 
+0

謝謝@klamitsuri – Sneha

1

如果使用「btn_add_to_order」在listrow_items.xml按鈕,然後聽衆的按鈕應該是在你的「LazyAdapter」級外觀@代碼波紋管

public class LazyAdapter extends BaseAdapter { 

      private Activity activity; 
      private ArrayList<HashMap<String, String>> data; 
      private static LayoutInflater inflater=null; 
      public ImageLoader imageLoader; 

      public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
       activity = a; 
       data=d; 
       inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       imageLoader=new ImageLoader(activity.getApplicationContext()); 
      } 

      public int getCount() { 
       return data.size(); 
      } 

      public Object getItem(int position) { 
       return position; 
      } 

      public long getItemId(int position) { 
       return position; 
      } 

      public View getView(int position, View convertView, ViewGroup parent) { 
       View vi=convertView; 
       if(convertView==null) 
        vi = inflater.inflate(R.layout.listrow_items, null); 

       TextView title = (TextView)vi.findViewById(R.id.title); 
       TextView cost = (TextView)vi.findViewById(R.id.cost); 
       ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); 


       HashMap<String, String> item = new HashMap<String, String>(); 
       item = data.get(position); 

       // Setting all values in listview 
       title.setText(item.get(ItemsActivity.KEY_TITLE)); 
       cost.setText(item.get(ItemsActivity.KEY_COST)); 
       imageLoader.DisplayImage(item.get(com.example.sample.ItemsActivity.KEY_THUMB_URL), thumb_image); 

       //Button mImgAddCart 
       Button mImgAddCart = (Button) vi.findViewById(R.id.btn_add_to_order); 
        mImgAddCart.setOnClickListener(new OnClickListener() { 

         public void onClick(View v) { 
          // TODO Auto-generated method stub 
          itemTitle = txt_title.getText().toString(); 
          itemCost = text_cost.getText().toString();     

          if (Constants.sItem_Detail.size() <= 0) { 
           HashMap<String, String> sTempObj = new HashMap<String, String>(); 
           sTempObj.put(KEY_TITLE, itemTitle); 
           sTempObj.put(KEY_COST, itemCost); 
           Constants.sItem_Detail.add(sTempObj);    
           }     

          AlertDialog.Builder alertdialog = new AlertDialog.Builder(
            ItemsActivity.this); 
          alertdialog.setTitle(getResources() 
            .getString(R.string.app_name)); 
          alertdialog.setMessage("Item Added to Cart"); 

          alertdialog.setPositiveButton("OK", 
            new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, 
               int which) 
             { 
              finish(); 
             } 
            });  
          alertdialog.show();    
         } 
        }); 

       return vi; 
      } 
     } 
+0

我已經發布了我的更新LazyAdapter代碼與幾個錯誤,請檢查現在 – Sneha

+1

我已經接受你的答案爲有用的謝謝 – Sneha

0

您的ItemsActivity課程中尚未初始化txt_titletext_cost

假設您在activity_item中有textviews。

 setContentView(R.layout.activity_item); 
     txt_title = (TextView) findViewById(R.id.title); 
     text_cost = (TextView) findViewById(R.id.cost); 
0

將getResources行更改爲使用活動上下文。還要在您的活動課程中將KEY_TITLE聲明爲公開。

AlertDialog.Builder alertdialog = new AlertDialog.Builder(
         a); 
       alertdialog.setTitle(a.getResources() 
         .getString(R.string.app_name)); 
       alertdialog.setMessage("Item Added to Cart"); 

       alertdialog.setPositiveButton("OK", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int which) 
          { 
           a.finish(); 
          } 
         });  
       alertdialog.show(); 
1

快速修復在LazyAdapter類當前的問題:

獲取:

KEY_TITLE cannot be resolved to a variable 

解決方案: 確保你已經在ItemsActivity

聲明KEY_TITLE作爲公共靜態字段

獲取:

類型ItemsActivity沒有外圍實例是在範圍訪問

AlertDialog.Builder alertdialog = new AlertDialog.Builder(ItemsActivity.this); 

解決方案:

AlertDialog.Builder alertdialog = new AlertDialog.Builder(activity); 

獲得:用於示出AlertDialog作爲 使用Activity上下文

alertdialog.setTitle(getResources().getString(R.string.app_name)); 

的方法getResources()是未定義的類型新View.OnClickListener(){} 解決方案: 使用Activity上下文的訪問資源爲:

alertdialog.setTitle(activity.getResources().getString(R.string.app_name)); 
+1

我接受你的答案爲有用的謝謝 – Sneha

1

你剛纔初始化您的第一個按鈕然後調用方法...

看看這可能會有所幫助。

public class LazyAdapter extends BaseAdapter { 

private Activity activity; 
private ArrayList<HashMap<String, String>> data; 
private static LayoutInflater inflater=null; 
public ImageLoader imageLoader; 
String itemTitle, itemCost; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
    activity = a; 
    data=d; 
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    imageLoader=new ImageLoader(activity.getApplicationContext()); 
} 

public int getCount() { 
    return data.size(); 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    View vi=convertView; 
    if(convertView==null) 
     vi = inflater.inflate(R.layout.listrow_items, null); 

    final TextView title = (TextView)vi.findViewById(R.id.title); 
    final TextView cost = (TextView)vi.findViewById(R.id.cost); 
    ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); 
    Button mImgAddCart = (Button) vi.findViewById(R.id.btn_add_to_order); 


    HashMap<String, String> item = new HashMap<String, String>(); 
    item = data.get(position); 

    // Setting all values in listview 
    title.setText(item.get(ItemsActivity.KEY_TITLE)); 
    cost.setText(item.get(ItemsActivity.KEY_COST)); 
    imageLoader.DisplayImage(item.get(com.example.sample.ItemsActivity.KEY_THUMB_URL), thumb_image); 

    //Button mImgAddCart 

     mImgAddCart.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       itemTitle = title.getText().toString(); 
       itemCost = cost.getText().toString();     

       if (Constants.sItem_Detail.size() <= 0) { 
        HashMap<String, String> sTempObj = new HashMap<String, String>(); 
        sTempObj.put(KEY_TITLE, itemTitle); 
        sTempObj.put(KEY_COST, itemCost); 
        Constants.sItem_Detail.add(sTempObj);    
        }     

       AlertDialog.Builder alertdialog = new AlertDialog.Builder(
         ItemsActivity.this); 
       alertdialog.setTitle(getResources() 
         .getString(R.string.app_name)); 
       alertdialog.setMessage("Item Added to Cart"); 

       alertdialog.setPositiveButton("OK", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int which) 
          { 
           activity.finish(); 
          } 
         });  
       alertdialog.show();    
      } 
     }); 

    return vi; 
} 

}