在片段中使用setText的Nullpointerexception(Android)

时间:2014-03-15 18:34:19

标签: java android nullpointerexception textview

我有一个带有单独片段的应用程序,使用滚动标签。每个片段只显示预设文本。虽然我需要使用html解析器来正确显示文本,但是文本将被格式化以获得良好的外观。我对Java和android都很陌生,所以我认为我的布局可能有问题,但我很难过,我希望有人可以提供帮助!该应用程序在发布时崩溃。该字符串存在于字符串文件中。

这是片段:

public static class AboutMeFragment extends Fragment {

    public AboutMeFragment() {
    }

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         textviewabout = (TextView)getActivity().findViewById(R.id.textviewabout);
         textviewabout.setText(Html.fromHtml(getString(R.string.about_me)));
        View rootView = inflater.inflate(R.layout.aboutme,
                container, false);
        return rootView;
    }
}

而且(再次,非常确定这是不正确的)会创建textview。

public class MainActivity extends FragmentActivity {

 SectionsPagerAdapter mSectionsPagerAdapter;

 ViewPager mViewPager;
 static TextView textviewabout;

这是aboutme.xml

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textviewabout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:scrollbars = "vertical"
        android:text="@string/about_me" />
     </ScrollView>

这是log cat:

03-15 18:38:11.332: E/Trace(30586): error opening trace file: No such file or directory (2)
03-15 18:38:11.722: W/dalvikvm(30586): threadid=1: thread exiting with uncaught exception (group=0x41094540)
03-15 18:38:11.802: E/AndroidRuntime(30586): FATAL EXCEPTION: main
03-15 18:38:11.802: E/AndroidRuntime(30586): java.lang.NullPointerException
03-15 18:38:11.802: E/AndroidRuntime(30586):    at com.example.keiththomsonsicv.MainActivity$AboutMeFragment.onCreateView(MainActivity.java:142)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.View.measure(View.java:15286)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.View.measure(View.java:15286)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.View.measure(View.java:15286)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2230)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.View.measure(View.java:15286)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2105)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1262)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1492)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1160)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4682)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:738)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.Choreographer.doCallbacks(Choreographer.java:564)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.Choreographer.doFrame(Choreographer.java:532)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:723)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.os.Handler.handleCallback(Handler.java:615)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.os.Looper.loop(Looper.java:155)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at android.app.ActivityThread.main(ActivityThread.java:5520)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at java.lang.reflect.Method.invoke(Method.java:511)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
03-15 18:38:11.802: E/AndroidRuntime(30586):    at dalvik.system.NativeStart.main(Native Method)

关于me.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$AboutMeFragment" >
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textviewabout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:scrollbars = "vertical"
        android:text="textviewabout" />
     </ScrollView>

</RelativeLayout>

3 个答案:

答案 0 :(得分:5)

如果你从LogCat提供Stacktrace会更容易找到错误,但我看到一个明显的错误:

在你的片段中,你指的是获取TextView的活动,但是你的textviewabout确定了片段的一部分,所以我确信代码必须是

public static class AboutMeFragment extends Fragment {

     public AboutMeFragment() {
     }

     @Override

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.aboutme,
            container, false);
          textviewabout = (TextView)rootView.findViewById(R.id.textviewabout);
          textviewabout.setText(Html.fromHtml(getString(R.string.about_me)));
          return rootView;
     }
}

编辑: 在您的活动中,您只显示声明,没有代码显示片段的显示方式。您应该添加Activity的XML以显示是否在那里声明Fragment。

答案 1 :(得分:1)

您应初始化观看次数并在setText的{​​{1}}中致电onActivityCreated。因为您的观点可能还没有准备好。

答案 2 :(得分:0)

在我看来,你的textviewabout对象是null(也许你可以通过调试你的应用程序来确认这一点。)

您如何告诉您的Activity使用aboutme.xml作为布局?您是否真的打算将此布局用于活动,还是要将其用于片段ui?

如果你想将它用于片段,你必须使用你在onCreateView方法中获得的inflater来首先膨胀你的aboutme.xml布局,然后检索TextView(你可能希望在Fragment中有TextView) class,在任何普通的szenario中都不需要静态视图类。

此致 托马斯