我是Android Studio中Fragment
和FloatingActionButtons
的新用户。我一直试图用FloatingActionButton
获取setOnClickListener
,但每次运行程序时都会收到一条错误消息,我无法弄清楚如何修复。我知道有很多其他问题在这里遇到同样的问题,虽然我已经通过它们仍然无法弄清楚,我是一个完整的新手,并提前感谢你的所有帮助。
以下是FloatingActionButton
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
xmlns:fab="http://schemas.android.com/apk/res-auto"
tools:context="com.example.user.health.IceOneActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_ice_one" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/editInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="230dp"
android:layout_gravity="bottom|end"
android:layout_marginRight="@dimen/fab_margin"
android:visibility="invisible"
app:backgroundTint="@color/colorFAB3"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
app:fab_title="Edit"
android:src="@drawable/ic_create_white_24dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/saveInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="160dp"
android:layout_gravity="bottom|end"
android:layout_marginRight="@dimen/fab_margin"
android:visibility="invisible"
app:elevation="6dp"
app:backgroundTint="@color/colorFAB2"
app:pressedTranslationZ="12dp"
app:fab_title="Save"
android:src="@drawable/ic_save_white_24dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/showInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_gravity="bottom|end"
android:layout_marginRight="@dimen/fab_margin"
android:visibility="invisible"
app:elevation="6dp"
app:backgroundTint="@color/colorFAB1"
app:pressedTranslationZ="12dp"
fab:fab_title="Done"
android:src="@drawable/ic_check_white_24dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/moreInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:elevation="6dp"
app:backgroundTint="@color/colorAccent"
app:pressedTranslationZ="12dp"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_add_white_24dp" />
这是java活动:
public class IceOneActivity extends Fragment implements View.OnClickListener {
private Boolean isFabOpen = false;
private FloatingActionButton moreInfo,saveInfo,editInfo,showInfo;
private Animation fab_open,fab_close,rotate_forward,rotate_backward;
public IceOneActivity() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View iceOneView = inflater.inflate(R.layout.content_ice_one, container, false);
//myFragmentView
moreInfo = (FloatingActionButton) inflater.inflate(R.layout.content_ice_one, container, false).findViewById(R.id.moreInfo);
saveInfo = (FloatingActionButton) inflater.inflate(R.layout.content_ice_one, container, false).findViewById(R.id.saveInfo);
editInfo = (FloatingActionButton) inflater.inflate(R.layout.content_ice_one, container, false).findViewById(R.id.editInfo);
showInfo = (FloatingActionButton) inflater.inflate(R.layout.content_ice_one, container, false).findViewById(R.id.showInfo);
fab_open = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_open);
fab_close = AnimationUtils.loadAnimation(getActivity(),R.anim.fab_close);
rotate_forward = AnimationUtils.loadAnimation(getActivity(),R.anim.rotate_forward);
rotate_backward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_backward);
moreInfo.setOnClickListener(this);
saveInfo.setOnClickListener(this);
editInfo.setOnClickListener(this);
showInfo.setOnClickListener(this);
return iceOneView;
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.moreInfo:
animateFAB();
break;
case R.id.saveInfo:
Log.d("1", "Fab 1");
break;
case R.id.editInfo:
Log.d("2", "Fab 2");
break;
case R.id.showInfo:
Log.d("3", "Fab 3");
break;
}
}
当我尝试运行活动时,这是应用程序的Logcat:
Process: com.example.user.health, PID: 13237
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.user.health.IceOneActivity.onCreateView(IceOneActivity.java:44)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18893)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5966)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2693)
at android.view.View.measure(View.java:18893)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2192)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1271)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1510)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1162)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6229)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:873)
at android.view.Choreographer.doCallbacks(Choreographer.java:676)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
答案 0 :(得分:1)
您只需要为视图充气一次。这就是这一行(在IceOneActivity中):
View iceOneView = inflater.inflate(R.layout.content_ice_one, container, false);
然后,您可以从这个虚增的视图中获取对FloatingActionButton
个实例的引用:
moreInfo = (FloatingActionButton) iceOneView.findViewById(R.id.moreInfo);
saveInfo = (FloatingActionButton) iceOneView.findViewById(R.id.saveInfo);
editInfo = (FloatingActionButton) iceOneView.findViewById(R.id.editInfo);
showInfo = (FloatingActionButton) iceOneView.findViewById(R.id.showInfo);
答案 1 :(得分:0)
FloatingActionButton 放置在活动布局中,您正在从片段布局中对其进行充气,这就是为什么您会得到空指针异常,您不必这样做,只需从活动中调用它:
moreInfo = (FloatingActionButton)getActivity().findViewById(R.id.moreInfo);
答案 2 :(得分:0)
您不必使用此 -
一次又一次地对片段视图进行充气inflater.inflate(R.layout.content_ice_one, container, false)
你已经这样做了一次 -
View iceOneView = inflater.inflate(R.layout.content_ice_one, container, false);
您应该使用iceOneView
使用 -
moreInfo = (FloatingActionButton) iceOneView.findViewById(R.id.moreInfo);