textView中的java null指针错误

时间:2012-06-20 08:03:33

标签: android tabs textview

我有这段代码:

public class InfoActions extends ActivityGroup{

TabHost tabHost;
TabSpec first;
TabSpec second;


View indicatorview1;
View indicatorview2;


@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // ToDo add your GUI initialization code here
    setContentView(R.layout.infoactions);

    tabHost=(TabHost)findViewById(R.id.tabhostsynchronisation);
    tabHost.setup(getLocalActivityManager());
    tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

    indicatorview1 = android.view.LayoutInflater.from(this).inflate(R.layout.tabs_bg_styled, null);


    first=tabHost.newTabSpec("Tab 1");
    ((TextView)indicatorview1.findViewById(R.id.tabsText)).setText("");
    first.setIndicator(indicatorview1);
    Intent in1 = new Intent(this, com.Orange.PrepareVisits.RoadmapsOnDevice.class);
    first.setContent(in1);

    indicatorview2 = android.view.LayoutInflater.from(this).inflate(R.layout.tabs_bg_styled, null);

    second=tabHost.newTabSpec("Tab 2");
    ((TextView)indicatorview2.findViewById(R.id.tabsText)).setText("Informations");
    second.setIndicator(indicatorview2);
    Intent in2 = new Intent(this, com.Orange.Information.Informations.class);
    second.setContent(in2);

    tabHost.addTab(first);
    tabHost.addTab(second);

}

这是我的tabs_bg_styled.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/tab_bg_selector"
    android:padding="10dp"
    android:gravity="center"
    android:orientation="vertical"
>
    <TextView
        android:id="@+id/tabsText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textSize="10dp"
        android:textStyle="bold"
        android:textColor="@drawable/tab_text_selector"
        android:gravity="center"
    />
</LinearLayout>

这是日志:

ERROR   AndroidRuntime      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
ERROR   AndroidRuntime      at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR   AndroidRuntime      at android.os.Looper.loop(Looper.java:130)
ERROR   AndroidRuntime      at android.app.ActivityThread.main(ActivityThread.java:3701)
ERROR   AndroidRuntime      at java.lang.reflect.Method.invokeNative(Native Method)
ERROR   AndroidRuntime      at java.lang.reflect.Method.invoke(Method.java:507)
ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
ERROR   AndroidRuntime      at dalvik.system.NativeStart.main(Native Method)
ERROR   AndroidRuntime  Caused by: java.lang.NullPointerException
ERROR   AndroidRuntime      at com.Orange.Information.InfoActions.onCreate(InfoActions.java:45)
ERROR   AndroidRuntime      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)

这是我收到错误的行:

 ((TextView)indicatorview1.findViewById(R.id.tabsText)).setText("");

有人有任何想法吗?

1 个答案:

答案 0 :(得分:4)

也许尽量不要在一条线上完成所有工作(至少在它工作之前)。它会更容易调试,并尝试使用这样的充气机(我总是如何使用它):

LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.tabs_bg_styled, null);
TextView tv = (TextView)vi.findViewById(R.id.tabsText);