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.
Related
I want to know why my application is throwing a runtime exception when I run it? I used to the code in the following link for my application,
dynamically add and remove view to viewpager.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ab=getActionBar();
pagerAdapter = new MainPagerAdapter();
pager = (ViewPager) findViewById (R.id.pager);
pager.setAdapter (pagerAdapter);
inflater = (LayoutInflater)getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v0 = (FrameLayout) inflater.inflate (R.layout.listlayout, null);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
pagerAdapter.addView (v0, 0);
pagerAdapter.notifyDataSetChanged();
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.button1)
{
pagerAdapter.addView (v0);
pagerAdapter.notifyDataSetChanged();
}
}
LogCat:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolistworkable/com.example.todolistworkable.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)Caused by: java.lang.NullPointerException
at com.example.todolistworkable.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
the object on line 43 of your code within your onCreate method is null. Most likely the target of one of your findViewById calls is not returning what you expect it to.
See this for more info.
How to interpret Logcat
I am using an custom dialog and i want to get the click of the button, and nothing is happening when the button is clicked. Please have a look at it.
public class custom_dialog extends Dialog {
Context m_context;
public custom_dialog (Context context) {
super(context);
// TODO Auto-generated constructor stub
this.m_context = context;
}
public custom_dialog (Context context, int theme) {
super(context,theme);
// TODO Auto-generated constructor stub
this.m_context = context;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onClick(View view) {
switch(view.getId()) {
case R.id.blue0:
Toast.makeText(getApplicationContext(),"hii..",Toast.LENGTH_SHORT).show();
break;
}
}
}
This is the error i get in logcat.
06-01 23:41:14.062 1512-1543/? I/InputDispatcher: Delivering touch to current input target
06-01 23:41:14.062 1512-1543/? I/InputDispatcher: Delivering touch to current input target
06-01 23:41:14.062 1512-1543/? I/InputDispatcher: Delivering touch to current input target
06-01 23:41:14.250 1512-1544/? I/InputReader: dispatchTouch::touch event's action is 1
06-01 23:41:14.250 1512-1543/? I/InputDispatcher: Delivering touch to current input target
06-01 23:41:14.328 2954-2954/com.aavishkaar.quickies W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-01 23:41:14.351 2954-2954/com.aavishkaar.quickies E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:2144)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at android.view.View$1.onClick(View.java:2139)
... 11 more
Caused by: java.lang.NullPointerException
at com.aavishkaar.quickies.MainActivity$custom_dialog.onCreate(MainActivity.java:238)
at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
at android.app.Dialog.show(Dialog.java:225)
at com.aavishkaar.quickies.MainActivity.button1(MainActivity.java:96)
... 14 more
06-01 23:41:14.359 1512-10029/? E/liblog: failed to call dumpstate
06-01 23:41:14.359 1512-1524/? E/: Dumpstate > /data/log/dumpstate_app_error
06-01 23:41:14.359 1512-10029/? D/PowerManagerService: acquireWakeLock flags=0x1 tag=ActivityManager-Launch
06-01 23:41:16.531 1512-1536/? D/BatteryService: update start
06-01 23:41:16.937 1566-1566/? E/StatusBarPolicy: ecio: 19
06-01 23:41:16.937 1566-1566/? E/StatusBarPolicy: iconLevel: 4
This is the button event which calls the custom dialog.
public void button1(View view)
{
final custom_dialog dialog = new custom_dialog(this,R.style.Theme_Dialog);
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = 0.1f;
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
// set the custom dialog components - text, image and button
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager)dialog. findViewById(R.id.pager);
awesomePager.setAdapter(awesomeAdapter);
dialog.show();
}
public class custom_dialog extends Dialog implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
findViewById(R.id.blue0).setOnClickListener(this);
}
}
your custom_dialog has to implement OnClickListener and you have to set "this" as click listener for your button
You need to set the buttons onClickListener in the onCreate methody
Button myButton = findViewById(R.id.blue0);
myButton.setOnClickListener(this);
and implement the View.onClickListener on the class
In my android application , I have 3 dialogue boxes in which the user puts info into 3 editTexts and it will display the one of the data onto another class/page after it randomly picks which data to choose.
This is my mainClass
public class MainActivity extends Activity {
//Variables are displayed in this area
String choices[] = new String[3];
//EditText editText;
//EditText editText2;
//EditText editText3;
Button mainButton ;
Random rand = new Random();
int finalChoice;
String displayChoice = "";
EditText editText ;
EditText editText2;
EditText editText3;
EditText editText4;
String test;
int count = 3;
//--------------------------- --------------------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Makes Button
setContentView(R.layout.activity_main);
declareTextBox();
setButton();
String choice1 = editText.getText().toString();
String choice2 = editText2.getText().toString();
String choice3 = editText3.getText().toString();
choices[0] = choice1; //pass from click button to method.
choices[1] = choice2;
choices[2] = choice3;
finalChoice =rand.nextInt(2);
}
public void setButton()
{
final Button mainbutton = (Button) findViewById(R.id.mainButton);
mainbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
test = ((EditText) findViewById(R.id.editText4)).getText().toString();
// count++;
//retChoice();
// loadScreen();
Intent i = new Intent(MainActivity.this, resultScreen.class);
i.putExtra("display" , displayChoice);
Intent intent = new Intent(MainActivity.this,loadingScreen.class);
//start the second Activity
MainActivity.this.startActivity(intent);
}
});
}
public void declareTextBox()
{
editText = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
}
public void getString(String finalChoice)
{
finalChoice = displayChoice;
}
public String retChoice()
{
displayChoice = choices[finalChoice];
return displayChoice;
}
public void clearText()
{
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText(" ");
}
});
editText2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText2.setText(" ");
}
});
editText3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText3.setText(" ");
}
});
}
public void getText2()
{
}
public void getText()
{
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is my Display class :
public class resultScreen extends Activity {
MainActivity ma = new MainActivity();
//Method supposedly retrieves the string data from MainActivity Class but somehow displayed null instead.
//Find a way to keep the string variable when transfering from one class to another class.
String finalResult = ma.retChoice();
public void onCreate(Bundle resultScreen){
super.onCreate(resultScreen);
setContentView(R.layout.resultscreen);
//ma.displayChoice.toString();
String str = finalResult;
TextView text = (TextView) findViewById(R.id.textView1);
text.setText(str);
final Button backbutton = (Button) findViewById(R.id.backButton);
backbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(resultScreen.this,MainActivity.class);
resultScreen.this.startActivity(intent);
}
});
}
}
And this is my loading screen class right after the main button is clicked
public class loadingScreen extends Activity{
protected void onCreate(Bundle loadingScreen) {
// TODO Auto-generated method stub
super.onCreate(loadingScreen);
setContentView(R.layout.loadingscreen);
//If sound clip 20 sec long we don't want to carryit outside next class
// A timer thread looking for "run" method
Thread timer = new Thread()
{
public void run() {
try {
//this is how many mil sec
sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent(loadingScreen.this, resultScreen.class);
loadingScreen.this.startActivity(intent);
loadingScreen.this.finish();
}
}
};
timer.start();
}
}
My app crashes a few seconds into the loading screen when the program attempts to display the data on the resultScreen.
Here's my logCat
05-17 22:32:54.446: D/AndroidRuntime(1181): Shutting down VM
05-17 22:32:54.446: W/dalvikvm(1181): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
05-17 22:32:54.636: E/AndroidRuntime(1181): FATAL EXCEPTION: main
05-17 22:32:54.636: E/AndroidRuntime(1181): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.choiceprototest/com.example.choiceprototest.resultScreen}: java.lang.NullPointerException
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.os.Looper.loop(Looper.java:137)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.reflect.Method.invoke(Method.java:511)
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-17 22:32:54.636: E/AndroidRuntime(1181): at dalvik.system.NativeStart.main(Native Method)
05-17 22:32:54.636: E/AndroidRuntime(1181): Caused by: java.lang.NullPointerException
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.example.choiceprototest.MainActivity.retChoice(MainActivity.java:101)
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.example.choiceprototest.resultScreen.<init>(resultScreen.java:18)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.Class.newInstanceImpl(Native Method)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.Class.newInstance(Class.java:1319)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-17 22:32:54.636: E/AndroidRuntime(1181): ... 11 more
05-17 22:33:03.385: I/Process(1181): Sending signal. PID: 1181 SIG: 9
05-17 22:33:05.435: E/Trace(1204): error opening trace file: No such file or directory (2)
Thanks guys.
I believe I see the problem. You are trying to access your Array but it is an instance variable instead of static so it is being killed off when you exit your Activity. You can either put them in a static class and access them that way or pass a String variable through your Intent, depending on what you need.
If you have the value of the String you need when you leave MainActivity then I would probably just pass it through your Intent. If the value depended on something in one of your other Activities then I would consider putting it in a separate class but it doesn't look like that's what you need here
Edit
You are already doing it here, kind of
Intent i = new Intent(MainActivity.this, resultScreen.class);
i.putExtra("display" , displayChoice);
Intent intent = new Intent(MainActivity.this,loadingScreen.class);
But you aren't starting the Activity that would get the String
i.putExtra("display" , displayChoice);
that line would pass a String with key "displpay" and value of displayChoice, although I think that value is an empty String at that point, but you don't start that Activity. I'm lost on how your flow is suppose to work but basically you would do something like this
String displayChoice = "testValue"; // some arbitrary value--whatever you need to send
Intent i = new Intent(MainActivity.this, resultScreen.class); // create the Intent
i.putExtra("display" , displayChoice); // add the extra
startActivity(i); //start the Activity
then to get the value inside resultScreen in onCreate()
Intent recIntent = getIntent(); // get the Intent that started this Activity
String value = recIntent.getStringExtra("display"); // get the value using the key
// value now equals "testValue"
I think your app chash because you have an ANR: "Application Not Responding" because you are running a long procces inside the UIThread. (onCreate() method is form the UIThread)
Use rather as an Asyntack for your sleeping thread or one handler (with messages).
If you need more I can edit your code tomorrow.
i have Target.java(it is the main activity) and Layout.java.
When user clicks a button a url opens.
The button click is handled in Layout.java.
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DebugLog.LOGD("Within onClick event of 'GO' button ");
Target.openURL();
}
});
Target.java has a static function openURL
public static void openURL() {
Uri uri = Uri.parse("http://www.thecinema.in");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
I'd like to know how i can make this code work, since the error im getting now is "Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity" . I can understand startActivity will not work within a static function, but please suggest me how i could acheive the objective ..Im a newbie to android.FYI ..Thanks guys
-Updated code :
Layout.java
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DebugLog.LOGD("Within onClick event of 'GO' button ");
Target iA = new Target();
iA.openURL();
}
});
Target.java
public void openURL() { //removed static keyword
DebugLog.LOGD("Within nDelete event of GO button ");
Uri uri = Uri.parse("http://www.thecinema.in");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Exception thrown:
03-02 10:10:28.351: D/QCAR(6820): Within onClick event of 'GO' button
03-02 10:10:28.351: W/dalvikvm(6820): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
03-02 10:10:28.361: E/AndroidRuntime(6820): FATAL EXCEPTION: main
03-02 10:10:28.361: E/AndroidRuntime(6820): java.lang.NullPointerException
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.app.Activity.startActivityForResult(Activity.java:2817)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.app.Activity.startActivity(Activity.java:2923)
03-02 10:10:28.361: E/AndroidRuntime(6820): at com.ingage.pocs.phonegap.Layout$4.onClick(Layout.java:124)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.view.View.performClick(View.java:2408)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.view.View$PerformClick.run(View.java:8816)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.os.Handler.handleCallback(Handler.java:587)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.os.Looper.loop(Looper.java:123)
03-02 10:10:28.361: E/AndroidRuntime(6820): at android.app.ActivityThread.main(ActivityThread.java:4633)
Guys what went wrong in here..
i was able to come up with a solution for this question.
Under Target.java i created openURL() as:
public void openURL(Context context)
{
Intent intent = new Intent("com.ingage.pocs.phonegap.URL");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
I created a new Activity OpenURL.java and associated the intent "com.ingage.pocs.phonegap.URL" with it:
package com.ingage.pocs.phonegap;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class OpenURL extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
DebugLog.LOGD("Within BookTicketsActivity Activity GO button ");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.thecinema.in"));
startActivity(browserIntent);
}
}
Layout.java:
private Context applicationContext;
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DebugLog.LOGD("Within onClick event of 'GO' button ");
ImageTargets ia =new ImageTargets();
ia.openURL(applicationContext);
iA.openURL();
}
});
The best thing is, it worked :) If you guys come up with a better or more sensible solution , pls do post here.thank you
startActivity is not static and since you call it from Target not Layout it cannot resolve call context.
You'll need to call startActivity from Layout.java or pass reference or Target to Layout class
do it like this
Layout.java:
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DebugLog.LOGD("Within onClick event of 'GO' button ");
Target.openURL(Layout.this);
}
});
Target.java:
public static void openURL(Context context) {
Uri uri = Uri.parse("http://www.thecinema.in");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
}
You have
Targets iA = new Targets();
Did you, perhaps, mean to write
Target iA = new Target();
That would throw a NullPointerException because you're asking Java to find a class named Targets when none exists.
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
}