On Click event for a button in custom dialog in android - java

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

Related

Getting java.lang.NullPointerException when trying to launch activity from presenter using view interface

I am trying to change from one screen to another after a user has signed up. The user is created but I get a java.lang.NullPointerException when I call the view interface from the presenter.
Here is the part in the presenter that creates the user and prompts the change to a login screen.
public void signUp() {
postUserUseCase.setWyat_user(user);
subscription = postUserUseCase
.execute()
.subscribeOn(rx.schedulers.Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<User>() {
#Override
public void onCompleted() {
Log.d("complete","complete");
signUpView.showLoginScreen();
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(User user) {
Log.d("next",user.getEmail());
}
});
}
SignupActivity implements the view interface's showLoginScreen method.
public class SignUpActivity extends AppCompatActivity implements SignUpView {
#Override
public void showLoginScreen() {
Intent loginIntent = new Intent(this,LoginActivity.class);
startActivity(loginIntent);
}
}
The user is created but I get a null pointer onCompleted.
EDIT:
This is different from What is a NullPointerException, and how do I fix it? because if I did the initializing of an interface I get a whole class with the methods that my activity will override. This is a view interface not a variable or a class.I just want to call on method.
EDIT 2: Logcat for the error.
01-14 14:41:49.359 28309-28309/com.wyat.wyat E/AndroidRuntime: FATAL
EXCEPTION: main
java.lang.IllegalStateException: Fatal Exception thrown on
Scheduler.Worker thread.
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:62)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
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:995)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
at dalvik.system.NativeStart.main(Native Method)
Caused by: rx.exceptions.OnCompletedFailedException
at rx.observers.SafeSubscriber.onCompleted(SafeSubscriber.java:90)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:272)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:207)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at android.os.Handler.handleCallback(Handler.java:605) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4517) 
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:995) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at com.wyat.wyat.accounts.presenters.SignUpPresenter$1.onCompleted(SignUpPresenter.java:92)
at rx.observers.SafeSubscriber.onCompleted(SafeSubscriber.java:84)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:272) 
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:207) 
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 
at android.os.Handler.handleCallback(Handler.java:605) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4517) 
at java.lang.reflect.Method.invokeNative(Native Method)

Enable WiFi in AlertDialog crashes

If wifi or mobile data is not enabled, it should ask the user, if he clicks yes in the AlertDialog, it will go to the settings.
This is inside main, on create, when the update button clicked:
btnUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG,"ButtonUpdate clicked");
connectIfNecessary();
currentDate = Calendar.getInstance();
formatter= new SimpleDateFormat("yyyy-MM-dd");
new UpdateDB(MainActivity.this);
txtLastUpdateShow.setText(formatter.format(currentDate.getTime()));
});
This is connectifnecessary:
public void connectIfNecessary(){
Log.v(TAG, "main connectIfNecessary");
if(!isConnected()){
final Context ctx = this;
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setCancelable(true);
builder.setMessage("need internet");
builder.setTitle("do you want to enable it now?");
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
return;
}
});
builder.show();
}
}
this is isConnected:
public boolean isConnected(){
Log.v(TAG, "mainisconnected");
ConnectivityManager conectivtyManager = (ConnectivityManager)
getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected()) {
Log.v(TAG, "mainisconnected true");
return true;
} else {
Log.v(TAG, "mainisconnected false");
return false;
}
}
in updatedb constructor, it will call asynctask to connect websites.
When i run this, and click update button, it crashes because it goes into updatedb withpout asking me in builder. The errors:
FATAL EXCEPTION: AsyncTask #1
Process: com.cursedchico.IstanbulEventPool, PID: 13068
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.cursedchico.IstanbulEventPool.EventRetriever.doInBackground(UpdateDB.java:303)
at com.cursedchico.IstanbulEventPool.EventRetriever.doInBackground(UpdateDB.java:109)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:841) 
01-01 18:48:00.299 13068-13068/com.cursedchico.IstanbulEventPool E/WindowManager: android.view.WindowLeaked: Activity com.cursedchico.IstanbulEventPool.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41e17480 V.E..... R.....I. 0,0-488,216} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:388)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:902)
at com.cursedchico.IstanbulEventPool.MainActivity.connectIfNecessary(MainActivity.java:106)
at com.cursedchico.IstanbulEventPool.MainActivity$6.onClick(MainActivity.java:276)
at android.view.View.performClick(View.java:4508)
at android.view.View$PerformClick.run(View.java:18675)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
01-01 18:48:00.299 13068-13068/com.cursedchico.IstanbulEventPool E/WindowManager: android.view.WindowLeaked: Activity com.cursedchico.IstanbulEventPool.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41e36258 V.E..... R......D 0,0-488,165} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:388)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at android.app.ProgressDialog.show(ProgressDialog.java:117)
at android.app.ProgressDialog.show(ProgressDialog.java:100)
at com.cursedchico.IstanbulEventPool.EventRetriever.onPreExecute(UpdateDB.java:269)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at com.cursedchico.IstanbulEventPool.UpdateDB.<init>(UpdateDB.java:56)
at com.cursedchico.IstanbulEventPool.MainActivity$6.onClick(MainActivity.java:280)
at android.view.View.performClick(View.java:4508)
at android.view.View$PerformClick.run(View.java:18675)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
MainActivity.java:106) is builder.show()
MainActivity.java:276) connectIfNecessary();
I am getting errors for hours, at first about appcompat, then manifest then layout now this.
I use real tablet device.
When device is not connected , following would be execution flow
Get inside connectIfNecessary()
Create Alert to ask user to enable internet. Please note that only Alter object is created at this moment, it will be displayed on screen only after stack of main thread unwinds.
Return from connectIfNecessary() create instance of updateDB(). Inside this application is crashing
Few simple changes would fix your code.
add isConnected() if check before creating instance of updateDB(). This will cover the CONNECTED case
if the device is NOT CONNECTED, user will shown the alter and take to settings screen. If user enables WIFI from settings and comes back, onResume() of your activity will be called. Check connectivity and add updateDB() here.

