Reputation: 698
This is a bit of code from a section of my app where I am trying to allow people to view the sections that they favorites. I have it working for that part. Now I'm trying to make it so that when onLongClick it allows the option to delete one. I do not know whether that part works yet because I am currently experiencing an error in the array section from what I have narrowed it down to. Its very weird because from what I can see the difference from it working and not there really shouldn't be any problem. Maybe I need some fresh eyes looking at this if you will. Thanks
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);
}
}
}
When that top part looks like this
grabFav();
favNum++;
if(favNum > 0){
title = new String[favNum];
category = new int[favNum];
member = new int[favNum];
allFavs();
It works fine (not sure about the onLongItemClick because I normally have it commented out so I can find the error). From what I can tell it should be working just fine. I posted the whole class incase you notice another error in it that will likely cause problems. Here is the log cat if you want to look
09-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)
Any help is greatly appreciated. Thanks
Upvotes: 0
Views: 113
Reputation: 20961
Have you checked, that all the title strings you get from your preferences actually exist, i.e. that this line does not assign null
to title[i]
:
title[i] = saved.getString("Title"+i, "");
Maybe you're missing the zeroth title "Title0" and start only at "Title1"?
Upvotes: 2
Reputation: 5208
You could use this format where you immediately set the content of your activity before using views. I used to get NullPointer exceptions whenever I called methods on views before the setContentView method.. Hope this'll resolve your problem :)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_xml);
// your code
}
'
Upvotes: 0