0

I'm trying to change an ImageView source on a Fragment from the MainActivity or from onDraw from within the Fragment.

I've currently got it as so:

public class FragmentMainActivity extends Fragment {

Paint paint = new Paint();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_hanging_man, container, false);

    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);

    ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
    RelativeLayout relativeLayoutFragment = (RelativeLayout) rootView.findViewById(R.id.relativeLayoutFragment);
    relativeLayoutFragment.addView(new MyView(getActivity(),imageView));

    return rootView;
}

public class MyView extends View {

    int intMove = 1;
    long currentTime = 0, dieTime = 0;

    public MyView (Context context, ImageView imageView) {
        super(context);
        this.postInvalidate();
        String strRandomGuess = MainActivity.strRandomGuess;
        if (strRandomGuess.equals("A"))  
            imageView.setImageResource(R.drawable.a);

The Fragment is as so:

<RelativeLayout android:id="@+id/linearLayoutFragment"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="layout.FragmentMainActivity"
    android:background="@color/background"
    android:orientation="horizontal">

    <ImageView android:id="@+id/imageView"
        android:contentDescription="@string/title_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/imageViewTop"
        android:layout_marginEnd="@dimen/imageViewEnd"/>

The monitor displays:

Process: com.gfaiers.hangman, PID: 24352
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at layout.FragmentMainActivity$MyView.onDraw(FragmentMainActivity.java:79)
at android.view.View.draw(View.java:16178)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16181)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16181)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16181)
at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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)

Is there something I'm completely missing here?!

Geoff
  • 583
  • 2
  • 7
  • 25
  • Yes, you're missing the fact that you can't use findViewById to search for an ID contained in the Fragment, only the view that is loaded by setContentView – OneCricketeer Jun 12 '16 at 19:46
  • @cricket_007 updated question. – Geoff Jun 12 '16 at 20:06
  • You will want the ImageView reference as a field and assign it in onCreateView, though. No reason to consistently try to findViewById inside of onDraw – OneCricketeer Jun 12 '16 at 20:10
  • @cricket_007 updated further. Sorry, I'm quite new to this :( – Geoff Jun 12 '16 at 20:19
  • I understand, Android has a high learning curve. Firstly, `static` variables are not needed here. Second, (not related to issue), try to keep class names consistent. By that, I mean `FragmentMainActivity` is a Fragment, and not an Activity. It confuses others who understand the naming conventions. Anyways, it's not really clear where or how you get that error message. Can you please edit once more with the full error instead of only the message? – OneCricketeer Jun 12 '16 at 20:26
  • @Geoff..... paint.setStrokeCap(Paint.Cap.ROUND); paint.setAntiAlias(true); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); imageView = (ImageView) rootView.findViewById(R.id.imageView); LinearLayout linearLayout = (LinearLayout) rootView.findViewById(R.id.linearLayoutFragment); linearLayout.addView(new MyView(getActivity())); – Muhammad Waleed Jun 12 '16 at 20:28
  • insert code in to onCreateView.. – Muhammad Waleed Jun 12 '16 at 20:28
  • @cricket_007 have updated further. I understand what you're saying about naming conventions etc. I see how it'd be confusing. – Geoff Jun 12 '16 at 20:34
  • @WaqarYounis that code is in onCreateView...? The code for the paint setting etc is working perfectly. The only part to cause errors for me is everything regarding the imageView. – Geoff Jun 12 '16 at 20:35
  • I want to say that the onDraw is happening before the ImageView is found. You need to initialize the ImageView before you call new MyView. Basically, move the ImageView findViewById immediately after the inflater.inflate line – OneCricketeer Jun 12 '16 at 20:42
  • this code insert in onCreateView you are registered imageview incorect... that resaon for crashing.... and try this code... – Muhammad Waleed Jun 12 '16 at 20:42
  • @cricket_007 error still exists. – Geoff Jun 12 '16 at 20:43
  • Okay, new plan, first, remove the static variables. Then, add a ImageView parameter to the MyView constructor and hook that up appropriately. – OneCricketeer Jun 12 '16 at 20:50
  • @cricket_007 updated, unsure if it's as you meant, but still errors :'( – Geoff Jun 12 '16 at 20:54
  • Nope. Don't make the MyView a class inside of the Fragment class. That is giving you more problems than you need. What I meant was keep the findViewById where you had it, but make `public MyView(Context context, ImageView ImageView)` and I hope you under what to do with that – OneCricketeer Jun 12 '16 at 20:57
  • @cricket_007 I think I may give up on this idea! Haha. Updated code. Gives different error now too.java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.MainActivity}: android.view.InflateException: Binary XML file line #91: Binary XML file line #91: Error inflating class fragment – Geoff Jun 12 '16 at 21:02

0 Answers0