1

我必须在 java 警报中弹出一个对话框,通知用户如果他想删除广告必须购买付费版本的应用程序......我做了两个 java 文件..另一个叫做对话,从中回忆上面布局xml中定义的文件和其他元素...问题是如果我放置新对话框(this)会给我错误...而不是如果当前通过单击复选框运行应用程序会使应用程序崩溃。崩溃后的复选框已检查应该有...复选框不能选中。

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

    }
4

1 回答 1

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;
            }
        });
    }
}
于 2013-06-28T20:35:57.070 回答