我想将值存储在sharedpreferences中,并从sharedpreferences获取值,最后在listview中显示这些值。 接下来,我想在长按特定值时单击从列表中删除值。删除值后,sharedpreferences中的状态已更新。 我做到了这一点,但是问题是当关闭应用程序并再次打开时,我必须在listview中显示sharedpreferences值。 但显示NullPointerExeption

实际上,我的要求是1)在sharedpreferences中存储值并在每次单击时在listview中显示值。仅允许五个值。并且onresume()方法中还提供了这些列表值。 2)当长按listview的特定值时,它必须删除并更新的值也存储在sharedpreferences中。

我的代码

public class ListViewDemo1 extends Activity {
    /**
     * Called when the activity is first created.
     */
    Button btn;
    static int count;
    private ListView list;
    public static ArrayList<String> values = new ArrayList<String>();        
    ArrayList countList = new ArrayList();
    private ArrayAdapter adapter;
    SharedPreferences shared;
    Editor editor;
    private static ArrayList<String> sharedList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        list = (ListView) findViewById(R.id.list);
        btn = (Button) findViewById(R.id.btn);
        shared = this.getSharedPreferences("Myprefernces", Context.MODE_WORLD_WRITEABLE);
        editor = shared.edit();
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                sharedList = new ArrayList();
                count++;
                if (count == 1) {
                    values.add("one");
                    countList.add(count);
                }
                if (count == 2) {
                    values.add("two");
                    countList.add(count);
                }
                if (count == 3) {
                    values.add("three");
                    countList.add(count);
                }
                if (count == 4) {
                    values.add("four");
                    countList.add(count);

                }
                if (count == 5) {
                    values.add("five");
                    countList.add(count);
                }
                if (count > 5) {
                    --count;
                    Toast.makeText(getApplicationContext(), "" + count, 100).show();
                }
                //put values to sharedpreferences
                editor.putInt("SIZE", values.size());
                for (int i = 0; i < values.size(); i++) {
                    editor.putString("addr" + i, values.get(i));
                }
                editor.commit();
                // getting values from sharedpreference
                int size = shared.getInt("SIZE", 0);
                for (int k = 0; k < size; k++) {
                    sharedList.add(shared.getString("addr" + k, ""));
                }
                adapter = new
                        ArrayAdapter(ListViewDemo1.this, android.R.layout.simple_list_item_1, sharedList);
                list.setAdapter(adapter);
            }
        });
        list.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {
                // TODO Auto-generated method stub
                --count;
                values.remove(arg2);
                sharedList.remove(arg2);
                editor.clear();
                editor.commit();
                editor.putInt("SIZE", sharedList.size());
                for (int i = 0; i < sharedList.size(); i++) {
                    editor.putString("addr" + i, sharedList.get(i));
                }
                editor.commit();
                adapter.notifyDataSetChanged();


                return true;
            }
        });

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (sharedList.size() > 0) {
            adapter = new
                    ArrayAdapter(ListViewDemo1.this, android.R.layout.simple_list_item_1, sharedList);
            list.setAdapter(adapter);
        }
    }
}

logcat的

04-21 09:56:02.142: E/AndroidRuntime(1160): FATAL EXCEPTION: main
04-21 09:56:02.142: E/AndroidRuntime(1160): java.lang.RuntimeException: Unable to resume activity {com.views/com.views.ListViewDemo1}: java.lang.NullPointerException
04-21 09:56:02.142: E/AndroidRuntime(1160):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-21 09:56:02.142: E/AndroidRuntime(1160):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-21 09:56:02.142: E/AndroidRuntime(1160):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 09:56:02.142: E/AndroidRuntime(1160):     at android.os.Looper.loop(Looper.java:137)
04-21 09:56:02.142: E/AndroidRuntime(1160):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-21 09:56:02.142: E/AndroidRuntime(1160):     at java.lang.reflect.Method.invokeNative(Native Method)
04-21 09:56:02.142: E/AndroidRuntime(1160):     at java.lang.reflect.Method.invoke(Method.java:511)
04-21 09:56:02.142: E/AndroidRuntime(1160):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 09:56:02.142: E/AndroidRuntime(1160):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-21 09:56:02.142: E/AndroidRuntime(1160):     at dalvik.system.NativeStart.main(Native Method)
04-21 09:56:02.142: E/AndroidRuntime(1160): Caused by: java.lang.NullPointerException
04-21 09:56:02.142: E/AndroidRuntime(1160):     at com.views.ListViewDemo1.onResume(ListViewDemo1.java:114)
04-21 09:56:02.142: E/AndroidRuntime(1160):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)

===============>>#1 票数:1

您只能在按钮的onClick()方法中实例化sharedList 恢复您的活动并且sharedList为null时,不会调用此方法。

您需要检查它是否不为空:

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    if(sharedList != null && sharedList.size()>0){
        adapter=new ArrayAdapter(ListViewDemo1.this,android.R.layout.simple_list_item_1,sharedList);
        list.setAdapter(adapter);
    }
} 

===============>>#2 票数:0

我认为问题在于初始化(或不这样做),

static int count;
public static ArrayList<String>  values = new ArrayList<String>();
private static ArrayList<String> sharedList;

现在,例如,未初始化的计数为之前的值:

count++;  
if(count==1){  

对于sharedList ,需要做同样的事情,例如new ArrayList

是的,每次都不会在onResume()之前调用onCreate()的原因,有关更多信息,请参见Android Activity Life Cycle
检查图1.活动生命周期。 在那个链接上。

它将为您的实现/需求以及如何为您的活动初始化或使用sharedPref和其他组件提供一个更好的主意。

===============>>#3 票数:0

阅读本文,希望对您有所帮助http://developer.android.com/guide/topics/data/data-storage.html

您可以使用SharedPreferences:

要保存数据:

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putString("text", mSaved.getText().toString());
 editor.putInt("selection-start", mSaved.getSelectionStart());
 editor.putInt("selection-end", mSaved.getSelectionEnd());
 editor.commit();

要检索数据:

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);

  ask by Durga translate from so

未解决问题?本站智能推荐: