0

我有listview,當它長按listitem時,使用共享偏好將listitem添加到收藏夾。當物品被點擊時,它會打開一個新的活動。該活動包含一個最喜歡的按鈕,該按鈕應該執行與該項目被長時間點擊相同的功能,即添加將活動打開到收藏夾的listitem。爲此,我通過intent在item click listener方法中傳遞listitem的位置。這是我試過的。按鈕在列表視圖上的空指針異常

onitemclicklistener我的主要活動

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

    Intent intent = new Intent(activity.getApplicationContext(), SingleItemView.class); 
     intent.putExtra("item position", position); 
    startActivity(intent); 
} 

singleitemview.java

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

List<Product> products; 

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



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

      Bundle extras = getIntent().getExtras(); 
      int posc= extras.getInt("item position"); 
      Product product = (Product) padaptr.getItem(posc); 
      ImageView button = (ImageView) findViewById(R.id.imgbtn_favorite); 

      if (checkFavoriteItem(product)) { 

       button.setImageResource(R.drawable.heart_red); 
       button.setTag("red"); 
      } else { 
       button.setImageResource(R.drawable.heart_grey); 
       button.setTag("grey"); 
      } 
     } 

      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; 
      } 
    }); 
    } 


    } 

現在,當我按下按鈕的應用程序崩潰在該行給出的logcat的空指針異常

Product product = (Product) padaptr.getItem(posc); 

logcat的

01-26 19:43:23.687 21810 21810 E AndroidRuntime        FATAL EXCEPTION: main 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        java.lang.NullPointerException 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at com.mycompany.myapp.SingleItemView$100000000.onClick(SingleItemView.java:39) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at android.view.View.performClick(View.java:4452) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at android.widget.Button.performClick(Button.java:148) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at android.view.View$PerformClick.run(View.java:18428) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at android.os.Handler.handleCallback(Handler.java:725) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at android.os.Handler.dispatchMessage(Handler.java:92) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at android.os.Looper.loop(Looper.java:176) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at android.app.ActivityThread.main(ActivityThread.java:5365) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at java.lang.reflect.Method.invokeNative(Native Method) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at java.lang.reflect.Method.invoke(Method.java:511) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
01-26 19:43:23.687 21810 21810 E AndroidRuntime        at dalvik.system.NativeStart.main(Native Method) 
+0

ProductListAdapter padaptr;永遠不會初始化?而且,難道你只是通過意圖傳遞這個項目嗎? –

回答

1

的logcat的有原因。 「padaptr」爲空。

也許解決方案可能是通過完整的對象通過活動。

這裏的教程:

http://www.101apps.co.za/index.php/articles/passing-objects-between-activities.html

但是,我用我自己的方法。我使用傑克遜圖書館。

我將對象轉換爲Json字符串,並將對象作爲字符串在extras中傳遞,並在新活動中將Json轉換爲對象。

這裏的教程:

http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

祝您好運!

+0

Thx兄弟我會看看它並嘗試它 – user5524159