这是我的主要活动AsyncTask
。
class GetProductDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
int i;
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this); //Here I get an error: The constructor ProgressDialog(PFragment) is undefined
pDialog.setMessage("Loading..");
pDialog.setTitle("Checking Network");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// emailid = args[0];
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
user1 = new ArrayList<HashMap<String, String>>();
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", loginemail));
JSONObject json = jParser.makeHttpRequest(userprofile, "GET", params);
// String HT ;
// HT= "5004";
// params.add(new BasicNameValuePair("email", emailid));
// params.add(new BasicNameValuePair(TAGu_id, deviceId));
// JSONObject json = jParser.makeHttpRequest(userprofile, "GET", params);
// params.add(new BasicNameValuePair("id_rs", HT));
// getting product details by making HTTP request
// Note that product details url will use GET request
// JSONObject json = jParser.makeHttpRequest(
// userprofile, "GET", params);
// check your log for json response
// Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_success);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_USER); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
unread = product.getString("unread_notes");
unread = "105";
// tv.setText(Integer.toString(Integer.parseInt(unread)));
//Log.d("Single company Details", json.toString());
// Log.d("BAgersssssssssss", String.valueOf(unread));
// product with this pid found
// Edit Text
// cm = (TextView) findViewById(R.id.T1);
// cc = (TextView) findViewById(R.id.T2);
// na = (TextView) findViewById(R.id.T3);
// em = (TextView) findViewById(R.id.T4);
//ph = (TextView) findViewById(R.id.T5);
//dte = (TextView) findViewById(R.id.T9);
// cm = (TextView) getActivity().findViewById(R.id.T1);
// cc = (TextView) getActivity().findViewById(R.id.T2);
// na = (TextView) getActivity().findViewById(R.id.T3);
// em = (TextView) getActivity().findViewById(R.id.T4);
// ph = (TextView) getActivity().findViewById(R.id.T5);
// dte = (TextView) getActivity().findViewById(R.id.T9);
// cm = (TextView) getView().findViewById(R.id.T1);
// cc = (TextView) getView().findViewById(R.id.T2);
// na = (TextView) getView().findViewById(R.id.T3);
// em = (TextView) getView().findViewById(R.id.T4);
// ph = (TextView) getView().findViewById(R.id.T5);
// dte = (TextView) getView().findViewById(R.id.T9);
// as =(TextView) getView().findViewById(R.id.T10);
//txtPrayBody = (TextView) findViewById(R.id.txtPrayRequest);
/*
txtPrayFrom = (TextView) findViewById(R.id.txtYourName);
txtPrayerMade = (TextView)findViewById(R.id.txt_num_prayers);
*/
// display product data in EditText
// cm.setText(product.getString(TAG_company_name));
// cc.setText(product.getString(TAG_customer_code));
// na.setText(product.getString(TAG_name));
// em.setText(product.getString(TAG_email));
// ph.setText(product.getString(TAG_phoneno));
// dte.setText(product.getString(TAG_date));
// as.setText(product.getString(TAG_associated_since));
/*
txtPrayBody.setText(product.getString(TAG_BODY));
txtPrayFrom.setText(product.getString(TAG_FOOTER));
txtPrayerMade.setText(product.getString(TAG_NUM_PRAYERS));
*/
// unread = "2";
// i= Integer.valueOf(unread);
// i = Integer.parseInt(unread);
Log.d("i", String.valueOf(unread));
//int compare = Integer.parseInt(unread);
if(i == 0)
{
// tv.setText(" ");
tv.setVisibility(View.GONE);
}else
if(i >= 99){
tv.setText(Integer.toString(Integer.parseInt("99+")));
tv.setVisibility(View.VISIBLE);
}
else {
tv.setText(Integer.toString(Integer.parseInt(unread)));
tv.setVisibility(View.VISIBLE);
}
} else {
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// });
});
return loginemail;
}
主要活动中的此选项菜单。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
// new MenuInflater(this).inflate(R.menu.menu, menu);
new MenuInflater(this).inflate(R.menu.againex, menu);
RelativeLayout badgeLayout = (RelativeLayout) menu.getItem(R.id.Notification).getActionView();
ImageView img=(ImageView)badgeLayout.findViewById(R.id.click);
tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
// tv.setText(String.valueOf(unread));
//unread
//tv.setText("0");
//tv.setText(unread);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent nf =new Intent(MainActivity.this,Notifylist.class);
startActivity(nf);
// tv.setText(" ");
// int i = Integer.parseInt(unread);
// Log.d("i", String.valueOf(unread));
//int compare = Integer.parseInt(unread);
// if(i == 0)
// {
// // tv.setText("0");
// tv.setVisibility(View.GONE);
// }else
// if(i >= 99){
// tv.setText("99+");
// tv.setVisibility(View.VISIBLE);
// }
// else {
// tv.setText(unread);
// tv.setVisibility(View.VISIBLE);
// }
tv.setVisibility(View.GONE);
new ChangeNotification().execute();
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
// case R.id.Notification:
//Intent nf =new Intent(Review_List.this,Notifylist.class);
//startActivity(nf);
// return true;
// case R.id.User_Profile:
// Intent wr1 =new Intent(Review_List.this,UserProfile.class);
// wr1.putExtra("Tag_emailid",loginemail);
// //Toast.makeText(getApplicationContext(),""+loginemail,Toast.LENGTH_SHORT).show();
// startActivity(wr1);
//return true;
default:
return super.onOptionsItemSelected(item);
}
}
我使用OptionMenu
中的布局和通知铃,并在AsyncTask
的帮助下使用此布局来带来未读计数的值。但是在count settext中得到NullPointerException
,即(tv.settext();)。
错误记录
11-26 17:13:01.300 30644-30644/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
11-26 17:13:01.950 30644-30668/samples.exoguru.materialtabs I/GMPM﹕ App measurement is starting up
11-26 17:13:02.030 30644-30668/samples.exoguru.materialtabs E/GMPM﹕ getGoogleAppId failed with status: 10
11-26 17:13:02.070 30644-30668/samples.exoguru.materialtabs E/GMPM﹕ Uploading is not possible. App measurement disabled
11-26 17:13:02.110 30644-30647/samples.exoguru.materialtabs W/art﹕ Suspending all threads took: 20ms
11-26 17:13:02.160 30644-30654/samples.exoguru.materialtabs I/art﹕ Background partial concurrent mark sweep GC freed 363(47KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 1051KB/2MB, paused 0 total 130ms
11-26 17:13:02.510 30644-30644/samples.exoguru.materialtabs D/Atlas﹕ Validating map...
11-26 17:13:02.550 30644-30675/samples.exoguru.materialtabs D/﹕ HostConnection::get() New Host Connection established 0x7f507ec32840, tid 30675
11-26 17:13:02.560 30644-30675/samples.exoguru.materialtabs I/OpenGLRenderer﹕ Initialized EGL, version 1.4
11-26 17:13:03.100 30644-30675/samples.exoguru.materialtabs D/OpenGLRenderer﹕ Enabling debug mode 0
11-26 17:13:03.110 30644-30675/samples.exoguru.materialtabs W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-26 17:13:03.110 30644-30675/samples.exoguru.materialtabs W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f507f1e9880, error=EGL_SUCCESS
11-26 17:13:03.140 30644-30644/samples.exoguru.materialtabs I/Choreographer﹕ Skipped 34 frames! The application may be doing too much work on its main thread.
11-26 17:13:05.250 30644-30675/samples.exoguru.materialtabs W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-26 17:13:05.250 30644-30675/samples.exoguru.materialtabs W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f507f1e9a40, error=EGL_SUCCESS
11-26 17:13:05.320 30644-30644/samples.exoguru.materialtabs D/sqlite Details:﹕ Id: 1 ,Name: ,Email: karan.taneja@amartam.com,Phone no:,device id:null,Reg id:1,reg date:2015-11-13 07:24:15,flag:2
11-26 17:13:05.370 30644-30644/samples.exoguru.materialtabs D/sqlite Details:﹕ Id: 1 ,Name: ,Email: karan.taneja@amartam.com,Phone no:,device id:null,Reg id:1,reg date:2015-11-13 07:24:15,flag:2
11-26 17:13:05.490 30644-30675/samples.exoguru.materialtabs W/EGL_emulation﹕ eglSurfaceAttrib not implemented
11-26 17:13:05.490 30644-30675/samples.exoguru.materialtabs W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f507f1e9ac0, error=EGL_SUCCESS
11-26 17:13:05.990 30644-30644/samples.exoguru.materialtabs D/i﹕ 105
11-26 17:13:05.990 30644-30644/samples.exoguru.materialtabs D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
11-26 17:13:05.990 30644-30644/samples.exoguru.materialtabs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: samples.exoguru.materialtabs, PID: 30644
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
at samples.exoguru.materialtabs.MainActivity$GetProductDetails$1.run(MainActivity.java:312)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:1)
当你在后台时,你无法与UI交互......只有你可以在PostExecute()或onProgressUpdate()上的UI上显示任何消息。