NullPointerException setText Fragment

时间:2013-10-30 19:53:06

标签: android android-fragments android-tabs

我想创建一个带有2个选项卡(选项卡A和选项卡B)的Android应用程序,然后更新选项卡A的内容(TextView),但在这种情况下会引发NullPointerException。这是我的代码:

MainActivity.java

public class MainActivity extends ActionBarActivity {

    TabA fragmentTabA = new TabA();
    TabB fragmentTabB = new TabB();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addTabs();
    }

    public void addTabs() {
        ActionBar actionBar = getSupportActionBar();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1 = actionBar.newTab();
        tab1.setText("Tab A");
        tab1.setTabListener((ActionBar.TabListener) new TabsListener(fragmentTabA));

        ActionBar.Tab tab2 = actionBar.newTab();
        tab2.setText("Tab B");
        tab2.setTabListener((ActionBar.TabListener) new TabsListener(fragmentTabB));

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);

        // I want to update the TextView of the tab A but NPE raises here
        fragmentTabA.setTextView("Hello World !");
    }
}

TabA.java

public class TabA extends Fragment {

    private TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_a, container, false);
        this.textView = (TextView) view.findViewById(R.id.textview_tab_a);
        return view;
    }

    public void setTextView (String txt) {
        this.textView.setText(txt);
    }
}

TabsListener.java

public class TabsListener implements ActionBar.TabListener {

    private Fragment fragment;

    public TabsListener (Fragment fragment) {
        this.fragment = fragment;
    }

    @Override
    public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
        arg1.replace(R.id.tabs_container, this.fragment);
    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        arg1.remove(this.fragment);
    }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {

    }
}

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tabs_container" />

tab_a.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" >

    <TextView
        android:id="@+id/textview_tab_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

logcat的

10-30 19:37:36.563: E/AndroidRuntime(4287): java.lang.RuntimeException: Unable to start 
activity ComponentInfo{com.example.actionbartest/com.example.actionbartest.MainActivity}: java.lang.NullPointerException
10-30 19:37:36.563: E/AndroidRuntime(4287):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at android.os.Looper.loop(Looper.java:137)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at java.lang.reflect.Method.invokeNative(Native Method)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at java.lang.reflect.Method.invoke(Method.java:525)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at dalvik.system.NativeStart.main(Native Method)
10-30 19:37:36.563: E/AndroidRuntime(4287): Caused by: java.lang.NullPointerException
10-30 19:37:36.563: E/AndroidRuntime(4287):     at com.example.actionbartest.TabA.setTextView(TabA.java:22)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at com.example.actionbartest.MainActivity.addTabs(MainActivity.java:35)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at com.example.actionbartest.MainActivity.onCreate(MainActivity.java:15)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at android.app.Activity.performCreate(Activity.java:5133)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-30 19:37:36.563: E/AndroidRuntime(4287):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-30 19:37:36.563: E/AndroidRuntime(4287):     ... 11 more

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在调用标签setTextView之前,您似乎正在TabA上调用onCreateView,因此您的setText失败,因为该标签尚未对其视图进行充气。

确保仅在视图膨胀后设置文本。 (并且由于您似乎没有使用任何类型的动态文本,您可以在onCreateView调用中自行设置文本)