2016-01-27 17 views
1

我有一個listview,它在每個列表項目上打開一個新的單個itemview活動。在singleitemview活動中,我有一個收藏夾按鈕,它使用sharedpreferences將打開單項活動活動的列表項添加到我的收藏夾活動中。要通過listitem單擊,我使用jackson庫將相應的listitem轉換爲json字符串,並通過意圖將putextra用於單個視圖活動。然後,我轉換回JSON字符串到ListItem對象在singleitemview並用它來添加到收藏夾在單個項目視圖活動中按下收藏夾按鈕時的空指針異常

但現在當我點擊singleitemview應用程序崩潰並重新打開應用程序的listutem添加到我的收藏夾活動

後的收藏夾按鈕

這裏是我的代碼清單活動

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, 
         long id) { 

          ObjectMapper mapper = new ObjectMapper(); 
          Product pro = productListAdapter.getItem(position); 
    try 
    { 
     String jsonInString = mapper.writeValueAsString(pro); 
     Intent intent = new Intent(activity.getApplicationContext(), SingleItemView.class); 
     intent.putExtra("selected item", jsonInString); 

     startActivity(intent); 
    } 
    catch (JsonProcessingException e) 
    {//something went wrong 
      } 



} 

singleitemview的

onitemclicklistener的.java

public class SingleItemView extends Activity 
{ 
ProductListAdapter padaptr; 
SharedPreference sharedPreference; 

List<Product> products = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    // TODO: Implement this method 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.singleitem); 
    sharedPreference = new SharedPreference(); 
    padaptr = new ProductListAdapter(SingleItemView.this, products); 



    Button btn = (Button) findViewById(R.id.singleitemButton1); 
    btn.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v){ 

      Bundle extras = getIntent().getExtras(); 
      String jsonObj = extras.getString("selected item"); 
      ObjectMapper mapper = new ObjectMapper(); 

      try 
      { 
       Product pro = mapper.readValue(jsonObj, Product.class); 

         //the fav image present on evry list item 
       ImageView button = (ImageView) findViewById(R.id.imgbtn_favorite); 
       if (checkFavoriteItem(pro)) { 

        sharedPreference.removeFavorite(SingleItemView.this, pro); 
        button.setTag("no"); 
        button.setImageResource(R.drawable.heart_grey); 
        Toast.makeText(SingleItemView.this, 
            SingleItemView.this.getResources().getString(R.string.remove_favr), 
            Toast.LENGTH_SHORT).show(); 
       } else { 
        sharedPreference.addFavorite(SingleItemView.this, pro); 
        Toast.makeText(SingleItemView.this, 
            SingleItemView.this.getResources().getString(R.string.add_favr), 
            Toast.LENGTH_SHORT).show(); 

        button.setTag("yes"); 
        button.setImageResource(R.drawable.heart_red); 
       } 
      } 
      catch (IOException e) 
      {}; 





     } 



      private boolean checkFavoriteItem(Product checkProduct) { 
       boolean check = false; 
       List<Product> favorites = sharedPreference.getFavorites(getApplicationContext()); 
       if (favorites != null) { 
        for (Product product : favorites) { 
         if (product.equals(checkProduct)) { 
          check = true; 
          break; 
         } 
        } 
       } 
       return check; 
      } 
    }); 
    } 


} 

線指向空指針eception日誌貓

     button.setTag("yes"); 

singleitem.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#A25550" 
    android:gravity="center"> 

    <Button 
     android:layout_height="wrap_content" 
     android:text="Addcto fav" 
     android:layout_width="wrap_content" 
     android:id="@+id/singleitemButton1"/> 

    <TextView 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_width="wrap_content" 
     android:id="@+id/singleitemTextView1"/> 

</LinearLayout> 

列表項XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@color/product_list_item_bg" 
android:descendantFocusability="blocksDescendants" > 

<RelativeLayout 
    android:id="@+id/pdt_layout_item" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/txt_pdt_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="6dp" /> 

    <TextView 
     android:id="@+id/txt_pdt_price" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txt_pdt_name" 
     android:padding="6dp" /> 

    <TextView 
     android:id="@+id/txt_pdt_desc" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txt_pdt_price" 
     android:padding="6dp" /> 

    <ImageView 
     android:id="@+id/imgbtn_favorite" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/txt_pdt_desc" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="3dp" 
     android:background="@null" 
     android:contentDescription="@string/favorites" /> 
</RelativeLayout> 

<View 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:layout_below="@+id/pdt_layout_item" 
    android:background="@color/view_divider_color" /> 

    </RelativeLayout> 

我logcat的

01-27 17:29:19.777 14852 14852 E AndroidRuntime FATAL EXCEPTION: main 
01-27 17:29:19.777 14852 14852 E AndroidRuntime java.lang.NullPointerException 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at com.mycompany.myapp.SingleItemView$100000000.onClick(SingleItemView.java:62) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.view.View.performClick(View.java:4452) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.widget.Button.performClick(Button.java:148) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.view.View$PerformClick.run(View.java:18428) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.os.Handler.handleCallback(Handler.java:725) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:92) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.os.Looper.loop(Looper.java:176) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5365) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:511) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
01-27 17:29:19.777 14852 14852 E AndroidRuntime at dalvik.system.NativeStart.main(Native Method) 
01-27 17:29:20.316 17044 17044 D AndroidRuntime Calling main entry com.android.commands.am.Am 
+0

is imagebutton in singleitem.xml file? – Piyush

+0

那個imagebutton id是否存在於singleitemview活動的xml文件中?因爲你的錯誤是在初始化Button變量。 –

+0

@PiyushGupta不在列表項目 – user5524159

回答

1

原因: 你收到此NullPointerException的原因是您試圖訪問一個id爲imgbtn_favoritesinglelistitem.xml它不存在。 只能在與Activity類關聯的xml中訪問變量。

解決方案:我注意到您只是使用該變量來設置Tag並更改ImageResource。你不能從另一個活動做到這一點。爲此,您既可以

  1. 存放在SharedPreferences全球價值,並顯示更改後的值時,listview再次打開。

  2. 或者,如果您不需要來打開一個新的活動,您可以訪問ListView活動本身中的視圖。

編輯:

  • 您還可以使用startActivityForResult讓你發送一些值回的ListView活動然後使變量的變化。舉例來說,您可以參考this link
  • -1

    您需要移動這條線:

    ImageView button = (ImageView) v.findViewById(R.id.imgbtn_favorite); 
    

    出你嘗試捕捉的(僅此塊之前把它)。你還沒有在你的else語句中初始化。

    0

    更換

    ImageView button = (ImageView) findViewById(R.id.imgbtn_favorite); 
    

    通過所有的

    ImageView button = (ImageView) v.findViewById(R.id.imgbtn_favorite); 
    
    +0

    如你所說,但仍然是相同的問題 – user5524159

    +1

    你能告訴我們這兩個xml代碼嗎? –

    +0

    yah現在就可以做到 – user5524159

    1

    首先在點擊事件初始化鑑於findViewById是不是一個好方法。在點擊事件之外初始化它。

    第二個您的Button沒有正確初始化,這就是爲什麼它變爲空。

    +0

    非常感謝那個建議所以我初始化按鈕之外的點擊事件,但仍然是同樣的問題。我應該如何解決你的問題 – user5524159

    +0

    你可以發佈你的singleitem.xml文件嗎?在那個XML文件是否有任何id存在「imgbtn_favorite」? – Sayem

    +0

    圖像按鈕存在於列表視圖的列表項中不在單個項目視圖中 – user5524159

    相關問題