使用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();
}
}
这是调用警报对话框的地方(这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将上下文传递为空,这就是问题所在。给出一些适当的值,你可能不会得到它。
这些行是从您的代码中选取的。这就是导致问题的原因。
You are passing context as null, that is the problem. Give some appropriate value and you will not get it probably.
These lines are picked from your code. This is causing the problem.