-3

I know this is a commonly asked question but I can not find the solution for this plus the fact that I'm a beginner in programming.

I'm trying to make text wrap around image work but I get null point exception error

Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

when passing TextView id to MainActiviy

So I have this Fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="make.appaplication.Fragment4">



<!-- TODO: Update blank fragment layout -->

<ImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:src="@drawable/nature"
    android:id="@+id/imageView"
    android:layout_weight="1"
    android:layout_marginRight="17dp"
    android:layout_marginEnd="17dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/textView3"
    android:textSize="17dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/text_page_1"
    android:layout_below="@+id/textView5"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    />

and passing its values to MainActivity like so

String text = getString(R.string.text_page_1);

    Drawable dIcon = ContextCompat.getDrawable(getApplicationContext(),R.drawable.nature);

    int leftMargin = dIcon.getIntrinsicWidth() + 10;

    SpannableString ss = new SpannableString(text);

    ss.setSpan(new MyLeadingMarginSpan2(3, leftMargin), 0, ss.length(), 0);

    TextView messageView = (TextView) findViewById(R.id.textView3);
    messageView.setText(ss);

And I have these two lines of code

    TextView messageView = (TextView) findViewById(R.id.textView3);
    messageView.setText(ss);

after

 setContentView(R.layout.activity_main);

and in onCreate method

Can anybody help me to solve this problem, please?

anomaR
  • 1
  • 3

1 Answers1

0

If the TextView is inside your Fragment, then you'll need to find the TextView inside your Fragment's View hierarchy. This usually happens inside onCreateView(), which is a lifecycle callback that only exists inside Fragments:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.Fragment, container, false);
    final TextView messageView = (TextView) rootView.findViewById(R.id.textView3);
    messageView.setText("Some Text");
    return rootView;    
}
PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • Thank you very much for help. You are the only one who answered me. Wonder why people downvote these kinds of posts? Anyway by `find` you mean write inside my `Fragment4.java` these lines you added to onCreateView? What does the actual `rootView` there mean? Could you guide me step by step to solve this whole issue? – anomaR Jul 02 '17 at 11:29
  • @anomaR I imagine it was down voted because `NullPointerException` posts are very common, and in almost all cases the solution is easily found by searching SO. However, in this instance the solution is a bit more complex than usual, which is why I answered. The process for inflating the root layout for a `Fragment` is different to the process for an `Activity` - rather than using `setContentView()`, you inflate the layout inside `onCreateView()` and return the inflated `View` at the end of that method. – PPartisan Jul 02 '17 at 11:33
  • Is there a way you can help me solving this issue via mail? I get confused and instead of asking multiple questions here maybe I could sort that out faster using mail. – anomaR Jul 02 '17 at 11:45
  • 1
    Problem solved. You gave me the right answer. Now I know. Thank you PPartisan. – anomaR Jul 10 '17 at 14:40