【发布时间】:2012-12-27 01:55:07
【问题描述】:
我有一个surfaceview,它打开一个带有膨胀布局的alertdialog,我想在这个布局的imageview上使用一个图像onclicklistener,但是每当我尝试加载对话框时都会出现nullpointerexception,当我设置onclicklistener时会发生这种情况到 imageview 'tweet'。如果我删除侦听器,它将加载而不会出现错误。下面是我的代码
activity.runOnUiThread(new Runnable(){
public void run() {
AlertDialog.Builder adb = new AlertDialog.Builder(Panel.this.context);
try{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.activity_anime_action, null);
adb.setView(dialoglayout)
.setNegativeButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent myIntent = new Intent(context, ActivityOne.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(myIntent);
((Activity) context).finish();
dialog.cancel();
}
});
ImageView tweet = (ImageView)((Activity) context).findViewById(R.id.dialog_tweet);
tweet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(VIEW_LOG_TAG, "WORKD");
}
});
}catch(Exception e){Log.d("INFLATER ERROR", e.toString());}
adb.show();
}});
【问题讨论】:
-
您应该发布您的 LogCat(堆栈跟踪)并标记发生异常的行。
-
目前,try and catch 只是打印出 java.lang.nullPointerException,当我将 onclicklistener 设置为 imageview 'tweet' 时会发生这种情况。如果我删除监听器,它将加载没有错误
-
如果您改为使用
dialoglayout.findViewById(R.id.dialog_tweet);会怎样?您似乎使用了错误的布局来查找您的ImageView -
那行得通,请将您的回复放在答案中,以便我接受:)。我看了看我正在使用的视图 lol
标签: android surfaceview