gpt4 book ai didi

java - Android 中 Dialog Builder 中的运行时异常

转载 作者:行者123 更新时间:2023-11-29 05:18:00 24 4
gpt4 key购买 nike

我正在制作一个应用程序,其中我制作了三个类 - MainActivity、Add 和 MyApp。我已经在应用程序的 list 文件(android:name="com.example.add.MyApp")中完成了 MyApp 的输入。

MyApp.java 代码是——

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

}
}

我的 Add.java 文件是 -

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

}

我的 MainActivity.java 代码是 -

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

}

我的 Manifest.java 是 -

      <?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>

现在,问题是对话框生成器中存在错误。我得到的错误如下 -

enter image description here

最佳答案

您不能使用应用程序上下文来创建像对话框这样的 UI 元素。 Add.java 文件中的这一行导致了问题:alertDialogBu​​ilder = new AlertDialog.Builder(context); 因为您传递的是应用程序上下文而不是 Activity 上下文。

只需将您的代码移动到您的 Activity 中,它应该可以正常工作。

将其移至您的 Activity :

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

然后在 Activity 中再次调用 onCreate() 而不是在 Application 类中!

tyc();

关于java - Android 中 Dialog Builder 中的运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25862211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com