0

我正在使用两个 java 文件来创建我的选项卡布局 SlidingTabLayout.java 和 SlidingTabStrip.java,并且我有选项卡的片段。我在 Tab1 中有一个按钮可以切换到 Tab2,但它不起作用。我正在使用下面的代码为 tab1 中的按钮执行此操作

    TabHost host = (TabHost) getActivity().findViewById(
                    android.R.id.tabhost);
    host.setCurrentTab(2);

上面的代码抛出空指针请帮忙

这是我的 tablayout 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

<include
    android:id="@+id/tool_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/tool_bar" />

<com.datavsn.adapter.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:elevation="2dp" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >
</android.support.v4.view.ViewPager>

</LinearLayout>

这就是错误的样子

08-31 14:10:17.948: E/AndroidRuntime(5961): FATAL EXCEPTION: main
08-31 14:10:17.948: E/AndroidRuntime(5961): Process: com.datavsn.microfinance, PID: 5961
08-31 14:10:17.948: E/AndroidRuntime(5961): java.lang.NullPointerException
08-31 14:10:17.948: E/AndroidRuntime(5961):     at com.datavsn.microfinance.LoanDisbursement.inittabHost(LoanDisbursement.java:102)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at com.datavsn.microfinance.LoanDisbursement.onActivityCreated(LoanDisbursement.java:57)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1797)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:979)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.View.measure(View.java:17478)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.View.measure(View.java:17478)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.View.measure(View.java:17478)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.View.measure(View.java:17478)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.View.measure(View.java:17478)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.View.measure(View.java:17478)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.View.measure(View.java:17478)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2285)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1396)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6624)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:812)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.Choreographer.doCallbacks(Choreographer.java:612)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.Choreographer.doFrame(Choreographer.java:582)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:798)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.os.Handler.handleCallback(Handler.java:733)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.os.Looper.loop(Looper.java:146)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at android.app.ActivityThread.main(ActivityThread.java:5602)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at java.lang.reflect.Method.invokeNative(Native Method)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at java.lang.reflect.Method.invoke(Method.java:515)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
08-31 14:10:17.948: E/AndroidRuntime(5961):     at dalvik.system.NativeStart.main(Native Method)

