片段捆绑包不起作用(使用Onclick过程)

时间:2016-08-25 19:04:26

标签: android android-fragments

我正在尝试将主要活动的数据发送到片段。但我不能这样。我的代码在这里。

主要活动

 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;

和我的片段代码

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;

/ **  *一个简单的{@link Fragment}子类。  * /

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);

}

}

我的按钮onclick监听器不起作用。谢谢你的帮助。

我改变但是它给出了一个像这里的错误:

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)

2 个答案:

答案 0 :(得分:0)

您的点击监听器可能无法正常工作,因为您要从view返回新的onCreateView()个实例。将其更改为:

return goster;

答案 1 :(得分:0)

@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
}