Android:如何设置CardView OnLongClick()?

Android:如何设置CardView OnLongClick()?,android,nullpointerexception,android-cardview,onlongclicklistener,Android,Nullpointerexception,Android Cardview,Onlongclicklistener,我有一个CardView的RecyclerView列表。我点击一个CardView,它使用一个意图来启动一个新的活动,向用户显示一个详细的CardView。我想在新的CardView中添加一个长点击,它将启动一个对话框,询问用户是否要删除CardView。我尝试了下面的设置。对话框没有启动,我也看不到祝酒词。我错过了什么 public class CardViewDetails extends AppCompatActivity { int position; CardView

我有一个CardView的RecyclerView列表。我点击一个CardView,它使用一个意图来启动一个新的活动,向用户显示一个详细的CardView。我想在新的CardView中添加一个长点击,它将启动一个对话框,询问用户是否要删除CardView。我尝试了下面的设置。对话框没有启动,我也看不到祝酒词。我错过了什么

public class CardViewDetails extends AppCompatActivity {

    int position;
    CardView cardView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

    CardView cardView = (CardView) findViewById(detsinglecard_view);
    cardView.setOnClickListener((View.OnClickListener) this);
    cardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(CardViewDetails.this,"Dialog Test",Toast.LENGTH_SHORT).show();                
            android.app.FragmentManager fm = getFragmentManager();
            DeleteCardViewFragment delCardViewDialog = new DeleteCardViewFragment();
            delCardViewDialog.show(fm,"delcardview dialog");
            return true;
        }
    }); 
活动详情:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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="#FFFFFF"
tools:context="com.wimso.v060B.CardViewDetails">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" >
</include>

<RelativeLayout
    android:id="@+id/cardViewDetails"
    android:layout_width="match_parent"
    android:layout_height="match_parent"        
    android:background="@color/background4main"
    android:layout_marginTop="6dp"
    android:layout_marginStart="6dp"
    android:layout_marginLeft="6dp"
    android:layout_marginEnd="6dp"
    android:layout_marginRight="6dp"  >

<TextView        
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="2dp"        
    android:textStyle="bold"
    android:textColor="@color/colorFlLabelFinal"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:clickable="false"
    android:layout_centerHorizontal="true"  />

<Spinner
    android:id="@+id/skycard_filter2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    style="@style/Base.Widget.AppCompat.Spinner"  />

**strong text**<android.support.v7.widget.CardView**strong text**

xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
**strong text**android:id="@+id/detsinglecard_view"**strong text**
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="4dp"
android:longClickable="true"  >    
...

**强文本**
...

在通过
findViewById()访问元素之前,必须在
onCreate()中调用
setContentView()


在通过
findViewById()访问元素之前,必须在
onCreate()中调用
setContentView()


看起来像
(CardView)findViewById(detsinglecard\u视图)返回null。检查
detsinglecard\u视图
是否是正确的id。我在上面添加了显示detsinglecard\u视图的布局代码。我不知道为什么应用程序会崩溃。好像你忘了调用
setContentView(R.layout.your_活动)您缺少setContentView。。。你认为findViewById是如何工作的?@cricket\u 007 Android新手在这里,所以我对我的代码片段的工作做了很多假设,然后很快发现我有多少不知道。看起来像
(CardView)findViewById(detsinglecard\u view)返回null。检查
detsinglecard\u视图
是否是正确的id。我在上面添加了显示detsinglecard\u视图的布局代码。我不知道为什么应用程序会崩溃。好像你忘了调用
setContentView(R.layout.your_活动)您缺少setContentView。。。你认为findViewById是如何工作的?@cricket\u 007 Android新手在这里,所以我对我的代码片段的工作做了很多假设,然后很快发现有多少我还不知道。好的,正在取得进展。这样应用程序就不会崩溃,新的CardView也能正常加载。当我长按CardView时,尽管什么也没发生。有什么想法吗?好的,取得进展。这样应用程序就不会崩溃,新的CardView也能正常加载。当我长按CardView时,尽管什么也没发生。有什么想法吗?
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);
    CardView cardView = (CardView) findViewById(detsinglecard_view);
        cardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            Bundle bundle2 = new Bundle();  
            bundle2.putInt("itemPosition",position);
            android.app.FragmentManager fm = getFragmentManager();
            DeleteCardViewFragment delCardViewDialog = new DeleteCardViewFragment();               
            delCardViewDialog.setArguments(bundle2);
            delCardViewDialog.show(fm,"delcardview dialog");
            return true;
        }
    }
}