我的第一个选项卡片段的 java 代码

 import java.util.ArrayList;
 import java.util.List;

 import com.datavsn.adapter.TabsPagerAdapter;
 import com.datavsn.database.DBHelper;
 import com.datavsn.database.DatabaseHelper;

 import android.R.string;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.TabActivity;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentTransaction;
 import android.support.v4.view.ViewPager;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.webkit.WebView.FindListener;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Spinner;
 import android.widget.AdapterView.OnItemSelectedListener;
 import android.widget.TabHost;
 /**
  * @since ver 1.0 modified by akshay as on 26 Aug 2015
  * <li>changes made to make activity into fragment</li>
  */
 public class LoanDisbursement extends Fragment {
    DBHelper dbhelper;
    EditText mode, batchNo, meetinNo,institutionNo;
    Spinner spinnerAgainstMeeting, spinnerTrans,institutionType;
    Button btnChoose,btnSave;
    ViewPager pager;
    TabsPagerAdapter move;
    ArrayList<String> institutionName = new ArrayList<String>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_loan,
                container, false);

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
 //     Context context = getActivity().getApplicationContext();
        initViews();
        institutionType.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                institutionName = dbhelper.getInstitutionNameByGroupId(dbhelper
                        .getGroupID(institutionType.getSelectedItem()
                                .toString()));
                if (institutionName.size() > 0) {
                    institutionNo.setText(institutionName.get(0));
                } else {
                    institutionNo.setText("");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });
        btnChoose.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                loadSpinnerInsNo();
            }
        });
        btnSave.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                TabHost host = (TabHost) getActivity().findViewById(
                        android.R.id.tabhost);
                host.setCurrentTab(2);
            }
        });

    }

    private void initViews() {
        // TODO Auto-generated method stub
        dbhelper = new DBHelper(getActivity());
        mode = (EditText) getView().findViewById(R.id.edtMode);
        batchNo = (EditText) getView().findViewById(R.id.edtBatch);
        institutionType = (Spinner) getView().findViewById(
                R.id.spnInstitutinType);
        institutionNo = (EditText) getView()
                .findViewById(R.id.edtInstitutionNo);
        meetinNo = (EditText) getView().findViewById(R.id.edtMeetingNo);
        spinnerAgainstMeeting = (Spinner) getView().findViewById(
                R.id.spinnerAgainestMeet);
        spinnerTrans = (Spinner) getView().findViewById(R.id.spinnerTrans);
        btnChoose = (Button) getView().findViewById(R.id.btnChoose);
        btnSave = (Button) getView().findViewById(R.id.btnSave);
        loadSpinnerInsType();
        loadSpinnerAgainstMeet();
        mode.setText("Cash");
        mode.setEnabled(false);
        batchNo.requestFocus();
        move = new TabsPagerAdapter(getFragmentManager());
    }

    /*
     * @since version 1.0.0 added by aniket w.r.t.#12092 as on 25 Aug 2015 This
     * method is used to yes or no into Spinner
     */
    private void loadSpinnerAgainstMeet() {
        // TODO Auto-generated method stub
        ArrayList<String> againstMeet = new ArrayList<String>();
        againstMeet.add("Yes");
        againstMeet.add("No");
        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_spinner_item,
                againstMeet);
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinnerAgainstMeeting.setAdapter(dataAdapter);

    }
    private void loadSpinnerInsType() {
        // TODO Auto-generated method stub

        ArrayList<String> labels = dbhelper.getGroupNames();

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_spinner_item, labels);

        // Drop down layout style - list view with radio button
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        institutionType.setAdapter(dataAdapter);

    }
    private void loadSpinnerInsNo() {
        // TODO Auto-generated method stub
        List<String> labels = dbhelper.getInstitutionNameByGroupId(dbhelper
                .getGroupID(institutionType.getSelectedItem().toString()));
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, labels);

        alert.setAdapter(dataAdapter, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                String strName = dataAdapter.getItem(which);
                AlertDialog.Builder builderInner = new AlertDialog.Builder(
                        getActivity());
                builderInner.setMessage(strName);
                institutionNo.setText(strName);
            }
        });
        alert.show();

    }


 }
4

3 回答 3

1

我创建了一个活动,并在上面设置了寻呼机适配器。用来调用不同的fragment,所有的fragment基本上都是不同的tab。所以基本上使用 TabHost 是不可能的,因为我从未在我的布局中使用过标签主机(有问题提供的布局)。

所以尝试使用下面的代码

adapter = new TabsPagerAdapter(getFragmentManager());
pager = (ViewPager) getView().findViewById(R.id.pager); 
pager.setAdapter(adapter);
pager.setCurrentItem(2); 

但即使这是不可能的,因为我的 pageradapter 已经在我的活动中设置,我正试图在片段中重新初始化。

最后解决的是,我将寻呼机适配器设置为静态并像这样访问它

TabActivity.pager.setCurrentItem(1);
于 2015-08-31T11:21:01.433 回答
1

你有两个标签,所以基本上设置

host.setCurrentTab(1);

实际上它从0开始,

TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
host.setCurrentTab(1);

有关更多详细信息,您可以访问:
https://developer.android.com/reference/android/widget/TabHost.html#setCurrentTab(int)

编辑后的答案

改用getView( ) getActivity()。我希望现在它会起作用。

TabHost host = (TabHost) getView().findViewById(android.R.id.tabhost);
于 2015-08-31T07:49:44.300 回答
0

第一次需要设置 TabHost

    TabHost tabs = (TabHost) findViewById(android.R.id.tabhost);

    tabs.setup();

    TabHost.TabSpec spec = tabs.newTabSpec("tag1");

    spec.setContent(R.id.tab1);
    spec.setIndicator("abc1");
    tabs.addTab(spec);

    spec = tabs.newTabSpec("tag2");
    spec.setContent(R.id.tab2);
    spec.setIndicator("abc2");
    tabs.addTab(spec);

    spec = tabs.newTabSpec("tag3");
    spec.setContent(R.id.tab3);
    spec.setIndicator("abc3");
    tabs.addTab(spec);

    tabs.setCurrentTab(0);
于 2015-08-31T07:42:53.737 回答