void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)'在null对象引用上

时间:2016-02-09 18:11:56

标签: android

我已经开始开发一个应用程序,但是我在DetailActivity.java类中遇到了一个错误,我无法识别请帮助我。谢谢提前

$(document).ready(function() {
  $(".div_textarea").focusin(function() {
    $(this).find('button').css('visibility', 'visible');
  });

  $('.div_textarea button').on('click', function() {
    console.log('works')
    $(this).css('visibility', 'hidden');
  });
});

}    我的xml文件是

public class PrayerDetailActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prayer_detail);
    Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    // Show the Up button in the action bar.
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    // savedInstanceState is non-null when there is fragment state
    // saved from previous configurations of this activity
    // (e.g. when rotating the screen from portrait to landscape).
    // In this case, the fragment will automatically be re-added
    // to its container so we don't need to manually add it.
    // For more information, see the Fragments API guide at:
    //
    // http://developer.android.com/guide/components/fragments.html
    //
    if (savedInstanceState == null) {
        // Create the detail fragment and add it to the activity
        // using a fragment transaction.
        Bundle arguments = new Bundle();
        arguments.putString(PrayerDetailFragment.ARG_ITEM_ID,
                getIntent().getStringExtra(PrayerDetailFragment.ARG_ITEM_ID));
        PrayerDetailFragment fragment = new PrayerDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.prayer_detail_container, fragment)
                .commit();
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        navigateUpTo(new Intent(this, PrayerListActivity.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}

显示的错误是

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/prayer_detail_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

请帮助我

4 个答案:

答案 0 :(得分:3)

在你的xml中没有声明FloatingActionButton

你正试图在onCreate()中使用它。

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

从活动的onCreate()方法中删除它,或以xml布局声明

答案 1 :(得分:0)

可能findViewById返回null。没有id&#34; fab&#34;在xml中。

答案 2 :(得分:0)

问题出在这些代码行上:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    }
});
  

尝试调用虚方法&#39; void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)&#39;在空对象引用上

由于在xml中没有显示任何FloatingActionButton所以如果您onCreate每次Acticity进入onCreate()时都会将其放入R.id.fab它会找到此ID (function () { 'use strict'; angular .module('MyApp') .controller('siteListSiteListController', siteListSiteListController) .controller('siteListSiteShowController', siteListSiteShowController); siteListSiteListController.$inject = ['$scope', 'sites']; function siteListSiteListController($scope, sites) { $scope.sites = sites.query(); $scope.itemsByPage = 5; } siteListSiteShowController.$inject = ['$scope', '$routeParams', '$location', 'site', 'physicalLocationContacts']; function siteListSiteShowController($scope, $routeParams, $location, site, physicalLocationContacts) { $scope.site = site.get({ GlobalLocId: $routeParams.GlobalLocId }); $scope.physicalLocationContacts = physicalLocationContacts.query({ LocationId: 505 }); } })(); 及其 NULL ,因为它不存在。

答案 3 :(得分:0)

主要问题是您的活动无法从xml找到fab引用

如果你真的想要FloatingActionButton,那么在所有视图结束时和完成协调器视图之前添加波纹管代码。

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

否则,您可以从活动中删除FloatingActionButton的代码和对象。