使用AlertDialog时捕获异常?

发布于 2024-12-10 18:23:42 字数 2358 浏览 0 评论 0原文

我试图将我的警报对话框放在一个与主类不同的类中,当它被调用时,它也会出现错误并且应用程序停止,我想知道如何捕获异常或如何在代码中找到错误像这样。

这是警报对话框的代码:

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PlacepinDialog extends AlertDialog{

    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    TextView text;
    ImageView image;
    Button place;
    Button cancel;
    LayoutInflater inflater;
    View layout;

    public PlacepinDialog(Context context) {
        super(context);
        //Setting up View and Inflater
        LayoutInflater inflater = (LayoutInflater)getLayoutInflater();
        View layout = inflater.inflate(R.layout.placepin_dialog,
                (ViewGroup) findViewById(R.id.mvMain));

        //Text Views
        TextView text = (TextView) layout.findViewById(R.id.Placetext);
        text.setText("Do you want to place a pin at the location you pressed?");

        //Image Views
        ImageView image = (ImageView) layout.findViewById(R.id.Placeimage);
        image.setImageResource(R.drawable.icon);

        //Building the Dialog
        builder = new AlertDialog.Builder(context);
        builder.setView(layout);
        alertDialog.setTitle("Place Pin");
        alertDialog = builder.create();
    }
    }

这是调用警报对话框的地方(这个 onTouchEvent 位于主类之外的另一个类中,所以我不能只执行 main.this ):

public boolean onTouchEvent(MotionEvent e, MapView mv){
        int i = e.getAction();

        switch(i){

        case MotionEvent.ACTION_DOWN:
            //When your finger touches the screen
            Log.d("ACTION DOWN", "Finger touched screen");

            break;

        case MotionEvent.ACTION_UP:
            //When your finger stop touching the screen
            Log.d("ACTION UP", "Finger was removed from screen");
            try{
            PlacepinDialog alertDialog = new PlacepinDialog(null);
            }
            catch(){

            }
            break;

        case MotionEvent.ACTION_MOVE:
            //When your finger moves around the screen
            Log.d("ACTION MOVE", "Finger was moved around the screen");

            break;
        }

        return false;
    }

Im trying to make my alertdialog in a separate class than my main class and when it gets called it also gets and error and the app stops, I would like to know how i would catch the exception or how i would find the error in a code like this.

Here is the code for the alert dialog:

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PlacepinDialog extends AlertDialog{

    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    TextView text;
    ImageView image;
    Button place;
    Button cancel;
    LayoutInflater inflater;
    View layout;

    public PlacepinDialog(Context context) {
        super(context);
        //Setting up View and Inflater
        LayoutInflater inflater = (LayoutInflater)getLayoutInflater();
        View layout = inflater.inflate(R.layout.placepin_dialog,
                (ViewGroup) findViewById(R.id.mvMain));

        //Text Views
        TextView text = (TextView) layout.findViewById(R.id.Placetext);
        text.setText("Do you want to place a pin at the location you pressed?");

        //Image Views
        ImageView image = (ImageView) layout.findViewById(R.id.Placeimage);
        image.setImageResource(R.drawable.icon);

        //Building the Dialog
        builder = new AlertDialog.Builder(context);
        builder.setView(layout);
        alertDialog.setTitle("Place Pin");
        alertDialog = builder.create();
    }
    }

here is where the alertdialog gets called(this onTouchEvent is in another class than the main class so i can't just do main.this):

public boolean onTouchEvent(MotionEvent e, MapView mv){
        int i = e.getAction();

        switch(i){

        case MotionEvent.ACTION_DOWN:
            //When your finger touches the screen
            Log.d("ACTION DOWN", "Finger touched screen");

            break;

        case MotionEvent.ACTION_UP:
            //When your finger stop touching the screen
            Log.d("ACTION UP", "Finger was removed from screen");
            try{
            PlacepinDialog alertDialog = new PlacepinDialog(null);
            }
            catch(){

            }
            break;

        case MotionEvent.ACTION_MOVE:
            //When your finger moves around the screen
            Log.d("ACTION MOVE", "Finger was moved around the screen");

            break;
        }

        return false;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青丝拂面 2024-12-17 18:23:42

您将上下文传递为空,这就是问题所在。给出一些适当的值,你可能不会得到它。

    PlacepinDialog alertDialog = new PlacepinDialog(null);

public PlacepinDialog(Context context)

builder = new AlertDialog.Builder(context); // context is null.

这些行是从您的代码中选取的。这就是导致问题的原因。

You are passing context as null, that is the problem. Give some appropriate value and you will not get it probably.

    PlacepinDialog alertDialog = new PlacepinDialog(null);

public PlacepinDialog(Context context)

builder = new AlertDialog.Builder(context); // context is null.

These lines are picked from your code. This is causing the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文