这是我的 listviewite.xml ,具有以下结构,当应用程序运行并且它到达getFavourite()时它强制关闭。我想从任何援助中知道原因。我认为问题出在xml结构中,是吗?帮助
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="left|center"
android:paddingBottom="5px"
android:paddingLeft="5px"
android:paddingTop="5px" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/avatar"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_marginRight="6dip"
android:visibility="gone" />
<ImageView
android:id="@+id/imgbtn_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignBottom="@+id/avatar"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:indeterminate="true"
android:maxHeight="30dip"
android:maxWidth="30dip"
android:minHeight="30dip"
android:minWidth="30dip"
android:contentDescription="@string/favorites" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="6dip"
android:indeterminate="true"
android:maxHeight="30dip"
android:maxWidth="30dip"
android:minHeight="30dip"
android:minWidth="30dip" />
</RelativeLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textColor="#0099CC" />
</LinearLayout>
</LinearLayout>
这是下面的 getFavourite()方法:
imageManager.displayImage(tweet.image_url, activity, holder.image,holder.progress); // CHANGED
/*If a product exists in shared preferences then set heart_red drawable
* and set a tag*/
if (checkFavoriteItem(tweet)) {
holder.favoriteImg.setImageResource(R.drawable.heart_red);
holder.favoriteImg.setTag("red");
} else {
holder.favoriteImg.setImageResource(R.drawable.heart_grey);
holder.favoriteImg.setTag("grey");
}
}
return v;
}
/* Checks whether a particular product exists in SharedPreferences */
public boolean checkFavoriteItem(Tweet checkTweet) {
boolean check = false;
List<Tweet> favorites = sharedPreference.getFavorites(context);
if (favorites != null) {
for (Tweet tweet : favorites) {
if (tweet.equals(checkTweet)) {
check = true;
break;
}
}
}
return check;
}
public ArrayList<Tweet> getFavorites(Context context) {
SharedPreferences settings;
List<Tweet> favorites;
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
if (settings.contains(FAVORITES)) {
String jsonFavorites = settings.getString(FAVORITES, null);
Gson gson = new Gson();
Tweet[] favoriteItems = gson.fromJson(jsonFavorites, Tweet[].class);
favorites = Arrays.asList(favoriteItems);
favorites = new ArrayList<Tweet>(favorites);
} else
return null;
return (ArrayList<Tweet>) favorites;
}
这是适配器代码:请求
public class TweetItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
private Activity activity;
private Context context;
public ImageManager imageManager;
public SharedPreference sharedPreference;
public TweetItemAdapter(Activity a, int textViewResourceId, ArrayList<Tweet> tweets) {
super(a, textViewResourceId, tweets);
this.tweets = tweets;
activity = a;
sharedPreference = new SharedPreference();
imageManager = new ImageManager(activity.getApplicationContext());
}
public static class ViewHolder {
public TextView username;
public TextView message;
public ImageView image;
public ProgressBar progress; // ADDED
public ImageView favoriteImg;
}
@Override
public int getCount() {
return tweets.size();
}
@Override
public Tweet getItem(int position) {
return tweets.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
holder = new ViewHolder();
holder.username = (TextView) v.findViewById(R.id.username);
holder.message = (TextView) v.findViewById(R.id.message);
holder.image = (ImageView) v.findViewById(R.id.avatar);
holder.progress = (ProgressBar) v.findViewById(R.id.progress_bar); // ADDED
holder.favoriteImg = (ImageView) v.findViewById(R.id.imgbtn_favorite);
v.setTag(holder);
} else
holder = (ViewHolder) v.getTag();
final Tweet tweet = tweets.get(position);
if (tweet != null) {
holder.username.setText(tweet.username);
holder.message.setText(tweet.message);
holder.image.setTag(tweet.image_url);
imageManager.displayImage(tweet.image_url, activity, holder.image,holder.progress); // CHANGED
/*If a product exists in shared preferences then set heart_red drawable
* and set a tag*/
if (checkFavoriteItem(tweet)) {
holder.favoriteImg.setImageResource(R.drawable.heart_red);
holder.favoriteImg.setTag("red");
} else {
holder.favoriteImg.setImageResource(R.drawable.heart_grey);
holder.favoriteImg.setTag("grey");
}
}
return v;
}
/* Checks whether a particular product exists in SharedPreferences */
public boolean checkFavoriteItem(Tweet checkTweet) {
boolean check = false;
List<Tweet> favorites = sharedPreference.getFavorites(context);
if (favorites != null) {
for (Tweet tweet : favorites) {
if (tweet.equals(checkTweet)) {
check = true;
break;
}
}
}
return check;
}
@Override
public void add(Tweet tweet) {
super.add(tweet);
tweets.add(tweet);
notifyDataSetChanged();
}
@Override
public void remove(Tweet tweet) {
super.remove(tweet);
tweets.remove(tweet);
notifyDataSetChanged();
}
}
logcat的:
07-12 09:49:11.312: E/AndroidRuntime(8857): FATAL EXCEPTION: main
07-12 09:49:11.312: E/AndroidRuntime(8857): java.lang.NullPointerException
07-12 09:49:11.312: E/AndroidRuntime(8857): at com.rowland.preferences.SharedPreference.getFavorites(SharedPreference.java:65)
07-12 09:49:11.312: E/AndroidRuntime(8857): at com.rowland.adapters.TweetItemAdapter.checkFavoriteItem(TweetItemAdapter.java:114)
07-12 09:49:11.312: E/AndroidRuntime(8857): at com.rowland.adapters.TweetItemAdapter.getView(TweetItemAdapter.java:99)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.AbsListView.obtainView(AbsListView.java:1593)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.ListView.makeAndAddView(ListView.java:1782)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.ListView.fillDown(ListView.java:705)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.ListView.fillFromTop(ListView.java:762)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.ListView.layoutChildren(ListView.java:1633)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.AbsListView.onLayout(AbsListView.java:1423)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.view.View.layout(View.java:7175)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.view.View.layout(View.java:7175)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.view.View.layout(View.java:7175)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.view.View.layout(View.java:7175)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.view.View.layout(View.java:7175)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.view.View.layout(View.java:7175)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.view.ViewRoot.performTraversals(ViewRoot.java:1146)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.os.Looper.loop(Looper.java:130)
07-12 09:49:11.312: E/AndroidRuntime(8857): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-12 09:49:11.312: E/AndroidRuntime(8857): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 09:49:11.312: E/AndroidRuntime(8857): at java.lang.reflect.Method.invoke(Method.java:507)
07-12 09:49:11.312: E/AndroidRuntime(8857): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-12 09:49:11.312: E/AndroidRuntime(8857): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-12 09:49:11.312: E/AndroidRuntime(8857): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您没有在适配器构造函数中初始化context
。您将其传递给getFavorites()
并在其上调用方法会导致NPE。