我正在尝试通过单击警报活动中的ImageButton
将计数器设置为0,计数器位于Server.java类(这是 TCP服务器)中,但是当我正在尝试通过设置 alert.java :
Server server;
refreshing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
server.count = 0;
SharedPreferences prefs = getSharedPreferences("MY_DATA", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.apply();
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
});
崩溃时出错:
Java.lang.NullPointerException: Attempt to write to field 'int com.example.sguidetti.selfmanegment.Server.count' on a null object reference
我在主要活动中使用相同的操作来重置同一个计数器,方法是点击另一个ImageButton
,这样可行。
(Ps:我是android的新手,所以我无法理解很多东西)
编辑这里是Server.java
public class Server {
DataBaseHandler myDB;
allert Allert;
MainActivity activity;
RecyclerViewAdapter adapterView;
Adapter adapter;
ServerSocket serverSocket;
public static int count=0;
String letto = "";
private SharedPreferences prefs;
static final int socketServerPORT = 8080;
public Server(MainActivity activity) {
this.activity = activity;
Thread socketServerThread = new Thread(new SocketServerThread());
socketServerThread.start();
}
public int getPort() {
return socketServerPORT;
}
public void onDestroy() {
if (serverSocket != null) try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class SocketServerThread extends Thread {
Vibrator vibrator;
String date,ora;
long[] pattern = {0, 1000, 500, 1000, 500, 1000};
int lun;
@Override
public void run() {
InputStream leggi;
try {
serverSocket = new ServerSocket(socketServerPORT);
while (true) {
myDB = DataBaseHandler.getInstance(activity);
Socket socket = serverSocket.accept();
leggi = socket.getInputStream();
byte[] data = new byte[1000];
lun = leggi.read(data, 0, data.length);
letto = new String(data, "UTF-8");
count++;
MediaPlayer mPlay = MediaPlayer.create(activity, R.raw.gabsuono);
mPlay.start();
vibrator = (Vibrator) activity.getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(pattern, -1);
date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
ora = new SimpleDateFormat("HH:mm:ss").format(new Date());
myDB.insertDataServer(date, ora, letto);
//adapterView.notifyDataSetChanged();
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
prefs = activity.getSharedPreferences("MY_DATA", MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("counter", count);
edit.commit();
activity.msg.setText(String.valueOf(count));
activity.msg.setVisibility(View.VISIBLE);
}
});
leggi.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void Parti() {
prefs = activity.getSharedPreferences("MY_DATA", MODE_PRIVATE);
count = prefs.getInt("counter", count);
activity.msg.setText("" + count);
if (count == 0)
activity.msg.setVisibility(View.INVISIBLE);
else
activity.msg.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:0)
你需要使用int static
public static final int count = 0;
然后您可以从其他活动中访问该值
Server.count = 5;
<强>更新强>
在服务器中声明您的计数
public static final String COUNT = "count";
也宣布
public static final String PREFS = "prefs";
并声明你的共享prefes
SharedPreferences prefs = getSharedPreferences(Server.PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
然后在您调用sharedPreference
之后单击按钮editor.putInt(Server.COUNT,5).commit();
然后获取警报活动中的int值,如下所示
int count = prefs.getInt(Server.COUNT,0);
答案 1 :(得分:0)
您忘记初始化server
对象,添加:
Server server = new Server();