【问题标题】:Using click listener on surfaceview inflated layout在surfaceview膨胀布局上使用点击监听器
【发布时间】: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


【解决方案1】:

您应该从 dialogLayout 找到 ImageView,因为这是您正在使用的 Dialog 的布局。

更新后的代码应该是:

ImageView tweet = (ImageView) dialoglayout.findViewById(R.id.dialog_tweet);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多