无法创建多合一共享选项

时间:2018-10-15 12:39:21

标签: java android button share

我创建了一个浮动按钮,并将其命名为fab 当人们单击它时,将立即弹出移动设备上安装的每个共享选项,用户可以选择共享URL的位置。 我通过意图创建了它,但是它无法正常工作,而崩溃了。请帮我。给我一小段代码。

main_activity java

Button buttonDonor;
Button buttonInfo;
Button needBlood;


public static String donorId = "no";
SharedPreferences sharedPreferences;

public static Double lat = 0.0;
public static Double lng = 0.0;

private final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1;

GoogleApiClient mGoogleApiClient;
LocationManager locationManager;
LocationListener locationListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Connecting to the database
    database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("donors");

    /**
     * Wiring up every thing
     */

 //sharebutton

    buttonInfo = (Button) findViewById(R.id.fab);
    buttonInfo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("text/plain");
            i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
            i.putExtra(Intent.EXTRA_TEXT, "http://google.com");
            startActivity(Intent.createChooser(i, "Share URL"));
        }
    });






    buttonInfo = (Button) findViewById(R.id.btn_info);
    Animation slideUpAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.information);
    buttonInfo.startAnimation(slideUpAnimation);
    buttonInfo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            startActivity(new Intent(MainActivity.this, Information.class));


        }
    });
    buttonInfo = (Button) findViewById(R.id.developer);
    Animation slideUpAnimation1 = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.developer);
    buttonInfo.startAnimation(slideUpAnimation1);
    buttonInfo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            startActivity(new Intent(MainActivity.this, About.class));
        }
    });
    buttonInfo = (Button) findViewById(R.id.request);
    Animation slideUpAnimation2 = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.request_blood);
    buttonInfo.startAnimation(slideUpAnimation2);
    buttonInfo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, RequestBlood.class));
        }
    });

    buttonInfo = (Button) findViewById(R.id.check_request);
    Animation slideUpAnimation3 = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.check_request);
    buttonInfo.startAnimation(slideUpAnimation3);
    buttonInfo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, RequestList.class));
        }
    });

    needBlood = (Button) findViewById(R.id.btn_need_blood);
    Animation slideUpAnimation4 = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.need_blood);
    needBlood.startAnimation(slideUpAnimation4);
    needBlood.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, NeedBlood.class));


        }
    });


    buttonDonor = (Button) findViewById(R.id.btn_donor_profile);
    Animation slideUpAnimation6 = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.be_donor);
    buttonDonor.startAnimation(slideUpAnimation6);
    if (donorId.toString().equals("no")) {
        buttonDonor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, DonorForm.class));
            }
        });
    } else {

    }

    /**
     * Initializing variable
     */
    try {
        donorId = sharedPreferences.getString("id", "no");

    } catch (Exception e) {
        e.printStackTrace();
    }

    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            lat = location.getLatitude();
            lng = location.getLongitude();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    if (!locationManager.isProviderEnabled(GPS_PROVIDER)) {
        Toast.makeText(MainActivity.this, "Please Turn on Location", Toast.LENGTH_LONG).show();
        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        MainActivity.this.startActivity(myIntent);
    }


    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        /**
         * Crating a location request
         */


        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);


        return;
    }
    locationManager.requestLocationUpdates(GPS_PROVIDER, 1000, 1, locationListener);


}

protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onConnected(@Nullable Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}






boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click Back twice to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);
}




}

2 个答案:

答案 0 :(得分:0)

buttonInfo = (Button) findViewById(R.id.fab).发布,因为fab是 FloatingActionButton ,并且将其强制转换为Button类型。

替换您的共享代码部分。喜欢,

FloatingActionButton fabBtn = (Button) findViewById(R.id.fab);
fabBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("text/plain");
        i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
        i.putExtra(Intent.EXTRA_TEXT, "http://google.com");
        startActivity(Intent.createChooser(i, "Share URL"));
    }
});

答案 1 :(得分:0)

  

原因:java.lang.ClassCastException:android.support.design.widget.FloatingActionButton无法转换为android.widget.Button       在com.nissan.dnmcbloodbank.MainActivity.onCreate(MainActivity.java:72)

替换此行

Button buttonInfo;

使用

FloatingActionButton buttonInfo;

和这行

buttonInfo = (Button) findViewById(R.id.fab);

使用

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