接口NullPointerException - 将按钮上的数据发送到MainActivity按钮在片段内单击

时间:2014-05-31 00:44:52

标签: android android-fragments interface nullpointerexception onclicklistener

正在处理片段内点击按钮时向我的MainActivity发送一些字符串数据。它似乎不是一个困难的程序,但我在某个地方被捆绑并收到NullPointerException。我假设它与我使用界面的方式有关。 Android新手,建议和评论将不胜感激。谢谢!

接口代码:

public interface BtnInterface {
    public void btnClicked(String x, String y, String z, String w);
}

主要活动单位:

public class MainActivity extends Activity implements BtnInterface{

    Fragment_X frag;
    FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frag = new Fragment_X();
        frag.setInterface(this);
        ft = getFragmentManager().beginTransaction();
        ft.add(R.id._frag_container, frag);
        ft.commit();
    }

    @Override
    public void btnClicked(String x, String y,String z, String w) {
        Log.d("data-check", x); 
    }

FRAGMENT SNIPPET:

public class Fragment_X extends Fragment {

    View v;
    Button btn;
    BtnInterface buttonInterface;
    String x, y, z, w;
    String  choices1 = {"random", "values"}
    String  choices2 = {"random", "values"}
    String  choices3 = {"random", "values"}
    String  choices4 = {"random", "values"}

    public void setInterface(BtnInterface buttonInterface) {
        this.buttonInterface = buttonInterface;
    }

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

        v = inflater.inflate(R.layout.frag_layout, container,
            false);

        spin1 = (Spinner) v.findViewById(R.id.spin1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            R.layout.spinner_layout, choices1);
        adapter.setDropDownViewResource(R.layout.spinner_dropdown);
        spinSomewhere.setAdapter(adapter);

        spin2 = (Spinner) v.findViewById(R.id.spin2);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getActivity(),
            R.layout.spinner_layout, choices2);
        adapter1.setDropDownViewResource(R.layout.spinner_dropdown);
        spinFor.setAdapter(adapter1);

        spin3 = (Spinner) v.findViewById(R.id.spin3);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity(),
            R.layout.spinner_layout, choices4);
        adapter2.setDropDownViewResource(R.layout.spinner_dropdown);
        spinXp.setAdapter(adapter2);

        spin4 = (Spinner) v.findViewById(R.id.spin4);
        ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(getActivity(),
            R.layout.spinner_layout, choices4);
        adapter3.setDropDownViewResource(R.layout.spinner_dropdown);
        spinWith.setAdapter(adapter3);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                x = spin1.getSelectedItem().toString();
                y = spin2.getSelectedItem().toString();
                z = spin3.getSelectedItem().toString();
                w = spin4.getSelectedItem().toString(); 

                buttonInterface.btnClicked(x,y,z,w);
            }
        });

        return v;
    }
}  

错误日志:

05-30 21:27:35.230: E/AndroidRuntime(3016): FATAL EXCEPTION: main
05-30 21:27:35.230: E/AndroidRuntime(3016): Process: com.ex.app, PID: 3016
05-30 21:27:35.230: E/AndroidRuntime(3016): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.ex.app/com.ex.app.MainActivity}:    java.lang.NullPointerException
05-30 21:27:35.230: E/AndroidRuntime(3016):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.os.Looper.loop(Looper.java:136)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at java.lang.reflect.Method.invokeNative(Native Method)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at java.lang.reflect.Method.invoke(Method.java:515)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at dalvik.system.NativeStart.main(Native Method)
05-30 21:27:35.230: E/AndroidRuntime(3016): Caused by: java.lang.NullPointerException
05-30 21:27:35.230: E/AndroidRuntime(3016):     at com.ex.app.Fragment_X.onCreateView(Fragment_X.java:75)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.app.Fragment.performCreateView(Fragment.java:1700)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.app.Activity.performStart(Activity.java:5240)
05-30 21:27:35.230: E/AndroidRuntime(3016):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
05-30 21:27:35.230: E/AndroidRuntime(3016):     ... 11 more

2 个答案:

答案 0 :(得分:1)

它为null,因为您在实例化后忘记将BtnInterface设置为Fragment_X

<强>溶液

frag = new Fragment_X();
frag.setInterface(this);
    ft = getFragmentManager().beginTransaction();
    ft.add(R.id._frag_container, frag);
    ft.commit();

答案 1 :(得分:0)

简单的回答。忘了最简单的一行:

btn = (Button) v.findViewById(R.id.btn);

正如我所说的......对Android来说很新。