用的setText在一个片段(机器人)的NullPointerException(Nullpoint

2019-10-29 13:33发布

我用滚动标签之间去单独片段的应用程序。 每个片段只显示预设的文字。 该文本将被格式化为良好的外观,虽然,所以我需要使用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>

这里是日志的猫:

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>

Answer 1:

这将是更容易,如果你的logcat提供堆栈跟踪发现错误,但我看到一个明显的错误:

在您的片段,你指的是活动得到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;
     }
}

编辑:您的活动。此外,你只显示的声明,没有代码要如何展现片段。 你应该添加活动的XML显示,如果这个片段声明那里。



Answer 2:

你应该初始化意见和调用setTextonActivityCreated您的Fragment 。 因为有可能是一种可能性,你的看法还没有准备好。



Answer 3:

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

你如何告诉你的活动使用aboutme.xml作为布局? 你真的打算使用这种布局对于活动或你想使用它的片段UI?

如果你想使用它的片段,你必须使用你的onCreateView方法得到充气先虚增您aboutme.xml布局,然后检索的TextView之后(你可能希望有片段类中TextView的,有应该是在任何正常SZENARIO不需要静态视图类)

问候,托马斯



文章来源: Nullpointerexception with setText in a Fragment (Android)