单独的类中的SharedPreferences

时间:2014-07-22 10:56:34

标签: android

我在非活动类中调用SharedPreferences时遇到问题。不幸的是,我在Android Studio中没有收到任何错误,但saveList()从未正常执行。

public class UDP_Client {
...

public void UDPBroatcast() {

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

        @Override
        protected Void doInBackground(Void... params) {

...
        listenForResponses(ds);

    }
  }
}


private void listenForResponses(DatagramSocket socket) throws IOException {
        byte[] buf = new byte[1024];
        try {
            while (true) {
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                String s = new String(packet.getData(), 0, packet.getLength());
                String ipaddress = packet.getAddress().getHostAddress();
                if (!(ipaddress.equals(ownIPAddress))) {
                    Log.d(TAG, "Received response: " + s + " From IP: " + ipaddress);
                }
                arrayList.add(ipaddress);
                Log.d(TAG, "Added IP:" + ipaddress + " to ArrayList");
            }
        } catch (SocketTimeoutException e) {
            Log.d(TAG, "Receive timed out");
            SaveIPs.saveList(); //Calling saveList after timeout
        }
    }    

...

public static class SaveIPs {

    private final Context context;
    private static SharedPreferences sharedPrefs;

    public SaveIPs(Context context) {
        this.context = context;
        sharedPrefs = context.getSharedPreferences("Found_Devices", 0);
    }

    private static void saveList() {
        SharedPreferences.Editor edit = sharedPrefs.edit();
        edit.clear();
        Object[] mStringArray = arrayList.toArray();
        for (int i = 0; i < mStringArray.length; i++) {
            Log.d("Saving storage position " + i + " to", (String) mStringArray[i]);
            edit.putString("Server" + i, (String) mStringArray[i]);
        }
        edit.commit();
    }
   }
}

有什么想法吗?

我遗漏了一些代码以供概览之用。

logcat的:

07-22 13:02:32.891    5232-5271/de.liquidbeam.LED.control D/UDP Discovery﹕ Own IP Address: 192.168.0.117
07-22 13:02:33.282    5232-5271/de.liquidbeam.LED.control D/UDP Discovery﹕ Sending data: <cmd="discover" application="liquidbeam_remote" challenge="myUDP" signature="5e6198975160345df82aa6085ebf7e9c"/>
07-22 13:02:33.282    5232-5271/de.liquidbeam.LED.control E/UDP Discovery﹕ Data sent:Messageto IP: 192.168.0.255
07-22 13:02:33.292    5232-5271/de.liquidbeam.LED.control D/UDP Discovery﹕ Added IP:192.168.0.117 to ArrayList
07-22 13:02:33.322    5232-5271/de.liquidbeam.LED.control D/UDP Discovery﹕ Received response: Hallo!!! From IP: 192.168.0.134
07-22 13:02:33.322    5232-5271/de.liquidbeam.LED.control D/UDP Discovery﹕ Added IP:192.168.0.134 to ArrayList
07-22 13:02:34.323    5232-5271/de.liquidbeam.LED.control D/UDP Discovery﹕ Receive timed out
07-22 13:02:34.333    5232-5271/de.liquidbeam.LED.control W/System.err﹕ java.lang.NullPointerException
07-22 13:02:34.333    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at de.liquidbeam.LED.control.fragments.UDP_Client$SaveIPs.saveList(UDP_Client.java:201)
07-22 13:02:34.333    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at de.liquidbeam.LED.control.fragments.UDP_Client$SaveIPs.access$200(UDP_Client.java:190)
07-22 13:02:34.333    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at de.liquidbeam.LED.control.fragments.UDP_Client.listenForResponses(UDP_Client.java:118)
07-22 13:02:34.333    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at de.liquidbeam.LED.control.fragments.UDP_Client.access$100(UDP_Client.java:36)
07-22 13:02:34.333    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at de.liquidbeam.LED.control.fragments.UDP_Client$1.doInBackground(UDP_Client.java:75)
07-22 13:02:34.333    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at de.liquidbeam.LED.control.fragments.UDP_Client$1.doInBackground(UDP_Client.java:57)
07-22 13:02:34.333    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-22 13:02:34.343    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-22 13:02:34.343    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-22 13:02:34.343    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-22 13:02:34.343    5232-5271/de.liquidbeam.LED.control W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

debuging arrayList:

this = {de.liquidbeam.LED.control.fragments.UDP_Client@830044849896}
socket = {java.net.DatagramSocket@830044852416}
buf = {byte[1024]@830045006728}
packet = {java.net.DatagramPacket@830045008808}
s = {java.lang.String@830045009744}"Hallo!!!"
ipaddress = {java.lang.String@830045009816}"192.168.0.134"
arrayList = {java.util.ArrayList@830044849872} size = 2
[0] = {java.lang.String@830045008192}"192.168.0.117"
[1] = {java.lang.String@830045009816}"192.168.0.134"

2 个答案:

答案 0 :(得分:1)

这里我的理解是你必须在从套接字连接获取数据后调用saveList方法,这意味着你可以在catch块之后调用saveList方法或者在这个块的内部创建finally块。

我认为ArrayList变量arrayList为null,因此只抛出NullPointerException。从你获取数据的套接字连接中检查数据或为null。

答案 1 :(得分:1)

您将收到此异常,因为首先需要初始化SaveIPs类构造函数。 从类中删除static修饰符:

  

公共类SaveIPs {//静态不需要希望:

然后在catch块中

        catch (SocketTimeoutException e) 
      {
             Log.d(TAG, "Receive timed out");
             SaveIPs saveip_obj = new SaveIPs(context)//pass the context here  
             saveip_obj.saveList();

      }

您可能没有pref中的任何数据,因为sharedPrefs = context.getSharedPreferences("Found_Devices", 0);

没有被召唤。 如果这不是想知道行号的原因。它指向Nullpointer Exception