我目前正在开发一个我正在开发的应用程序无法调试的问题。我在SDK 27,Graddle 3.0.1上,使用ZXing 1.9.8。
在一个Activity中,我调用了一个我用ZXing库创建的QR Reader类,所以它读取代码并通过Bundle将它发送到另一个Activity来分析它。
正在发生的事情是它打开相机,读取代码,然后应用程序关闭,无处不在,甚至无法打开下一个活动。 当用户选择在我做的一个小输入对话框上手动插入代码时,它现在到达下一个活动,但它太简单了,因为它没有在那里的任何断点处停止所以我可以调试它。
我通过在调用 bundle.putExtra 和 startactivity() 这对我来说可以理解为黑魔法。
由于没有错误,我开始查看logcat并重复发现:
01-29 15:28:32.706 701-765/system_process I/ActivityManager: Displayed com.psa.dkd/.activities.GetCarDetails: +90ms
01-29 15:28:32.858 701-1562/system_process I/WindowManager: WIN DEATH: Window{f7887c u0 com.psa.dkd/com.psa.dkd.activities.GetCarDetails}
01-29 15:28:32.862 701-889/system_process W/ActivityManager: Force removing ActivityRecord{7d4750 u0 com.psa.dkd/.activities.GetCarDetails t165}: app died, no saved state
01-29 15:28:32.869 701-1530/system_process I/WindowManager: WIN DEATH: Window{6bb4c7f u0 com.psa.dkd/com.psa.dkd.activities.ReaderQR EXITING}
01-29 15:28:32.871 701-1486/system_process I/WindowManager: WIN DEATH: Window{11094d u0 SurfaceView - com.psa.dkd/com.psa.dkd.activities.ReaderQR EXITING}
01-29 15:28:32.875 701-1528/system_process I/WindowManager: WIN DEATH: Window{a96ce5a u0 com.psa.dkd/com.psa.dkd.activities.ReadChassisActivity}
从我收集到的内容中,当主线程执行的工作量太大时,就会发生WIN DEATH,但这在过去是完美的。此外,主线程中没有那么多工作,因为我尽可能使用线程。
这可能是导致此更新的更新吗?我输了。
修改
ReadChassisActivity:https://pastebin.com/r60SPPHF
ReaderQR:https://pastebin.com/PSPT29Da
GetCarDetails:https://pastebin.com/Sgbi1XDi
ReadChassisActivity
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.psa.dkd.R;
import com.psa.dkd.utils.Logger;
import com.psa.dkd.utils.SessionManagement;
import java.util.concurrent.TimeUnit;
public class ReadChassisActivity extends AppCompatActivity {
private int BARCODE_READER_REQUEST_CODE;
SessionManagement session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_chassis);
session = new SessionManagement(getApplicationContext());
setTitle("Welcome" + session.getUserDetails().get("name"));
RelativeLayout new_car_manual = (RelativeLayout) findViewById(R.id.new_car_manual);
new_car_manual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
LayoutInflater li = LayoutInflater.from(ReadChassisActivity.this);
View promptsView = li.inflate(R.layout.input_prompt, null);
final EditText input = promptsView.findViewById(R.id.editTextDialogUserInput);
input.requestFocus();
AlertDialog alertDialog = new AlertDialog.Builder(ReadChassisActivity.this, R.style.DialogTheme).create();
alertDialog.setTitle("Manual Input");
alertDialog.setMessage("Insert barcode");
alertDialog.setView(promptsView);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String result = input.getText().toString().toUpperCase();
Logger.writeToLog(result, 0, 9);
Logger.writeLogToDB(session, 0, result, 9, 0);
Intent workIntent = new Intent(ReadChassisActivity.this, GetCarDetails.class);
Bundle b = new Bundle();
b.putString("car_code", result);
workIntent.putExtras(b);
/*try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
startActivity(workIntent);
finish();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
RelativeLayout new_car = (RelativeLayout) findViewById(R.id.new_car);
new_car.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ReadChassisActivity.this, "Connecting camera...", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ReadChassisActivity.this, ReaderQR.class);
startActivityForResult(intent, BARCODE_READER_REQUEST_CODE);
}
});
RelativeLayout exit = (RelativeLayout) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog alertDialog = new AlertDialog.Builder(ReadChassisActivity.this, R.style.DialogTheme).create();
alertDialog.setTitle("Exit");
alertDialog.setMessage("Return to login");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finishAffinity();
finish();
//session.logoutUser();
Intent i = new Intent(ReadChassisActivity.this, LoginActivity.class);
startActivity(i);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BARCODE_READER_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
String result = data.getStringExtra("code");
Logger.writeToLog(result, 0, 9);
Logger.writeLogToDB(session, 0, result, 9, 0);
Intent workIntent = new Intent(ReadChassisActivity.this, GetCarDetails.class);
Bundle b = new Bundle();
b.putString("car_code", result);
workIntent.putExtras(b);
/*try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
startActivity(workIntent);
finish();
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
Toast.makeText(getApplicationContext(), "No results", Toast.LENGTH_LONG).show();
Intent choiceIntent = new Intent(ReadChassisActivity.this, ReadChassisActivity.class);
startActivity(choiceIntent);
finish();
}
}
}
@Override
public void onBackPressed() {
}
}
ReaderQR
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.google.zxing.Result;
import com.psa.dkd.R;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ReaderQR extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader_qr);
QrScanner(this.mScannerView);
setTitle("Pass it over the code!");
}
public void QrScanner(View view) {
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera();
}
public void handleResult(Result rawResult) {
Intent returnIntent = new Intent();
returnIntent.putExtra("code", rawResult.getText());
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
}
GetCarDetails
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.psa.dkd.R;
import com.psa.dkd.utils.SessionManagement;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import static com.psa.dkd.utils.Misc.jsonToMap;
public class GetCarDetails extends Activity {
private final int WAIT_TIME = 1000;
private String car_code;
private String server;
private String worker_id;
private HashMap<String, String> car_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_details);
findViewById(R.id.mainSpinner2).setVisibility(View.VISIBLE);
SessionManagement session = new SessionManagement(GetCarDetails.this);
this.server = session.getUserDetails().get("server");
this.worker_id = session.getUserDetails().get("id");
this.car_code = getIntent().getExtras().getString("car_code");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// GET CAR DETAILS FROM DB - car_code
try {
Log.d("TEST", "TRYING...");
String result = new getCarData().execute("APP/getCarDetails.php", car_code, worker_id).get();
Log.d("TEST", "RECEIVED: " + result);
car_data = jsonToMap(result);
} catch (InterruptedException | ExecutionException | JSON Exception e) {
Log.d("TESTE", e.toString());
Toast.makeText(getApplicationContext(), "Error has happened", Toast.LENGTH_LONG).show();
Intent choiceIntent = new Intent(GetCarDetails.this, ReadChassisActivity.class);
startActivity(choiceIntent);
finish();
}
if (car_data.get("result").equals("1")) {
Bundle b = new Bundle();
b.putString("car_code", car_code);
(...)
Intent i = new Intent(GetCarDetails.this, JobSelectActivity.class);
i.putExtras(b);
startActivity(i);
finish();
} else {
if (car_data.get("result").equals("0")) {
Toast.makeText(GetCarDetails.this, "Nothing scheduled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(GetCarDetails.this, "This one is done, take another", Toast.LENGTH_LONG).show();
}
Intent mainIntent = new Intent(GetCarDetails.this, ReadChassisActivity.class);
startActivity(mainIntent);
finish();
}
}
}, WAIT_TIME);
}
public class getCarData extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String text = "";
BufferedReader reader = null;
try {
URL url = new URL(server + params[0]);
JSONObject postDataParams = new JSONObject();
postDataParams.put("code", params[1]);
postDataParams.put("user_id", params[1]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000); // milliseconds
conn.setConnectTimeout(15000); // milliseconds
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("json", postDataParams.toString());
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
String param = "json=" + URLEncoder.encode(postDataParams.toString(), "UTF-8");
wr.write(param.getBytes());
wr.flush();
wr.close();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line).append("\n");
}
text = sb.toString();
return text;
} catch (Exception e) {
Log.d("ERROR FETCHING DATA", "Exception: " + e.getMessage());
return "Exception: " + e.getMessage();
}
}
}
}
答案 0 :(得分:1)
尝试删除Toast.makeText
最近3天内,我一直在搜索类似的错误。我也不例外,只有WINDEATH像你一样。
我希望这对某人有帮助。
答案 1 :(得分:0)
收到相机结果后,您忘记恢复相机处理程序。您需要添加以下内容:
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
答案 2 :(得分:0)
所以,我一直尝试其他方法,我来到这里:
(...)
Intent workIntent = new Intent(ReadChassisActivity.this, GetCarDetails.class);
Bundle b = new Bundle();
b.putString("car_code", result);
workIntent.putExtras(b);
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
startActivity(workIntent);
finish();
似乎为putExtras()方法提供时间来解决问题,对可用性影响最小。
这是为什么?此外,这看起来像用胶带把建筑物给我,是不是有更好的方法?