我的申请有一个奇怪的问题。单击按钮我想运行ASyncTask但是运行任务,我当前的Activity关闭了。我不知道为什么,我不知道如何解决它。有人知道问题出在哪里吗?
public class SpeedTestActivity extends BaseActivity {
private View mRunningBar;
private TextView uploadLabel;
private TextView uploading;
private TextView downloadLabel;
private TextView downloading;
private Activity context;
private SpeedTestSocket speedTestSocket;
private static final String TAG = "SpeedTesting";
private long testingTime = 0;
LineChart chart;
LineData data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speed_test);
SpeedTestSocket speedTestSocket;
String TAG = "SpeedTesting";
downloadLabel = (TextView) findViewById(R.id.downloadingLabel);
downloading = (TextView) findViewById(R.id.downloading);
uploadLabel = (TextView) findViewById(R.id.uploadingLabel);
uploading = (TextView) findViewById(R.id.uploading);
mRunningBar = findViewById(R.id.runningBar);
mRunningBar.setVisibility(View.GONE);
findViewById(R.id.start_test).setOnClickListener(testButtonClicked);
chart = (LineChart) findViewById(R.id.chart);
ArrayList<Entry> dataset1 = new ArrayList<Entry>();
ArrayList<Entry> dataset2 = new ArrayList<Entry>();
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("0");
xVals.add("1");
xVals.add("2");
xVals.add("3");
xVals.add("4");
xVals.add("5");
xVals.add("6");
xVals.add("7");
xVals.add("8");
xVals.add("9");
xVals.add("10");
LineDataSet setData1 = new LineDataSet(dataset1, "Download");
LineDataSet setData2 = new LineDataSet(dataset2, "Upload");
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(setData1);
dataSets.add(setData2);
data = new LineData(xVals, dataSets);
chart.setData(data);
chart.invalidate(); // refresh
//new SpeedTest(this, mRunningBar, downloadLabel, uploadLabel, downloading , uploading , chart, data).execute();
}
private final View.OnClickListener testButtonClicked = new View.OnClickListener() {
@Override
public void onClick(View v) {
new SpeedTestTask().execute();
}
};
// scheduling the task at interval
public class SpeedTestTask extends AsyncTask<Void, Void, Float> {
@Override
protected void onPreExecute() {
mRunningBar.setVisibility(View.VISIBLE);
}
@Override
protected Float doInBackground(Void... params) {
speedTestSocket = new SpeedTestSocket();
/* speedTestSocket.startUpload("1.testdebit.info",
80, "/", 10000000); //will block until upload is finished*/
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
if (speedTestSocket.getSpeedTestMode() == SpeedTestMode.UPLOAD) {
SpeedTestReport uploadReport = speedTestSocket.getLiveUploadReport();
Log.i(TAG, "---------------current upload report--------------------");
Log.i(TAG, "progress : " + uploadReport.getProgressPercent() + "%");
Log.i(TAG, "transfer rate bit : " + uploadReport.getTransferRateBit() + "b/s");
Log.i(TAG, "transfer rate octet : " + uploadReport.getTransferRateOctet()*8/1024/1024 + "Mbps");
Log.i(TAG, "uploaded for now : " + uploadReport.getTemporaryPacketSize()
+ "/" + uploadReport.getTotalPacketSize());
testingTime = (uploadReport.getReportTime() - uploadReport.getStartTime()) / 1000;
Log.i(TAG, "amount of time : " + testingTime + "s");
data.addEntry(new Entry( (uploadReport.getTransferRateOctet()*8/1024/1024) + 50f, 0), Math.round(testingTime));
if (testingTime>=10){
speedTestSocket.closeSocketJoinRead();
}
Log.i(TAG, "--------------------------------------------------------");
} else if (speedTestSocket.getSpeedTestMode() == SpeedTestMode.DOWNLOAD) {
SpeedTestReport downloadReport = speedTestSocket.getLiveDownloadReport();
Log.i(TAG, "---------------current download report--------------------");
Log.i(TAG, "progress : " + downloadReport.getProgressPercent() + "%");
Log.i(TAG, "transfer rate bit : " + downloadReport.getTransferRateBit() + "b/s");
Log.i(TAG, "transfer rate octet : " + downloadReport.getTransferRateOctet()*8/1024/1024 + "Mbps");
Log.i(TAG, "downloaded for now : " + downloadReport.getTemporaryPacketSize()
+ "/" + downloadReport.getTotalPacketSize());
testingTime = (downloadReport.getReportTime() - downloadReport.getStartTime()) / 1000;
Log.i(TAG, "amount of time : " + testingTime + "s");
if (testingTime>=10){
speedTestSocket.closeSocketJoinRead();
}
}
}
};
timer.scheduleAtFixedRate(task, 0, 1000);
speedTestSocket.startUpload("1.testdebit.info", 80, "/", 100000000);
return null;
}
@Override
protected void onPostExecute(Float result) {
chart.notifyDataSetChanged(); // let the chart know it's data changed
chart.invalidate(); // refresh
chart.animateY(1000, Easing.EasingOption.EaseOutBack);
mRunningBar.setVisibility(View.GONE);
}
}
}
编辑:添加了logcat日志
04-07 12:18:09.958 26390-26432/? E/dalvikvm: can't open /mnt/sdcard/20160407-101809.hprof: Permission denied
04-07 12:18:09.968 26390-26432/? E/AndroidRuntime: Error reporting crash
java.lang.RuntimeException: Failure during heap dump; check log output for details
at dalvik.system.VMDebug.dumpHprofData(Native Method)
at dalvik.system.VMDebug.dumpHprofData(VMDebug.java:302)
at android.os.Debug.dumpHprofData(Debug.java:885)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:89)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
04-07 12:18:09.978 278-507/? I/WindowManager: WIN DEATH: Window{418e33c8 com.thtanalyzer.thtanalyzer/com.thtanalyzer.thtanalyzer.MainActivity paused=false}
04-07 12:18:09.978 278-1628/? I/ActivityManager: Process com.thtanalyzer.thtanalyzer (pid 26390) has died.
04-07 12:18:09.978 278-1628/? W/ActivityManager: Force removing ActivityRecord{41a7bdd8 com.thtanalyzer.thtanalyzer/.SpeedTestActivity}: app died, no saved state
04-07 12:18:09.988 278-396/? D/NetworkPolicy: mMSimTelephonyManager.getDataSubscription() = 0
EDIT2:添加了权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
答案 0 :(得分:0)
定义testButtonClicked Click Listener
在实际将其设置为A侦听器之前,代码就像
.............your code .......
private final View.OnClickListener testButtonClicked = new View.OnClickListener() {
@Override
public void onClick(View v) {
new SpeedTestTask().execute();
}
};
然后这一行:
findViewById(R.id.start_test).setOnClickListener(testButtonClicked);
.................................... .. 您的其余代码
我希望这会有所帮助