简体   繁体   中英

Run time exception in Dialog Builder in Android

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 -

在此输入图像描述

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();

删除MyApp并在MainActivity中创建对话框...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM