尝试在ToggleButton上设置OnCheckedStateListener时会导致空指针异常。那个例外是什么?我查看了开发文档并做了相应的工作..
public class AccountViewDetailsFragment extends Fragment {
ToggleButton toggleButton;
LinearLayout testLinearLayout;
public AccountViewDetailsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.account_view_details_fragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
testLinearLayout = (LinearLayout)getView().findViewById(R.id.toggleTestLinearLayout);
toggleButton = (ToggleButton)getView().findViewById(R.id.toggleButtonTest);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
testLinearLayout.setVisibility(View.GONE);
}
if(!isChecked){
testLinearLayout.setVisibility(View.VISIBLE);
}
}
});
}
}
这是XML布局
<FrameLayout 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:background="@color/ivory"
tools:context="com.cube_me.cubeme.Accounts.AccountViewAttachedFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/groupBackground">
<TextView
android:layout_width="350dp"
android:layout_height="@dimen/accountViewDetail_groupTV_height"
android:text="Inquiries"
android:gravity="start|center"
android:paddingLeft="@dimen/accountViewDetail_groupLeftPadding"
android:textSize="@dimen/account_groupTextSize"
android:textColor="@color/mainText"/>
<ToggleButton
android:id="@+id/toggleButtonTest"
style="@style/myToggle"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_alignParentRight="true" />
</LinearLayout>
<LinearLayout
android:id="@+id/toggleTestLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:id="@+id/account_companyShippingAddressTextView"
android:layout_width="@dimen/account_TV_viewWidth"
android:layout_height="match_parent"
android:background="@color/textViewBackGround"
android:gravity="right"
android:paddingRight="@dimen/account_TV_rightPadding"
android:paddingTop="20dp"
android:text="Shipping Address"
android:textColor="@color/secondaryText"
android:textSize="@dimen/account_secondaryTextSize" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/account_shippingAddressStreetEditText"
android:layout_width="match_parent"
android:layout_height="@dimen/account_viewHeight"
android:background="@drawable/custom_edittext"
android:hint="Street"
android:imeOptions="actionNext"
android:inputType="text"
android:paddingLeft="@dimen/editText_leftPadding"
android:textColor="@color/mainText"
android:textSize="@dimen/account_mainTextSize"
android:theme="@style/EditTextTheme" />
<EditText
android:id="@+id/account_shippingAddressBuildingNoEditText"
android:layout_width="match_parent"
android:layout_height="@dimen/account_viewHeight"
android:background="@drawable/custom_edittext"
android:hint="Building Number"
android:imeOptions="actionNext"
android:inputType="text"
android:paddingLeft="@dimen/editText_leftPadding"
android:textColor="@color/mainText"
android:textSize="@dimen/account_mainTextSize"
android:theme="@style/EditTextTheme" />
<EditText
android:id="@+id/account_shippingAddressCityEditText"
android:layout_width="match_parent"
android:layout_height="@dimen/account_viewHeight"
android:background="@drawable/custom_edittext"
android:hint="City"
android:imeOptions="actionNext"
android:inputType="text"
android:paddingLeft="@dimen/editText_leftPadding"
android:textColor="@color/mainText"
android:textSize="@dimen/account_mainTextSize"
android:theme="@style/EditTextTheme" />
<EditText
android:id="@+id/account_shippingAddressStateEditText"
android:layout_width="match_parent"
android:layout_height="@dimen/account_viewHeight"
android:background="@drawable/custom_edittext"
android:hint="State"
android:imeOptions="actionNext"
android:inputType="text"
android:paddingLeft="@dimen/editText_leftPadding"
android:textColor="@color/mainText"
android:textSize="@dimen/account_mainTextSize"
android:theme="@style/EditTextTheme" />
<EditText
android:id="@+id/account_shippingAddressPOBoxEditText"
android:layout_width="match_parent"
android:layout_height="@dimen/account_viewHeight"
android:background="@drawable/custom_edittext"
android:hint="PO Box"
android:imeOptions="actionNext"
android:inputType="text"
android:paddingLeft="@dimen/editText_leftPadding"
android:textColor="@color/mainText"
android:textSize="@dimen/account_mainTextSize"
android:theme="@style/EditTextTheme" />
<EditText
android:id="@+id/account_shippingAddressPinCodeEditText"
android:layout_width="match_parent"
android:layout_height="@dimen/account_viewHeight"
android:background="@drawable/custom_edittext"
android:hint="Pin Code"
android:imeOptions="actionNext"
android:inputType="text"
android:paddingLeft="@dimen/editText_leftPadding"
android:textColor="@color/mainText"
android:textSize="@dimen/account_mainTextSize"
android:theme="@style/EditTextTheme" />
<EditText
android:id="@+id/account_shippingAddressCountryEditText"
android:layout_width="match_parent"
android:layout_height="@dimen/account_viewHeight"
android:background="@drawable/custom_edittext"
android:hint="Country"
android:imeOptions="actionNext"
android:inputType="text"
android:paddingLeft="@dimen/editText_leftPadding"
android:textColor="@color/mainText"
android:textSize="@dimen/account_mainTextSize"
android:theme="@style/EditTextTheme" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
引起例外
FATAL EXCEPTION: main Process: com.cube_me.cubeme, PID: 7869
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ToggleButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.cube_me.cubeme.Accounts.AccountViewDetailsFragment.onActivityCreated(AccountViewDetailsFragment.java:42)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2083)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1129)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1237)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1079)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17547)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.o
答案 0 :(得分:1)
您应该在OnCreateView方法中初始化切换按钮,如下所示:
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.account_view_details_fragment, container, false);
toggleButton = (ToggleButton)view.findViewById(R.id.toggleButtonTest);
return view;
}