WDLinken
WDLinken

Reputation: 3

(ImageView)findViewById() always returns null

I got a problem where findViewById() always returns null for the ImageView in my Dialog:

 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" />

The Error:

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)

This also occurs in the FrameLayout in the second Dialog. Interstingly only these two are affected, I can add any other View without any problem.

I hope you can help me, I can't imagine what's wrong with it.

Upvotes: 0

Views: 1183

Answers (1)

Android.K.Doe
Android.K.Doe

Reputation: 136

It may have mistaken Activity view from Detailed view. I bet your ImageView is under Detailed View since it was "layout_below:detailedTextView". Try Changing it to

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

Upvotes: 1

Related Questions