从单独的类更新Listview(SharedPreferences - > ArrayList)

时间:2014-07-29 13:05:28

标签: java android listview arraylist sharedpreferences

我想在onPostExecute()单独的类中以片段形式更新我的Listview。

Listview的第一次初始化有效,但是我再次调用createList(),应用程序崩溃(NullPointerException)

任何想法?

Main_Fragment:

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
            View fragmentView = inflater.inflate(R.layout.main_fragment, container, false);

            StartSocketService = (Button) fragmentView.findViewById(R.id.start_socketservice);
            StartSocketService.setOnClickListener(this);
            StopSocketService = (Button) fragmentView.findViewById(R.id.stop_socketservice);
            StopSocketService.setOnClickListener(this);

            listview = (ListView) fragmentView.findViewById(R.id.listView1);
            createList();

            return fragmentView;
        }

        public void createList(){

            //Reading Server IPs from SharedPreferences and put them to ListView
            SharedPreferences settings = getActivity().getSharedPreferences("Found_Devices", 0);

            for (int i = 0; i < 255; i++) {
                if ((settings.getString("Server"+i,null)) != null) {
                    serverList.add(settings.getString("Server"+i, null));
                    Log.v("Reading IP: " +settings.getString("Server"+i, null), " from SraredPreferrences at pos.: "+i );
                }
            }
            //Initializing listView

            final StableArrayAdapter adapter = new StableArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, serverList);
            listview.setAdapter(adapter);

            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                    final String item = (String) parent.getItemAtPosition(position);
                    view.animate().setDuration(2000).alpha(0).withEndAction(new Runnable() {
                        @Override
                        public void run() {
                            serverList.remove(item);
                            adapter.notifyDataSetChanged();
                            view.setAlpha(1);
                        }
                    });
                }

            });
        }

某些课程:

async_cient = new AsyncTask<Void, Void, Void>() {

           ...

            protected void onPostExecute(Void result) {
                Toast.makeText(mContext, "Scan Finished", Toast.LENGTH_SHORT).show();
                Main_Fragment cList = new Main_Fragment();
                cList.createList();
                super.onPostExecute(result);

            }

        };

日志:

07-29 14:42:46.428    3382-3382/de.liquidbeam.LED.control D/AndroidRuntime﹕ Shutting down VM
07-29 14:42:46.428    3382-3382/de.liquidbeam.LED.control W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41549ba8)
07-29 14:42:46.438    3382-3382/de.liquidbeam.LED.control E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: de.liquidbeam.LED.control, PID: 3382
    java.lang.NullPointerException
            at de.liquidbeam.LED.control.fragments.Main_Fragment.createList(Main_Fragment.java:56)
            at de.liquidbeam.LED.control.background.UDP_Discover$1.onPostExecute(UDP_Discover.java:94)
            at de.liquidbeam.LED.control.background.UDP_Discover$1.onPostExecute(UDP_Discover.java:57)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
07-29 14:47:46.591    3382-3382/de.liquidbeam.LED.control I/Process﹕ Sending signal. PID: 3382 SIG: 9

行:

56 : SharedPreferences settings = getActivity().getSharedPreferences("Found_Devices", 0);
94 : cList.createList();
57: async_cient = new AsyncTask<Void, Void, Void>() {

1 个答案:

答案 0 :(得分:0)

您创建片段的新实例,而无需运行上下文(活动)。所以 这条线

SharedPreferences settings = getActivity().getSharedPreferences("Found_Devices", 0);

试图获得他的活动,但片段没有活动;)