【问题标题】:Android App development : unfortunately application has stoppedAndroid 应用程序开发:不幸的是应用程序已停止
【发布时间】:2015-04-02 02:25:09
【问题描述】:
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 应用程序已停止,日志 cat 如下所示

它的某些部分可以工作,但是当我单击它时,它会显示错误。我在导航抽屉程序 logcat 中实现了这一点,如下所示:

04-02 02:23:30.880 1454-1454/example.ashok.appnav D/AndroidRuntime: 关闭虚拟机 04-02 02:23:30.880 1454-1454/example.ashok.appnav W/dalvikvm: threadid=1: 线程退出未捕获异常 (group=0xa6194908) 04-02 02:23:30.880 1454-1454/example.ashok.appnav E/AndroidRuntime:致命异常:主要 java.lang.NullPointerException 在 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) 在 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(Native Method)

  1. 请解决我的问题。我用的是安卓工作室

【问题讨论】:

  • 第 31 行是什么

标签: java android android-fragments


【解决方案1】:

你应该放

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.

【讨论】:

  • 把v换成rootView,View v就没用了。
  • 你没有实现onClick()。
  • 不,我实现了 onclick public void onClick(View v) { bleft = (Button) v.findViewById(R.id.bleft);明亮=(按钮)v.findViewById(R.id.bright); bshowanswer = (按钮) 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);开关 (v.getId()){
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多