使用tabhost获取选项卡片段中的editText值

时间:2015-07-30 12:03:10

标签: android tabs android-tabhost

是否可以从tabhost中的tab中获取edittext值?我被指向使用像

这样的语法
invoice_id = mTabHost.getTabWidget().getChildAt(0).findViewById(R.id.editText3).toString();

但是获得空指针异常错误。

日志

07-30 08:29:38.319    1620-1620/com.example.Prototype E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.Prototype, PID: 1620
java.lang.NullPointerException
        at com.example.Prototype.Invoice3$1.onTabChanged(Invoice3.java:61)
        at android.support.v4.app.FragmentTabHost.onTabChanged(FragmentTabHost.java:317)
        at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:446)
        at android.widget.TabHost.setCurrentTab(TabHost.java:431)
        at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
        at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

TabHost:

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;


public class Invoice3 extends FragmentActivity  {
private FragmentTabHost mTabHost;
String check = null;
ShowAlert alert = new ShowAlert();
String invoice_id;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main2);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Client", null),
            clientFragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Products", null),
            productsFragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Confirm", null),
            confirmFragmentTab.class, null);

    mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {

            check = GlobalApp.data().value;

            if (tabId.equals("tab2")) {
                if(check == "Select Client")
                {

                    mTabHost.setCurrentTab(0);

                    alert.showAlertDialog(Invoice3.this,
                            "Error",
                            "Client name not selected", true);


                }
                else
                {


                    invoice_id = mTabHost.getTabWidget().getChildAt(0).findViewById(R.id.editText3).toString();

                    GlobalApp.data().id = invoice_id;

                    mTabHost.setCurrentTab(1);
                }
            }
            if (tabId.equals("tab3")) {
                alert.showAlertDialog(Invoice3.this,
                        "Error",
                        check, true);
            }
        }
    });
}


}

0 个答案:

没有答案