Setting OnClickListener throws an exception?

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

Unfortunately, (App Name) has stopped Android, Eclipse

I am able to compile, build, and install the project successfully And the project is running well in portrait, But In landscape mode, When I click in button, the app says: Unfortunately has stopped. Could you please help me figure this out?
This is The app Logcat In Landscape mode:
01-24 09:58:19.936: W/dalvikvm(6847): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-24 09:58:19.986: E/AndroidRuntime(6847): FATAL EXCEPTION: main
01-24 09:58:19.986: E/AndroidRuntime(6847): java.lang.NullPointerException
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.Divani.Marzieh.ExamActivity.addItemList(ExamActivity.java:79)
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.Divani.Marzieh.ExamActivity$1.onClick(ExamActivity.java:71)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.view.View.performClick(View.java:3511)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.view.View$PerformClick.run(View.java:14105)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.os.Handler.handleCallback(Handler.java:605)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.os.Looper.loop(Looper.java:137)
01-24 09:58:19.986: E/AndroidRuntime(6847): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-24 09:58:19.986: E/AndroidRuntime(6847): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 09:58:19.986: E/AndroidRuntime(6847): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-24 09:58:19.986: E/AndroidRuntime(6847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-24 09:58:19.986: E/AndroidRuntime(6847): at dalvik.system.NativeStart.main(Native Method)
This is The examActivity Code:
public class ExamActivity extends Activity {
private EditText etInput1;
private EditText etInput2;
private Button btnAdd;
private ListView lvItem;
private ArrayList<Item> itemArrey;
private ArrayAdapter<Item> itemAdapter;
private static TabHost tabHost;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("LIST");
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setIndicator("DETAILS");
spec2.setContent(R.id.tab2);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.setCurrentTab(id.tab1);
setUpView();
}
public static TabHost getCurrentTabHost(){
return tabHost;
}
private void setUpView() {
// TODO Auto-generated method stub
etInput1 = (EditText)this.findViewById(R.id.editText1);
etInput2 = (EditText)this.findViewById(R.id.editText2);
btnAdd = (Button)this.findViewById(R.id.button1);
lvItem = (ListView)this.findViewById(R.id.list);
itemArrey = new ArrayList<Item>();
itemArrey.clear();
itemAdapter = new CustomlistActivity(this, android.R.layout.simple_list_item_1,R.id.textView1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
if (isInputValid(etInput1) && isInputValid(etInput2)) {
if(((RadioButton)findViewById(R.id.radio0)).isChecked())
itemArrey.add(new Item(R.drawable.t,etInput1.getText().toString()+"\n"+etInput2.getText().toString()));
else if(((RadioButton)findViewById(R.id.radio1)).isChecked())
itemArrey.add(new Item(R.drawable.s,etInput1.getText().toString()+"\n"+etInput2.getText().toString()));
else if(((RadioButton)findViewById(R.id.radio2)).isChecked())
itemArrey.add(new Item(R.drawable.d,etInput1.getText().toString()+"\n"+etInput2.getText().toString()));
itemAdapter.notifyDataSetChanged();
ExamActivity.getCurrentTabHost().setCurrentTab(0);
}
}
protected boolean isInputValid(EditText etInput2) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
etInput2.setError("Please Enter Item");
return false;
} else {
return true;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater .inflate(R.menu.mymenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
EditText e = (EditText)findViewById(R.id.editText3);
switch (item.getItemId()) {
case R.id.item1:
Toast toast = Toast.makeText(ExamActivity.this, e.getText().toString(), 5000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
whenever you change the orientation of the screen the activity gets restarted after that the onStart() method is called.
Initialize your code that you are using at ExamActivity.addItemList(ExamActivity.java:79) in your onStart() method ex: your list or array
go through the site http://www.vogella.com/tutorials/AndroidLifeCycle/article.html#configurationchange
Do you have a separate layout main.xml for landscape, ie layout/main.xml and layout-land/main.xml ? If so then check that layout-land/main.xml is not missing some or all of the radio buttons radio0/1/2.

Start activity from service isn't working (Android)

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.

Categories

Resources