gpt4 book ai didi

android - 使用AlertDialog时捕获异常?

转载 作者:行者123 更新时间:2023-12-03 08:15:47 27 4
gpt4 key购买 nike

我试图将我的alertdialog与我的主类放在一个单独的类中,并且当它被调用时它也会得到并且错误并且应用程序停止,我想知道我将如何捕获异常或如何在代码中发现错误像这样。

这是警报对话框的代码:

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

这是调用alertdialog的地方(此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;
}

最佳答案

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

    PlacepinDialog alertDialog = new PlacepinDialog(null);

public PlacepinDialog(Context context)

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

这些行是从您的代码中选择的。这引起了问题。

关于android - 使用AlertDialog时捕获异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7786295/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com