OnClick无法使用radiobuttons在Fragment中工作

时间:2016-10-16 04:33:36

标签: android android-layout android-fragments fragment

我遇到问题#include <limits.h> #include <stdio.h> #define GROUP_SIZE 5 static int nextBit(void); static int nextGroup(char *dest); static char str[] = "helloo"; int main(void) { char bits[GROUP_SIZE + 1]; int firstTime, nBits; firstTime = 1; while ((nBits = nextGroup(bits)) == GROUP_SIZE) { if (!firstTime) { (void) putchar(' '); } firstTime = 0; (void) printf("%s", bits); } if (nBits > 0) { if (!firstTime) { (void) putchar(' '); } while (nBits++ < GROUP_SIZE) { (void) putchar('0'); } (void) printf("%s", bits); } (void) putchar('\n'); return 0; } static int nextBit(void) { static int bitI = 0, charI = -1; if (--bitI < 0) { bitI = CHAR_BIT - 1; if (str[++charI] == '\0') { return -1; } } return (str[charI] & (1 << bitI)) != 0 ? 1 : 0; } static int nextGroup(char *dest) { int bit, i; for (i = 0; i < GROUP_SIZE; ++i) { bit = nextBit(); if (bit == -1) { break; } dest[i] = '0' + bit; } dest[i] = '\0'; return i; } 有人可以帮忙吗?

onClickListener

Fragment1.java;

Error:
10-16 04:30:50.489 921-921/adil.fragments E/AndroidRuntime: FATAL EXCEPTION: main
                                                            java.lang.NullPointerException                                                                
at adil.fragments.Fragment1$1.onClick(Fragment1.java:56)                                                               
at android.view.View.performClick(View.java:4084)                                                               
at android.widget.CompoundButton.performClick(CompoundButton.java:100)                                                               
at android.view.View$PerformClick.run(View.java:16966)                                                               
at android.os.Handler.handleCallback(Handler.java:615)                                                               
at android.os.Handler.dispatchMessage(Handler.java:92)                                                               
at android.os.Looper.loop(Looper.java:137)                                                               
at android.app.ActivityThread.main(ActivityThread.java:4745)                                                               
at java.lang.reflect.Method.invokeNative(Native Method)                                                               
at java.lang.reflect.Method.invoke(Method.java:511)                                                               
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)                                                               
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)                                                               
at dalvik.system.NativeStart.main(Native Method)

Main_Activity.java;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // inflate ...
    View view = inflater.inflate(R.layout.fragment1,  container, false);
    return view;
}

public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


    final Button radioButton = (Button) getActivity().findViewById(R.id.radioButton);
    final Button button = (Button) getActivity().findViewById(R.id.button);

    radioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            RadioButton radioButton = (RadioButton) getActivity().findViewById(R.id.radioButton);
            if (radioButton.isChecked()) {
                    button.setText("asd");
            }
        }

    });
}

@Override
public void onClick(View v) {

}

3 个答案:

答案 0 :(得分:1)

更改

final Button radioButton = (Button) view.findViewById(R.id.radioButton);
    final Button button = (Button) view.findViewById(R.id.button);

更改

radioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){

            if (radioButton.isChecked()) {
                    button.setText("asd");
            }
        }

将上述代码移至 onCreateView()方法

答案 1 :(得分:1)

我认为您应该将onActivitycreated代码移动到onCreateview,就像在此代码中所做的那样。

 Fragment1.java
 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.RadioButton;
 import android.widget.TextView;


public class Fragment1 extends Fragment {

RadioButton radioButton=null;
TextView textView;
public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup,Bundle bundle){
    View view=inflater.inflate(R.layout.fragment1,viewGroup,false);
    radioButton=(RadioButton)view.findViewById(R.id.radio);
    textView=(TextView)view.findViewById(R.id.text);
    radioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setText("text changed");
        }
    });
    return view;
}
}

MainActivity.java

    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

     public class MainActivity extends AppCompatActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     FragmentTrasacation        fragmentTransaction=getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.frameid,new Fragment1());
    fragmentTransaction.commit();
}
 }

答案 2 :(得分:0)

这里可能存在相关问题:

Inside OnClickListener I cannot access a lot of things - how to approach?

错误最有可能来自:

RadioButton radioButton = (RadioButton) getActivity().findViewById(R.id.radioButton);

因为onClick中的getActivity()可能不会返回您打算返回的视图。获取指向放射性按钮所在变量的视图的指针,或者在onClick()之前将getActivity()的值放在某个变量中,然后在onClick中使用它。