android


Button click event closes Activity insted of running ASyncTask


I have a wierd problem with my application. On button click I want to run ASyncTask but insted of running task, my current Activity gets closed. I don't know why and I am not sure how to fix it. Does someone knows where is the problem?
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);
}
}
}
EDIT: Added logcat logs
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: Added permissions
<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" />
Define testButtonClicked Click Listener Before actually setting it as A listener so the code will be like
.............your code .......
private final View.OnClickListener testButtonClicked = new View.OnClickListener() {
#Override
public void onClick(View v) {
new SpeedTestTask().execute();
}
};
then this line :
findViewById(R.id.start_test).setOnClickListener(testButtonClicked);
....................................
..
your rest of the code again
I hope this helps

Related Links

Alert dialog period is to short
IntentFilter Camera and Picture Taken
Activity reloads the whole data from server again when coming back from an other activity in Android
Using ContentProviders for non persistent data
Can I custom tab layout and theme in android?
Event handler for OptionsMenuItem in fullscreenwrapper2 and sl4a
How to modify already sent broadcast in android
Delete view from ListView
Android different screen size issues (Testing on Galaxy S1, S2, S3 and Note2 etc)
Debugging my Android app [closed]
getSelectedText on inactive InputConnection-working on retrieving Title Column from CalendarContract.Events in android
Showing map inside a circle
How to support MOV(quick time) in android?
how to read images from folder not url help me so show in mapview ballon
Simple DELETE statement did not work in SQlite [duplicate]
how to edit run.sh file in eclipse

Categories

HOME
service-worker
algorithm
httparty
hyperlink
wakanda
redux-form
interface
sitecore
spring-data-redis
entity-framework-core
carousel
simulink
permissions
pyqt4
zip
webix
opengl-3
rcloud
mql5
reactive-programming
endeca
nancy
selenium-ide
onedrive-api
enterprise-architect
chargify
sigma.js
googlebot
blade
inno-download-plugin
zxing
monit
resourcebundle
expressionengine3
slidetoggle
failover
mobx
left-join
erb
installscript
imx6
navigator
servicestack-text
openalpr
google-earth-engine
graphhopper
reactivekit
xv6
watchface
datacachefactory
parser-generator
android-exoplayer
importerror
vaadin-elements
xbmc
term
dtsearch
scalding
modelsim
spring-data-couchbase
android-testing
lsyncd
sandcastle
pocketsphinx-android
envi
jodd
xdoclet
aurora
createprocess
fuzzywuzzy
thread-sleep
libvlc
maybe
intel-c++
opserver
visual-sourcesafe-2005
sql-server-data-tools
aspectj-maven-plugin
ejb-2.x
variadic-templates
gii
android-framework
maven-scm
synapse
mt4j
c++builder-xe5
away3d
modular
epiceditor
bjyauthorize
thttpd
josql
cgimage
box2d-iphone
saxparseexception
mysql-error-1205
webdev.webserver
inline-formset
luajava
load-time
uideviceorientation
silent
message-passing
sifr
pagemethods
feasibility
asynchronous-wcf-call
linfu-dynamicproxy

Resources

Encrypt Message