I'm fairly new at threading so this could be a pretty n00by question.. (:
I've got a thread running in a separate class to my main activity this all works well, I'm able to access methods on my main activity from the thread using handler.Post(new Runnable() {... and this all works well until I try to call a dialog which requires user input.
This is the method for the dialog:
public boolean displayConfirm(String message, String positiveButton, String negativeButton, final displayConfirmer onOnClick) {
AlertDialog.Builder builder = new AlertDialog.Builder(ISyncCRMActivity.this);
builder.setMessage(message);
builder.setCancelable(false);
builder.setPositiveButton(positiveButton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
onOnClick.onClick(true);
}
});
builder.setNegativeButton(negativeButton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
onOnClick.onClick(false);
}
});
AlertDialog dialog = builder.create();
dialog.show();
return bool;
}
private boolean bool;
public interface displayConfirmer {
public void onClick(boolean result);
}
As you can see you pass it a message for the dialog to display and a yes, no button text, then it returns wherever the user clicked yes (true) or no (false), with this I'm using a interface for the onClick(). All of this allows me to use this code to execute it...
displayConfirm("Is this a dialog?", "Yes", "No", new ISyncCRMActivity.displayConfirmer() {
public void onClick(boolean result) {
// TODO Auto-generated method stub
Toast.makeText(ISyncCRMActivity.this, result ? "Yes" : "No", Toast.LENGTH_LONG).show();
}
});
This will wait until the user has clicked yes or no before moving on in the code which is exactly what I need, but in another thread.
So in my separate thread this is what I'm doing:
handler.post(new Runnable() {
public void run() {
iSyncCRMActivity.displayConfirm("Is this a dialog?", "Yes", "No", new ISyncCRMActivity.displayConfirmer() {
public void onClick(boolean result) {
if (result) dialog = true;
else dialog = false;
}
});
}
});
if (dialog) {
//clicked yes
} else {
//clicked no
}
As you can see, I want to invoke the dialog on the UI thread and then handle the response on the separate thread.
How am I able to achieve this? Is this even achievable?!
Any help would be much appreciated (: Thanks!
Edit:
this is the error:
07-20 11:04:35.782: ERROR/global(16201): Deprecated Thread methods are not supported.
07-20 11:04:35.782: ERROR/global(16201): java.lang.UnsupportedOperationException
07-20 11:04:35.782: ERROR/global(16201): at java.lang.VMThread.stop(VMThread.java:85)
07-20 11:04:35.782: ERROR/global(16201): at java.lang.Thread.stop(Thread.java:1379)
07-20 11:04:35.782: ERROR/global(16201): at java.lang.Thread.stop(Thread.java:1344)
07-20 11:04:35.782: ERROR/global(16201): at com.millennium.isynccrm.Classes.TcpClient.disconnect(TcpClient.java:55)
07-20 11:04:35.782: ERROR/global(16201): at com.millennium.isynccrm.Classes.SyncClient.stopSync(SyncClient.java:73)
07-20 11:04:35.782: ERROR/global(16201): at com.millennium.isynccrm.Classes.SyncClient.recieveMessage(SyncClient.java:236)
07-20 11:04:35.782: ERROR/global(16201): at com.millennium.isynccrm.Classes.TcpClient$1.run(TcpClient.java:34)
07-20 11:04:35.782: ERROR/global(16201): [ 07-20 11:04:35.782 16201:0x3f54 F/dalvikvm ]
07-20 11:04:35.782: ERROR/global(16201): Exception!!! threadid=10: thread exiting with uncaught exception (group=0x4001d810)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): FATAL EXCEPTION: Thread-12
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.view.ViewRoot.checkThread(ViewRoot.java:2806)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.view.View.requestLayout(View.java:8125)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.view.View.requestLayout(View.java:8125)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.view.View.requestLayout(View.java:8125)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.view.View.requestLayout(View.java:8125)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.view.View.requestLayout(View.java:8125)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.widget.TextView.checkForRelayout(TextView.java:5378)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.widget.TextView.setText(TextView.java:2688)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.widget.TextView.setText(TextView.java:2556)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at android.widget.TextView.setText(TextView.java:2531)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at com.millennium.isynccrm.ISyncCRMActivity.updateCurentTask(ISyncCRMActivity.java:282)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at com.millennium.isynccrm.Classes.SyncClient.stopSync(SyncClient.java:74)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at com.millennium.isynccrm.Classes.SyncClient.recieveMessage(SyncClient.java:236)
07-20 11:04:35.812: ERROR/AndroidRuntime(16201): at com.millennium.isynccrm.Classes.TcpClient$1.run(TcpClient.java:34)
I believe you are waiting for your dialog boolean to be set before you proceed to your next line of code. There are a number of ways to do this and I suggest you read up on Java concurrency tutorials here. Below is quick solution to get things working but I am not sure that it is the best as I am unfamiliar with android development. However, I thought the main point of dialogs in Android is that they are asynchronous (Why don't you put what you want to happen next in your callback rather than getting another thread to wait?).
A new class which keeps a record of whether yes or no has been pressed:
public class WaitingDialog implements Runnable {
volatile boolean finished;
volatile boolean dialog;
String message;
String button1;
String button2;
public WaitingThread(String message, String button1, String button2) {
this.button1 = button1;
this.button2 = button2;
this.message = message;
finished = false;
}
#Override
public void run() {
iSyncCRMActivity.displayConfirm(message, button1, button2, new ISyncCRMActivity.displayConfirmer() {
public void onClick(boolean result) {
if (result) dialog = true;
else dialog = false;
finished = true;
}
});
}
public boolean isFinished() {
return finished;
}
public boolean getDialog {
return dialog;
}
}
And your existing code using this new class:
WaitingDialog waitingDialog = new WaitingDialog("Is this a dialog?", "Yes", "No");
handler.post(waitingDialog);
//Wait here for onclick to be called before you proceed
while(!waitingDialog.isFinished())
if (waitingDialog.getDialog()) {
//clicked yes
} else {
//clicked no
}
Related
I'm a new in RxJava in Android development. I had changed in project AsyncTask to RxJava and got a ConcurrentModificationException. Yes, I used collection (sparseArray) but it doesn't matter 'cause exeption was thrown in findViewById.setVisibility. Only when I try to invoke setVisibility. I confused, what I do wrong? I have a TextView in fragment. At first I set up OnClickListener, in listener I init Single.fromCallable, then I set up OnDragListener
TextView tv;
tv.setOnClickListener(v -> {
if (isClickEnable) {
tv.setBackgroundResource(R.drawable.cheap_dark);
cheapInObservable(tv);
}
});
tv.setOnDragListener(new MyDragListener());
private void cheapInObservable(TextView tView) {
Single.fromCallable( () -> tView).subscribeOn(Schedulers.io())
.delay(250, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
.doOnSuccess(this::onSuccessCheapIn)
.subscribe();
}
And in this code I get exeption:
private class MyDragListener implements View.OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
View dragView = (View)event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_ENDED:
if(!event.getResult()) {
if(dragView == v) {
dragView.setVisibility(View.VISIBLE);
activity.findViewById(sparseCheaps.get(dragView.getId())).
setVisibility(View.VISIBLE);
}
}
break;
private void onSuccessCheapIn(TextView tv) {
tv.setVisibility(View.INVISIBLE);
TextView tvd = activity.findViewById(sparseCheaps.get(tv.getId()));
tvd.setVisibility(View.VISIBLE);
AnimatorSet set = new AnimatorSet();
tvd.animate().rotation(0);
int up = activity.findViewById(R.id.guidelineGlowUp).getTop();
int left = getXcoord(tvd);
set.setDuration(400).playTogether(ObjectAnimator.ofFloat(tvd, TextView.TRANSLATION_X,
tvd.getX(), left),
ObjectAnimator.ofFloat(tvd, TextView.TRANSLATION_Y, tvd.getY(), up - 3));
set.setInterpolator(new AccelerateInterpolator((float) 0.4));
set.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
isAnimationCheaps = false;
super.onAnimationEnd(animation);
}
});
set.start();
}
I've found out that only the exeption is thrown when I use setVisibility. If I use AsyncTask instead of Rx it works without exception
StackTrace is:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:795)
at java.util.HashMap$KeyIterator.next(HashMap.java:822)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1154)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1156)
at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:4322)
at android.view.ViewRootImpl.access$1100(ViewRootImpl.java:103)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3407)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
I'm developing an app with eclipse and arduino which connects to a bluetooth module. I can then control the LEDS on the board. However my code shows no errors but the app force closes everytime I hit a button. Here is my code:
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class BluetoothTest extends Activity
{
TextView labelConnect;
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button openButton = (Button)findViewById(R.id.open);
Button closeButton = (Button)findViewById(R.id.close);
Button onButton = (Button)findViewById(R.id.onButton);
Button offButton = (Button)findViewById(R.id.offButton);
Button redButton = (Button)findViewById(R.id.redButton);
Button greenButton = (Button)findViewById(R.id.greenButton);
Button blueButton = (Button)findViewById(R.id.blueButton);
labelConnect = (TextView)findViewById(R.id.mylabel);
//Open Bluetooth
openButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
findBT();
openBT();
}
catch (IOException ex) { }
}
});
//Close Bluetooth
closeButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
closeBT();
}
catch (IOException ex) { }
}
});
//Red Button
redButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
redButton();
}
catch (IOException ex) { }
}
});
//Green Button
greenButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
greenButton();
}
catch (IOException ex) { }
}
});
//Blue Button
blueButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
blueButton();
}
catch (IOException ex) { }
}
});
//On Button - set strip to white
onButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
onButton();
} catch (Exception e) {
// TODO: handle exception
}
}
});
//Off Button - set strip to all OFF
offButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
offButton();
} catch (Exception e) {
// TODO: handle exception
}
}
});
} // end onCreate
void findBT()
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
labelConnect.setText("No bluetooth adapter available");
}
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("BTNode0")) // Change to match RN42 - node name
{
mmDevice = device;
Log.i("ArduinoBT", "findBT found device named " + mmDevice.getName());
Log.i("ArduinoBT", "device address is " + mmDevice.getAddress());
break;
}
}
}
labelConnect.setText("Bluetooth Device Found");
}
void openBT() throws IOException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
labelConnect.setText("BT << " + mmDevice.getName() + " >> is now open ");
}
void closeBT() throws IOException
{
mmOutputStream.close();
mmSocket.close();
labelConnect.setText("Bluetooth Closed");
}
void offButton() throws IOException
{
mmOutputStream.write("0".getBytes());
}
void redButton() throws IOException
{
mmOutputStream.write("1".getBytes());
}
void greenButton() throws IOException
{
mmOutputStream.write("2".getBytes());
}
void blueButton() throws IOException
{
mmOutputStream.write("3".getBytes());
}
void onButton() throws IOException
{
mmOutputStream.write("4".getBytes());
}
}
And here is my log for what happens when I hit the connect button
04-25 12:15:10.771: W/asset(22604): Copying FileAsset 0x74c0a9d8 (zip:/data/app/Android.Arduino.Bluetooth-2.apk:/resources.arsc) to buffer size 1912 to make it aligned.
04-25 12:15:10.821: D/RenderPolicy(22604): ViewRootImpl.enableHardwareAcceleration -> enableRenderPolicy
04-25 12:15:10.881: I/Adreno-EGL(22604): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL4169980)
04-25 12:15:10.881: I/Adreno-EGL(22604): OpenGL ES Shader Compiler Version: 17.01.10.SPL
04-25 12:15:10.881: I/Adreno-EGL(22604): Build Date: 02/04/14 Tue
04-25 12:15:10.881: I/Adreno-EGL(22604): Local Branch:
04-25 12:15:10.881: I/Adreno-EGL(22604): Remote Branch:
04-25 12:15:10.881: I/Adreno-EGL(22604): Local Patches:
04-25 12:15:10.881: I/Adreno-EGL(22604): Reconstruct Branch:
04-25 12:15:12.363: W/dalvikvm(22604): threadid=1: thread exiting with uncaught exception (group=0x41625e18)
04-25 12:15:12.373: E/AndroidRuntime(22604): FATAL EXCEPTION: main
04-25 12:15:12.373: E/AndroidRuntime(22604): Process: Android.Arduino.Bluetooth, PID: 22604
04-25 12:15:12.373: E/AndroidRuntime(22604): java.lang.NullPointerException
04-25 12:15:12.373: E/AndroidRuntime(22604): at Android.Arduino.Bluetooth.BluetoothTest.openBT(BluetoothTest.java:185)
04-25 12:15:12.373: E/AndroidRuntime(22604): at Android.Arduino.Bluetooth.BluetoothTest$1.onClick(BluetoothTest.java:53)
04-25 12:15:12.373: E/AndroidRuntime(22604): at android.view.View.performClick(View.java:4480)
04-25 12:15:12.373: E/AndroidRuntime(22604): at android.view.View$PerformClick.run(View.java:18673)
04-25 12:15:12.373: E/AndroidRuntime(22604): at android.os.Handler.handleCallback(Handler.java:733)
04-25 12:15:12.373: E/AndroidRuntime(22604): at android.os.Handler.dispatchMessage(Handler.java:95)
04-25 12:15:12.373: E/AndroidRuntime(22604): at android.os.Looper.loop(Looper.java:157)
04-25 12:15:12.373: E/AndroidRuntime(22604): at android.app.ActivityThread.main(ActivityThread.java:5872)
04-25 12:15:12.373: E/AndroidRuntime(22604): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 12:15:12.373: E/AndroidRuntime(22604): at java.lang.reflect.Method.invoke(Method.java:515)
04-25 12:15:12.373: E/AndroidRuntime(22604): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
04-25 12:15:12.373: E/AndroidRuntime(22604): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
04-25 12:15:12.373: E/AndroidRuntime(22604): at dalvik.system.NativeStart.main(Native Method)
As they have already said in the comments, the mmDevice you are trying to use in the openBT method is null, because no device was found in the findBT method and the variable was not initialized. You need to fix your code so that you don't try to open the connection if the device was not found.
The other main issue in your code, once you solve this, is the SocketConnection you are trying to open in the Main Thread. Button handlers and GUI events are executed in the main thread, so your openBT should be moved to a separate thread (or better, a Service).
I created an overlay "always on top button", which is a service HUD, and I can't start an activity screen from there, it gives the error: "Unfortunately App has stopped". In the beginning, all I used to know if there was any TouchEventwas a toast, and that toast was created, but it was created several times, so I don't know if it gives that error because this code, which is on TouchEvent body , is repeated several times too.
here is my code:
public class HUD extends Service implements OnClickListener, OnTouchListener, OnLongClickListener {
Button mButton;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
//mView = new HUDView(this);
mButton = new Button(this);
mButton.setId(1);
mButton.setText("Button");
mButton.setClickable(true);
mButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.OPAQUE);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mButton, params);
}
#Override
public void onDestroy() {
super.onDestroy();
if(mButton != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
mButton = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getX()<mButton.getWidth() & event.getY()>0)
{
Toast.makeText(this,"Overlay button event", Toast.LENGTH_SHORT).show(); //this my toast
Intent i = new Intent(); //this is my new acivity (intent)
i.setClass(HUD.this, screen.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
HUD.this.stopSelf();
}
return false;
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(this,"Click", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
System.exit(1);
return false;
}
}
So my question is, is this code on TouchEvent body being repeated several times? If it is, is that the cause of the error?
log cat:
07-20 22:11:06.962: I/Choreographer(1620): Skipped 52 frames! The application may be doing too much work on its main thread.
07-20 22:11:08.062: D/AndroidRuntime(1620): Shutting down VM
07-20 22:11:08.062: W/dalvikvm(1620): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-20 22:11:08.132: E/AndroidRuntime(1620): FATAL EXCEPTION: main
07-20 22:11:08.132: E/AndroidRuntime(1620): android.app.SuperNotCalledException: Activity {com.example.screenshot/com.example.screenshot.screen} did not call through to super.onCreate()
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2146)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.os.Looper.loop(Looper.java:137)
07-20 22:11:08.132: E/AndroidRuntime(1620): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-20 22:11:08.132: E/AndroidRuntime(1620): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 22:11:08.132: E/AndroidRuntime(1620): at java.lang.reflect.Method.invoke(Method.java:511)
07-20 22:11:08.132: E/AndroidRuntime(1620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-20 22:11:08.132: E/AndroidRuntime(1620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-20 22:11:08.132: E/AndroidRuntime(1620): at dalvik.system.NativeStart.main(Native Method)
screen.java:
public class screen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
Toast.makeText(getApplicationContext(), "Made it", 0).show();
finish();
}
}
See android start activity from service
Intent i= new Intent(getBaseContext(), screen.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);
You error seems to be inside screen activity. There are many thread which might help to figure out the error that you are getting for the activity:
Error in Android "SuperNotCalledException:Activity did not call through to super.OnCreate()"
android.app.SuperNotCalledException: Activity did not call through to super.onStop()
Update
The error is because you haven't called: super.onCreate(savedInstanceState); in your screen activity's onCreate(). That should be the first thing to be called in onCreate(). Do something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//.... other stuff
}
Hope this helps.
I am rather new to Android/Java programming and looking for some help with a very, very basic app that I am working with. I am purely building this just to get used to Java coding and building applications for Droids, so bear with me.
I have 10 radio buttons grouped together and only one of them is the right answer (button #10). If the user clicks on any of them 1-9 I have it displaying a message that says "sorry try again", but if they click on #10, I want it to take them to a new activity, namely a page where I will put up some graphics and text that says congraulations and things of that nature. The buttons work fine for 1-9, but when I click on #10 I am gettinga force close error.
Here's what I have so far:
public class MainQuiz extends Activity implements OnCheckedChangeListener, android.widget.RadioGroup.OnCheckedChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_quiz);
((RadioGroup)findViewById(R.id.radio_group)).setOnCheckedChangeListener(this);}
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
String numeral = null;
if (checkedId == R.id.button1) {
numeral = "Nope, try again!";
} else if (checkedId == R.id.button2) {
numeral = "Nope, try again!";
} else if (checkedId == R.id.button3) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button4) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button5) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button6) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button7) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button8) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button9) {
numeral = "Nope, try again!";
}
else if (checkedId == R.id.button10) {
Intent myIntent = new Intent(MainQuiz.this, CorrectAnswer.class);
MainQuiz.this.startActivity(myIntent);
}
Toast.makeText(getApplicationContext(), ""+numeral+"",
Toast.LENGTH_SHORT).show(); }
Any help would be apprecitaed, I am sure this is a simple fix and I am sure it is something glaring in the syntax and code that I have messed up, but again, I am new :)
Thanks!
Here is the log after the Force Close:
11-14 13:56:48.982: D/AndroidRuntime(862): Shutting down VM
11-14 13:56:48.982: W/dalvikvm(862): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-14 13:56:48.992: E/AndroidRuntime(862): FATAL EXCEPTION: main
11-14 13:56:48.992: E/AndroidRuntime(862): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.quiz/com.test.quiz.CorrectAnswer}: java.lang.ClassCastException: com.test.quiz.CorrectAnswer
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.os.Looper.loop(Looper.java:123)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-14 13:56:48.992: E/AndroidRuntime(862): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 13:56:48.992: E/AndroidRuntime(862): at java.lang.reflect.Method.invoke(Method.java:521)
11-14 13:56:48.992: E/AndroidRuntime(862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-14 13:56:48.992: E/AndroidRuntime(862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-14 13:56:48.992: E/AndroidRuntime(862): at dalvik.system.NativeStart.main(Native Method)
11-14 13:56:48.992: E/AndroidRuntime(862): Caused by: java.lang.ClassCastException: com.test.quiz.CorrectAnswer
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-14 13:56:48.992: E/AndroidRuntime(862): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
11-14 13:56:48.992: E/AndroidRuntime(862): ... 11 more
The force close will always put an exception stack trace in your logcat file. Please put this in future posts, it tells us what line crashed and why.
My guess from your code and newness to Android- you probably didn't add the CorrectAnswer activity to your manifest.
I don't know what is your problem, but you can improve your code:
use "switch - case" construction.
switch (chekedId) {
case R.id.button10:
Intent myIntent = new Intent(MainQuiz.this, CorrectAnswer.class);
startActivity(myIntent);
break;
default:
Toast.makeText(getApplicationContext(), "Nope, try again!",
Toast.LENGTH_SHORT).show();
}
It seems like this line is probably what is causing your problems.
MainQuiz.this.startActivity(myIntent);
Remove the MainQuiz, and it should work
this.startActivity(myIntent);
MainQuiz.this.startActivity(myIntent);
replace by
startActivity(myIntent);
I am trying to avoid using onDestroy and want to keep this as simple as possible, but when I exit the program, I get a Force Close error. Not sure why. Here is the code for the main part of the application. Any suggestions?
Main Application Code
public class Quotes extends Activity implements OnClickListener {
ProgressDialog dialog;
private WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView adsview = (WebView) findViewById(R.id.ads);
adsview.getSettings().setJavaScriptEnabled(true);
adsview.loadUrl("http://www.dgdevelco.com/quotes/androidad.html");
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String q = SP.getString("appViewType","http://www.dgdevelco.com/quotes/quotesandroidtxt.html");
String c = SP.getString("appRefreshRate","20");
webview = (WebView) findViewById(R.id.scroll);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new QuotesWebView(this));
webview.loadUrl(q);
ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
timer.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
webview.reload();
}
}, 10, Long.parseLong(c),TimeUnit.SECONDS);
findViewById(R.id.refresh).setOnClickListener(this);
}
#Override
public void onPause(){
super.onPause();
}
#Override
public void onResume(){
super.onResume();
}
public void onClick(View v){
switch(v.getId()){
case R.id.refresh:
webview.reload();
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem about = menu.getItem(0);
about.setIntent(new Intent(this, About.class));
MenuItem preferences = menu.getItem(1);
preferences.setIntent(new Intent(this, Preferences.class));
return true;
}
}
LogCat
07-04 13:34:55.011: INFO/DevicePushListener(1415): Connection State Changed: NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
07-04 13:34:55.011: WARN/WindowManager(1314): Attempted to add application window with unknown token HistoryRecord{40c40f50 com.dge.quotes/.Quotes}. Aborting.
07-04 13:34:55.034: DEBUG/AndroidRuntime(24137): Shutting down VM
07-04 13:34:55.034: WARN/dalvikvm(24137): threadid=1: thread exiting with uncaught exception (group=0x40018560)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): FATAL EXCEPTION: main
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy#40517fb0 is not valid; is your activity running?
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.view.ViewRoot.setView(ViewRoot.java:527)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.app.Dialog.show(Dialog.java:241)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.app.ProgressDialog.show(ProgressDialog.java:107)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.app.ProgressDialog.show(ProgressDialog.java:90)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at com.dge.quotes.QuotesWebView.onPageStarted(QuotesWebView.java:22)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:271)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.os.Looper.loop(Looper.java:123)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at android.app.ActivityThread.main(ActivityThread.java:3806)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at java.lang.reflect.Method.invoke(Method.java:507)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-04 13:34:55.058: ERROR/AndroidRuntime(24137): at dalvik.system.NativeStart.main(Native Method)
07-04 13:34:55.089: WARN/ActivityManager(1314): Force finishing activity com.dge.quotes/.Quotes
Its written in your stacktrace
Unable to add window -- token android.os.BinderProxy#40517fb0 is not valid; is your activity running
You start a thread that does a reload
You then press back
Your activity finished
The thread returns and tries to draw on your activity
Oops its already finished
Take a look at the example code provided on the ScheduledExecutorService documentation page:
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep");
};
final ScheduledFuture beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}}
(http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html)
As others have said, the problem is that the ScheduledExecutorService keeps running and reloading the page even after the user closes that activity. To fix this, you can stop the ScheduledExecutorService in onPause.
Calling scheduleAtFixedFate() returns a ScheduledFuture object. Store this object and then call cancel(true) on it in your onPause() method.
Your activity is already stopped when it reaches that line of code. Refer to the exchange in this "bug" report in the Android forum: http://code.google.com/p/android/issues/detail?id=3953