(ImageView)findViewById()始终返回null

时间:2016-08-31 00:19:09

标签: java android xml nullpointerexception

我遇到一个问题,其中findViewById()总是在我的对话框中为ImageView返回null:

 private void startDetailView(int num)throws NullPointerException{

    Dialog detailed = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    detailed.setContentView(R.layout.activity_detail_view);
    TextView text = (TextView)detailed.findViewById(R.id.detailedTextView);
    ImageView picture = (ImageView)findViewById(R.id.detailedImageView);
    detailed.show();

    /*picture.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            final Dialog dial = new Dialog(ScrollActivity.this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
            dial.setContentView(R.layout.picture_view);
            FrameLayout cont = (FrameLayout)findViewById(R.id.container);
            cont.setBackground(getResources().getDrawable(R.drawable.vine));
            dial.show();

            cont.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    dial.dismiss();
                }
            });
        }
    });*/
}

XML:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/detailedImageView"
    android:layout_below="@+id/detailedTextView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="134dp" />

错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.FrameLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at at.tryouts.mcfuckinvineapp.wein.ScrollActivity$1.onClick(ScrollActivity.java:78)
        at android.view.View.performClick(View.java:5280)
        at android.view.View$PerformClick.run(View.java:21239)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:234)
        at android.app.ActivityThread.main(ActivityThread.java:5526)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这也发生在第二个Dialog中的FrameLayout中。只有这两个受影响,我可以毫无问题地添加任何其他视图。

我希望你能帮助我,我无法想象它有什么问题。

1 个答案:

答案 0 :(得分:0)

从详细视图中可能有错误的活动视图。我打赌你的ImageView在详细视图下,因为它是&#34; layout_below:detailedTextView&#34;。尝试将其更改为

ImageView picture = (ImageView) detailed.findViewById(R.id.detailedImageView);