User
User

Reputation: 1488

Catching exceptions when using AlertDialog?

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

Upvotes: 0

Views: 178

Answers (1)

Yashwanth Kumar
Yashwanth Kumar

Reputation: 29121

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.

Upvotes: 2

Related Questions