Hüseyin YILMAZ Hüseyin YILMAZ - 3 months ago 18
Android Question

Fragment Bundle Does Not Work (With Onclick Procedure)

I'm trying to send a data from main activity to fragment. But i could not this. my code is here.

main activty

Bundle bundle = new Bundle();
String mesajim="Bu sana disaridan gelen mesajdir.";
bundle.putString("mesaj",mesajim);
UrunAc fragment2 = new UrunAc();
fragment2.setArguments(bundle);
FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.fragment_container, fragment2);
fragmentTransaction2.commit();
return true;


and my fragment code

package com.technotalkative.navigationviewdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


/**
* A simple {@link Fragment} subclass.
*/

public class UrunAc extends Fragment implements View.OnClickListener {

public View goster;
public String mesaj;
public TextView tw ;


public UrunAc() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

mesaj =this.getArguments().getString("mesaj");

goster = inflater.inflate(R.layout.fragment_urun,container,false);

Button tiklat = (Button)goster.findViewById(R.id.tiklasana);
tiklat.setOnClickListener(this);

return inflater.inflate(R.layout.fragment_urun, container, false);
}


@Override
public void onClick(View v) {

tw.setText(mesaj);

}


}

my button onclick listener does not work. thanks for your help.

i change but it gives an error like here :

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.technotalkative.navigationviewdemo, PID: 3745
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.technotalkative.navigationviewdemo.UrunAc.onClick(UrunAc.java:49)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
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)

Answer
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

    mesaj =this.getArguments().getString("mesaj");

    goster = inflater.inflate(R.layout.fragment_urun,container,false);

    tw = (TextView)goster.findViewById(R.id.tw);// Change your tw id 

    Button tiklat = (Button)goster.findViewById(R.id.tiklasana);
    tiklat.setOnClickListener(this);

    return goster;// here is the problem
}
Comments