I want to refresh after registering the article, so I'm trying to use the int while I'm looking for it, but if I go to the int and come back, the registration button that I have to access the DB doesn't work. Strangely, the button works when you return to the back button in the emulator after moving to the int. When moving to int, I closed the current activity, but it's the same phenomenon. Help me
Header with back button.
private void setHeader(){
noticeID = getIntent().getIntExtra("noticeID",0);
noticeTitle = getIntent().getStringExtra("noticeTitle");
TextView titleText = (TextView) board1.findViewById(R.id.titleText);
titleText.setText(noticeTitle);
final Button backButton = (Button)board1.findViewById(R.id.backButton);
backButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
Intent intent = new Intent(NoticesBoard.this, Notice.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
NoticesBoard.this.startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
This is the registration part of the article registration part.
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
AlertDialog.Builder builder = new AlertDialog.Builder(NoticesBoard.this);
dialog = builder.setMessage ("Registered".")
.setPositiveButton ("OK", null)
.create();
dialog.show();
} } else {
AlertDialog.Builder builder = new AlertDialog.Builder(NoticesBoard.this);
dialog = builder.setMessage ("Registration failed")
.set NegativeButton ("OK", null)
.create();
dialog.show();
}
} } catch (Exception e) {
e.printStackTrace();
}
}
};
ReplyRequest addRequest = new ReplyRequest(noticeID + "","", userID, replyContent, responseListener);
RequestQueue queue = Volley.newRequestQueue(NoticesBoard.this);
queue.add(addRequest);
}
});
}
This is the part of the database portion.
class BackgroundTask extends AsyncTask<Void, Void, String>
{
String target;
@Override
protected void onPreExecute() {
target = "http://cafe24.com/php";
}
@Override
protected String doInBackground(Void...voids) {
try {
URL url = new URL(target);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String temp;
StringBuilder stringBuilder = new StringBuilder();
while((temp = bufferedReader.readLine()) !=null)
{
stringBuilder.append(temp + "\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} } catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void onProgressUpdate(Void...values) {
super.onProgressUpdate();
}
@Override
public void onPostExecute(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("response");
int count = 0;
int replyID, noticeID, rreplyID;
String replyContent, replyName, replyDate;
while(count < jsonArray.length())
{
JSONObject object = jsonArray.getJSONObject(count);
replyID = object.getInt("replyID");
noticeID = object.getInt("noticeID");
rreplyID = object.getInt("rreplyID");
replyContent = object.getString("replyContent");
replyName = object.getString("replyName");
replyDate = object.getString("replyDate");
N_Reply reply = new N_Reply (replyID, noticeID, rreplyID, replyContent, replyName, replyDate);
replyList.add(reply);
count++;
}
} } catch (Exception e) {
e.printStackTrace();
}
}
}
}
This is the log.
04-25 10:44:52.923 11709-12200/com.example.uet.nsu E/Volley: [224] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at libcore.net.UriCodec.encode(UriCodec.java:132)
at java.net.URLEncoder.encode(URLEncoder.java:57)
at com.android.volley.Request.encodeParameters(Request.java:450)
at com.android.volley.Request.getBody(Request.java:436)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:260)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:234)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Thank you for reading.
android-intent
If the problem occurs in the same step each time, it is the fastest way to look at the log and debug it. According to the log, the value does not exist where the current string object is handed over and is in a null state, causing a nullPointerException, and the app is malfunctioning.
If the line of code that you implemented (in the log) is printed during the call stack, look at the code in that part first. If it doesn't exist, please try to do the part you suspect in debugging mode one by one. Or adding logs is a good idea. Anyway, I have to find out which of the codes I wrote myself is the problem, but it is not easy to find it with the information you posted.
© 2024 OneMinuteCode. All rights reserved.