1

I am making an app in which I have made three classes - MainActivity, Add and MyApp. I have done entry of MyApp in manifest file ( android:name="com.example.add.MyApp") in application.

MyApp.java Code is -

 package com.example.add; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Application; import android.os.Build; @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); tyc(); } public void tyc() { // TODO Auto-generated method stub add nn = new add(this); nn.Toastby(); } } 

My Add.java file is -

 package com.example.add; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.widget.Toast; public class add { Context context; AlertDialog.Builder alertDialogBuilder; public add(Context context) { this.context = context; } public void Toastby() { Toast.makeText(context, "hi..", Toast.LENGTH_LONG).show(); alertDialogBuilder = new AlertDialog.Builder( context); // set title alertDialogBuilder.setTitle("Your Title"); // set dialog message alertDialogBuilder .setMessage("Click yes to exit!") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { dialog.cancel(); } }) .setNegativeButton("No",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } } 

My MainActivity.java Code is -

 package com.example.add; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } 

}

My Manifest.java is -

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.add" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="20" /> <application android:allowBackup="true" android:name="com.example.add.MyApp" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 

Now, The issue is that there is an error in Dialog builder. The error which I am getting is as follows -

enter image description here

1
  • why are you trying to do this in your Application class? Commented Sep 16, 2014 at 6:51

2 Answers 2

3

You cannot use the Application context for creating UI elements like Dialogs. This Line in your Add.java File causes the problem: alertDialogBuilder = new AlertDialog.Builder(context); because you are passing an Application Context instead of an Activity context.

Just move your Code into your Activity and it should work fine.

Move this into your Activity:

public void tyc() { // TODO Auto-generated method stub add nn = new add(this); nn.Toastby(); } 

then call in onCreate() again in Activity not in the Application class!

tyc(); 
Sign up to request clarification or add additional context in comments.

6 Comments

i just want into main activity.... when activity Oncreate method is fire...ONcreate methos should work for displaying AlertDialoge Boz
I updated the answer. Is it clearer now? You cannot create a Dialog from the Application context that's why the tyc function needs to be in your Activity.
I have done this code so that I get the dialog box on each activity of my app..I have tested for the Toast but it is coming fine but the dialog box is not coming...what should I do?
You can create an abstract BaseActivity, put your code in its onCreate method and then you have to subclass this Activity like this: public class MainActivity extends BaseActivity {} Every Activity that extends BaseActivity would then display that popup. The reason that a Toast works is because it doesn't need an Activity context.
I just told you. If you don't understand what I mean update your initial question with your exact use case and I can try to give you the code you need.
|
0

Remove MyApp and create your dialog in MainActivity ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.