Android App开发:遗憾的是应用程序停止了

时间:2015-04-02 02:25:09

标签: java android android-fragments

public class Jav extends Fragment implements View.OnClickListener {
    public static Jav newInstance(){
        Jav fragment = new Jav();
        return fragment;
    }
    Button bleft,bshowanswer,bright;
    TextView tv_xx,tv_yy,tvquestion,tvanswers;
    String[] greque;
    String[] greans;
    int index;
    public Jav() {
    }
    public void onActivityCreated(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        super.onActivityCreated(savedInstanceState);
        index = 0;
        greque = getResources().getStringArray(R.array.greques);
        greans = getResources().getStringArray(R.array.greanse);
        bleft.setOnClickListener(this);
        bright.setOnClickListener(this);
        bshowanswer.setOnClickListener(this);
        tvquestion.setText(greque[index]);
        tvanswers.setText("press a button for ans");
        tv_xx.setText(String.valueOf(index+1));
        tv_yy.setText(String.valueOf(greque.length));

    }
    View v;
    public void onClick(View v) {
        bleft = (Button) v.findViewById(R.id.bleft);
        bright = (Button) v.findViewById(R.id.bright);
        bshowanswer = (Button) v.findViewById(R.id.bshowanswer);
        tvquestion = (TextView) v.findViewById(R.id.tvquestion);
        tvanswers =(TextView) v.findViewById(R.id.tvanswers);
        tv_xx = (TextView) v.findViewById(R.id.tv_xx);
        tv_yy = (TextView) v.findViewById(R.id.tv_yy);

        switch (v.getId()){
        case R.id.bleft:
            break;
        case R.id.bshowanswer:
            break;
        case R.id.bright:
            break;
        }
    }

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

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(2);
    }
}
  1. 当我运行我的程序时,我的Android应用程序已停止,并且日志猫显示在下面
  2. 它的一部分工作,但是当我点击它时显示错误。我在导航抽屉程序logcat中实现了这个,如下所示:

      

    04-02 02:23:30.880 1454-1454 / example.ashok.appnav D / AndroidRuntime:关闭VM   04-02 02:23:30.880 1454-1454 / example.ashok.appnav W / dalvikvm:threadid = 1:线程退出时未捕获异常(组= 0xa6194908)   04-02 02:23:30.880 1454-1454 / example.ashok.appnav E / AndroidRuntime:FATAL EXCEPTION:main       显示java.lang.NullPointerException               at example.ashok.appnav.Jav.onActivityCreated(Jav.java:31)               在android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1794)               在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:977)               在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)               在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)               在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)               在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:456)               在android.os.Handler.handleCallback(Handler.java:725)               在android.os.Handler.dispatchMessage(Handler.java:92)               在android.os.Looper.loop(Looper.java:137)               在android.app.ActivityThread.main(ActivityThread.java:5041)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:511)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)               在dalvik.system.NativeStart.main(本地方法)

    1. 请解决我的问题。我使用android studio

1 个答案:

答案 0 :(得分:0)

你应该把

bleft = (Button) rootView.findViewById(R.id.bleft);
bright = (Button) rootView.findViewById(R.id.bright);
bshowanswer = (Button) rootView.findViewById(R.id.bshowanswer);
tvquestion = (TextView) rootView.findViewById(R.id.tvquestion);
tvanswers =(TextView) rootView.findViewById(R.id.tvanswers);
tv_xx = (TextView) rootView.findViewById(R.id.tv_xx);
tv_yy = (TextView) rootView.findViewById(R.id.tv_yy);
onCreateView中的