我有listview,当长按listitem时,使用共享首选项将listitem添加到收藏夹。单击该项目时,将打开一个新活动。该活动包含一个喜欢的按钮,该按钮应该与长期点击的项目具有相同的功能,即添加打开活动的listitem到收藏夹。为此,我通过项目点击监听器方法的意图传递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 :(得分:1)
logcat有原因。 “padaptr”为空。
也许解决方案可以通过活动传递整个对象。
这是一个教程:
http://www.101apps.co.za/index.php/articles/passing-objects-between-activities.html
但是,我使用自己的方法。我使用杰克逊图书馆。
我将对象转换为Json String,并在对象中将对象作为字符串传递,在新活动中将Json转换为对象。
这是一个教程:
http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
祝你好运!