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 -