AlertDialog中的复选框

时间:2013-06-28 17:56:24

标签: java android checkbox alertdialog

我必须在java警报中调出一个对话框,告知用户是否要删除广告必须购买付费版本的应用程序...我做了两个java文件..另一个叫做对话回想一下上面的文件和布局xml中定义的其他元素......问题是,如果我把新的Dialog(这个)给我错误...相反,如果当前通过单击复选框运行应用程序崩溃应用程序。复选框在崩溃之后检查了应该...复选框不能可检查。

package com.X.XX.XXX;


import android.app.AlertDialog;
import android.content.Context;

public class Dialogo {

    public static final int BUY = 1000;
    AlertDialog.Builder builder;

    public void mostra(int Id) {
        // TODO Auto-generated method stub
        switch(Id)
        {
        case BUY:
            builder.setTitle("Title");
            builder.setMessage("TEXT ");
            builder.setNegativeButton("CLOSE", null);
            builder.setPositiveButton("BUY", null);
            break;
        }
        builder.create().show();
    }

}

package com.X.XX;

import com.X.XX.XXX.Dialog;

import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class Settings extends PreferenceActivity{

    CheckBoxPreference no_ads;
    Dialogo dialogo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.addPreferencesFromResource(R.xml.settings);
        dialogo = new Dialogo(); 

        no_ads = (CheckBoxPreference) this.findPreference("no_ads");
        no_ads.setOnPreferenceClickListener(new OnPreferenceClickListener(){

            @Override
            public boolean onPreferenceClick(Preference preference) {
                // TODO Auto-generated method stub
                dialogo.mostra(Dialogo.BUY);
                ((CheckBoxPreference)preference).setChecked(false);
                return false;
            }});

    }

1 个答案:

答案 0 :(得分:1)

您必须在使用之前初始化构建器变量。 因此,请确保将上下文传递给您的函数,首先将其声明为:

public void mostra(Context context, int Id) {
    builder = new AlertDialog.Builder(context);

然后像这样称呼它:

dialogo.mostra(Settings.this, Dialogo.BUY);

完整的工作示例:

package com.X.XX.XXX;

import android.app.AlertDialog;
import android.content.Context;

public class Dialogo {
    public static final int BUY = 1000;

    public void mostra(Context context, int Id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        // TODO Auto-generated method stub
        switch(Id)
        {
        case BUY:
            builder.setTitle("Title");
            builder.setMessage("TEXT ");
            builder.setNegativeButton("CLOSE", null);
            builder.setPositiveButton("BUY", null);
            break;
        }
        builder.create().show();
    }
}


package com.X.XX;
import com.X.XX.XXX.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class Settings extends PreferenceActivity{
    CheckBoxPreference no_ads;
    Dialogo dialogo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.addPreferencesFromResource(R.xml.settings);
        dialogo = new Dialogo(); 

        no_ads = (CheckBoxPreference) this.findPreference("no_ads");
        no_ads.setOnPreferenceClickListener(new OnPreferenceClickListener(){
            @Override
            public boolean onPreferenceClick(Preference preference) {
                // TODO Auto-generated method stub
                dialogo.mostra(Settings.this, Dialogo.BUY);
                ((CheckBoxPreference)preference).setChecked(false);
                return false;
            }
        });
    }
}