在android片段中填充类片段时出错

时间:2014-11-24 20:17:52

标签: android android-fragments

我正在开发一个Android应用程序。在开发过程中,我面临一个片段中的按钮。我开发了如下代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Utility.log("TestMultiAnswerActivity","Start of onCreateView()");
            .....
        multiQuestionAndAnswersFragmentOkButtonIdLocalVariable =(Button)view.findViewById(R.id.multiQuestionAndAnswersFragmentOkButtonId);
        Utility.log("TestMultiAnswerActivity","777");
        multiQuestionAndAnswersFragmentOkButtonIdLocalVariable.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  readCurrentQuestion();
              }
            });
        Utility.log("TestMultiAnswerActivity","888");
        ....
    }

片段声明为

<fragment
    android:id="@+id/multiQuestionAndAnswersFragment"
    android:name="edu.utd.OOADques.TestMultiAnswerActivity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /> 

当我运行此代码时,我收到错误

11-24 15:09:51.148: I/TestMultiAnswerActivity(22916): Start of onCreateView()
11-24 15:09:51.307: I/TestMultiAnswerActivity(22916): 777
11-24 15:09:51.315: D/AndroidRuntime(22916): Shutting down VM
11-24 15:09:51.315: W/dalvikvm(22916): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-24 15:09:51.405: E/AndroidRuntime(22916): FATAL EXCEPTION: main
11-24 15:09:51.405: E/AndroidRuntime(22916): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.utd.OOADques/edu.utd.OOADques.TestActivity}: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.os.Looper.loop(Looper.java:137)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.ActivityThread.main(ActivityThread.java:5103)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at java.lang.reflect.Method.invokeNative(Native Method)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at java.lang.reflect.Method.invoke(Method.java:525)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at dalvik.system.NativeStart.main(Native Method)
11-24 15:09:51.405: E/AndroidRuntime(22916): Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.Activity.setContentView(Activity.java:1895)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at edu.utd.OOADques.TestActivity.onCreate(TestActivity.java:20)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.Activity.performCreate(Activity.java:5133)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-24 15:09:51.405: E/AndroidRuntime(22916):    ... 11 more
11-24 15:09:51.405: E/AndroidRuntime(22916): Caused by: java.lang.NullPointerException
11-24 15:09:51.405: E/AndroidRuntime(22916):    at edu.utd.OOADques.TestMultiAnswerActivity.onCreateView(TestMultiAnswerActivity.java:63)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.Fragment.performCreateView(Fragment.java:1695)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.app.Activity.onCreateView(Activity.java:4746)
11-24 15:09:51.405: E/AndroidRuntime(22916):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
11-24 15:09:51.405: E/AndroidRuntime(22916):    ... 21 more

我在其他2个片段中使用了类似的代码而没有任何问题只是差异那些片段,这是复选框。在这个片段我使用复选框,在那些片段我使用单选按钮。有人可以指导一下这个问题吗?

我尝试过针对类似错误提出的其他解决方案但没有效果。

我正在扩展片段(android.app.Fragment)。

0 个答案:

没有答案