我正在使用两个 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();
}
}