DialogFragment - 从db到MainActivity Android Studio的引用

时间:2016-10-14 22:03:24

标签: android alertdialog android-dialogfragment

在我的数据库中,当条件满足时,我想要出现一个AlertDialog弹出窗口。我尝试过两种方式,比如这个

数据库处理器

Cursor c = db.rawQuery(selectQuery, null);

if (c.moveToFirst()) {
 result = c.getInt(c.getColumnIndex(KEY_COUPONS_TIMES_TO_USE))
           -c.getInt(c.getColumnIndex(KEY_COUPONS_TIMES_USED));
if (result < 4) {
    MainActivity alert = new MainActivity();
    alert.showAlert();
 }
}

MainActivity

public void showAlert() {

AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
myAlert.setMessage("Text").create() 
myAlert.show();

}

代码中没有红色错误,但在使用应用程序时满足条件时,屏幕上会出现严重错误。这是控制台所说的

D/QUERY: UPDATE coupons SET times_used = times_used + 1  WHERE id=1
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with 
uncaught exception(group=0xa4ccfb20)

[10-17 06:13:14.364  1579: 1724 D/]
HostConnection::get() New Host Connection established 0xb9986d90, tid 1724
E/AndroidRuntime: FATAL EXCEPTION: main

Process: promocje, PID: 2744 java.lang.NullPointerException 

at android.content.ContextWrapper.getApplicationInfo
(ContextWrapper.java:152)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
at promocje.MainActivity.showAlert(MainActivity.java:164)    
at promocje.db.engine.DatabaseHandler.useCoupon
(DatabaseHandler.java:889)
at promocje.adapter.CouponListAdapter.useCoupon
(CouponListAdapter.java:166)
at promocje.adapter.CouponListAdapter$1$1.onFinish
(CouponListAdapter.java:144)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Application terminated. 

我也尝试过以替代方式

数据库处理器

Cursor c = db.rawQuery(selectQuery, null);

if (c.moveToFirst()) {
 result = c.getInt(c.getColumnIndex(KEY_COUPONS_TIMES_TO_USE)) 
           - c.getInt(c.getColumnIndex(KEY_COUPONS_TIMES_USED));
if (result < 4) {
    PopUpFragment popup = new PopUpFragment();
    popup.show(FragmentManager manager, String tag);
}

带有Dialog Fragment的新java文件

public class PopUpFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("I'm a PopUp")
            .setPositiveButton("Button 1", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // POSITIVE BUTTON
                }
            })
            .setNegativeButton("Delete me", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
 }
}

这里我在.show()方法中遇到了一些参数问题。

任何帮助将不胜感激!

0 个答案:

没有答案