我是Android Studio中的Fragment
和FloatingActionButtons
的新手。我一直試圖獲得FloatingActionButton
setOnClickListener
,但每次運行該程序時,都會收到一條錯誤消息,我無法弄清楚如何解決。我知道在這裏也有很多其他的問題,雖然我已經通過了他們,但仍然無法解決,我是一個完全的新手,並且提前感謝您的幫助。setOnClickListener FloatingActionButton on Fragment Error
下面是FloatingActionButton
的XML代碼:
<?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)
謝謝大家的幫助,我已經做了你說的話雖然IM仍然收到此錯誤:'顯示java.lang.NullPointerException:試圖調用虛擬方法'無效android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)'在空對象引用上' –
@ geek101上面發佈的xml文件的名稱是什麼?它是不是'content_ice_one'? –
上面發佈的xml文件是'app_bar_ice_one.xml',但是'content_ice_one'是內容所在的位置。 –