这是我的应用程序部分的一些代码,我试图让人们查看他们喜欢的部分。我让它为那部分工作。现在我正在努力使它在onLongClick时它允许选项删除一个。我不知道那部分是否有效,因为我目前在数组部分遇到了一个错误,从我将其缩小到了。它非常奇怪,因为我可以看到它与它工作的区别,而不是真的不应该有任何问题。如果你愿意,也许我需要一些新鲜的眼睛看着这个。感谢
public class Favorites extends ListActivity{
public static final String PREFS_NAME = "Favorites";
String[] title;
int favNum, num;
int[] category, member;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
grabFav();
num = favNum;
num++;
if(favNum > -1){
title = new String[num];
category = new int[num];
member = new int[num];
allFavs();
setListAdapter(new ArrayAdapter<String>(this, R.layout.item_list, title));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
}
public void grabFav(){
SharedPreferences saved = this.getSharedPreferences(PREFS_NAME, 0);
favNum = saved.getInt("FavoriteNum", -1);
}
public void allFavs(){
SharedPreferences saved = this.getSharedPreferences(PREFS_NAME, 0);
for(int i = 0; i< favNum; i++){
title[i] = saved.getString("Title"+i, "");
}
for(int n = 0; n< favNum; n++){
category[n] = saved.getInt("Category"+n, 0);
}
for(int m = 0; m< favNum; m++){
member[m] = saved.getInt("Member"+m, 0);
}
}
}
当那个顶部看起来像这样
grabFav();
favNum++;
if(favNum > 0){
title = new String[favNum];
category = new int[favNum];
member = new int[favNum];
allFavs();
它工作正常(不确定onLongItemClick,因为我通常将它注释掉,所以我可以找到错误)。据我所知,它应该工作得很好。我发布了整个班级,因为你发现了另一个可能导致问题的错误。如果你想看
,这是log cat09-04 11:09:20.576: ERROR/AndroidRuntime(18769): FATAL EXCEPTION: main
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): java.lang.NullPointerException
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.ListView.makeAndAddView(ListView.java:1727)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.ListView.fillDown(ListView.java:652)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.ListView.fillFromTop(ListView.java:709)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.ListView.layoutChildren(ListView.java:1580)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.view.View.layout(View.java:7035)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.view.View.layout(View.java:7035)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.view.View.layout(View.java:7035)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.view.View.layout(View.java:7035)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.os.Looper.loop(Looper.java:123)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-04 11:09:20.576: ERROR/AndroidRuntime(18769): at dalvik.system.NativeStart.main(Native Method)
非常感谢任何帮助。感谢
答案 0 :(得分:2)
您是否检查过,您从偏好设置中获得的所有标题字符串确实存在,即此行未将null
分配给title[i]
:
title[i] = saved.getString("Title"+i, "");
也许你错过了第0个标题“Title0”而只是从“Title1”开始?
答案 1 :(得分:0)
您可以使用此格式,在使用视图之前立即设置活动内容。每当我之前调用视图上的方法时,我都会得到NullPointer异常 setContentView方法..希望这能解决你的问题:)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_xml);
// your code
}