我有一個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
is imagebutton in singleitem.xml file? – Piyush
那個imagebutton id是否存在於singleitemview活動的xml文件中?因爲你的錯誤是在初始化Button變量。 –
@PiyushGupta不在列表項目 – user5524159