按钮不会触发onClick方法

时间:2017-01-31 15:30:30

标签: android onclicklistener android-button android-4.4-kitkat

处理Android Version - KITKATButton - newRide未触发onClick()方法时问题仍然存在,相同的代码在其他Android Version > KITKAT上运行良好。

我的班级的Java代码片段:

 LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View contentView = inflater.inflate(R.layout.activity_ongoing_ride_details, null, false);
    View cView = getLayoutInflater().inflate(R.layout.title_bar, null);

    imgSignOut = (ImageView) cView.findViewById(R.id.imgSignout);
    imgRefresh = (ImageView) cView.findViewById(R.id.imgRefresh);
    tvTitle = (TextView) cView.findViewById(R.id.tvTitle);
    newRide = (Button)contentView.findViewById(R.id.btnNewRide);

    if (Utilities.getStringFromPrefs(getApplicationContext(), "userType")
        .equals("driver")) {
        setContentView(contentView);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(cView);

        actionBar.setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#ffffff")));
        tvTitle.setText("ON GOING RIDES");
        tvTitle.setTextColor(Color.BLACK);
    } 
    else {
        frameLayout.addView(contentView);

        getActionBar().setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM
            | android.support.v7.app.ActionBar.DISPLAY_SHOW_HOME | android.support.v7.app.ActionBar.DISPLAY_HOME_AS_UP);
        getActionBar().setCustomView(R.layout.custom_title);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
        getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#ffffff")));
        TextView headTxt = (TextView) findViewById(R.id.mytext);
        headTxt.setTextColor(Color.BLACK);
        headTxt.setText("ON GOING RIDES");

    }

    lvRideList = (ListView) findViewById(R.id.lvRideDetails);
    llNoBookings = (LinearLayout) findViewById(R.id.llNoBookings);

    HsApi = new APIImple();
    onGoingRideResult = new ArrayList<OnGoingRideDetails>();
    mTask = new GetOnGoingRideDetails().execute();

    if (!(Utilities.getStringFromPrefs(getApplicationContext(), "userType")
        .equals("driver"))) {

        newRide.setVisibility(View.VISIBLE);
        newRide.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (!Utilities.isConnected(getApplicationContext())) {
                    Utilities.notConnectedToast(getApplicationContext());

                } else {
                    Intent in = new Intent(OnGoingRideDetailsList.this,
                        FragmentActivity.class);
                    startActivity(in);
                }
            }


        });
    } 
    else {
        newRide = (Button) findViewById(R.id.btnNewRide);
        newRide.setVisibility(View.GONE);
    }

 lvRideList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // TODO Auto-generated method stub

            if (onGoingRideResult.get(position).getUserType()
                .equals("driver")) {
                Intent intentToDriverScreen = new Intent(
                    OnGoingRideDetailsList.this,
                    DriverWelcomeScreen.class);
                intentToDriverScreen.putExtra("rideData",
                    onGoingRideResult.get(position));
                startActivity(intentToDriverScreen);
            } 
            else {
                Intent intentToUserRideScreen = new Intent(
                    OnGoingRideDetailsList.this,
                    UserCurrentBookings.class);
                intentToUserRideScreen.putExtra("rideData",
                    onGoingRideResult.get(position));
                startActivity(intentToUserRideScreen);
            }
        }
    });

}

XML activity_ongoing_ride_details.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/bgcolor"
    android:descendantFocusability="blocksDescendants">


<Button
    android:id="@+id/btnNewRide"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dp"
    android:background="@drawable/mybutton_bg"
    android:clickable="true"
    android:text="Click here for New Ride"
    android:textColor="@color/buttontextcolor"
    android:textSize="15dp"
    android:visibility="visible"/>


<ListView
    android:id="@+id/lvRideDetails"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#c8c8c8"
    android:dividerHeight="1dp"
    android:focusable="false"/>

<LinearLayout
    android:id="@+id/llNoBookings"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical"
    android:visibility="gone">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:padding="10dp"
        android:text="Sorry! No Active Bookings Available for you"
        android:textColor="#ff9966"
        android:textStyle="bold"/>

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:src="@drawable/sadsmiley"/>
</LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

 if (!(Utilities.getStringFromPrefs(getApplicationContext(), "userType")
    .equals("driver")))

我认为问题出在本声明中。这种情况不正确