简体   繁体   English

Android:按钮不起作用,但代码未显示任何错误

[英]Android: Button doesn't work, but code doesn't show any error

I'm trying to create a form to capture data, I need to access the camera to take pictures.我正在尝试创建一个表单来捕获数据,我需要访问相机才能拍照。 My problem is when I try to call the camera with imagenButton , it's not working but the code doesn't show any error.我的问题是当我尝试使用imagenButton调用相机时,它不起作用但代码没有显示任何错误。

Here's my fragment to declare form:这是我声明表单的fragment

public class EspecimenesInsertarFragment extends Fragment implements 
Response.Listener<JSONObject>, Response.ErrorListener {
private EspecimenesViewModel especimenesViewModel;
RequestQueue rq;
JsonRequest jrq;

final int COD_SELECCIONA=10;
final int COD_FOTO=20;

private static final int REQUEST_IMAGE_CAMERA=101;
private static final int REQUEST_PERMISSION_CAMERA=101;

Button btnGuardar, btnCancelar;
ImageButton btnCamara, btnGaleria;
ImageView imageView;
public static final int MY_DEFAULT_TIMEOUT = 50000;


public EspecimenesInsertarFragment() {
    // Required empty public constructor
}

public static EspecimenesInsertarFragment newInstance() { return new EspecimenesInsertarFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment

    View view;
    view = inflater.inflate(R.layout.fragment_colecciones_insertar, container, false);
    imageView = (ImageView)view.findViewById(R.id.imageView);
    btnCamara=(ImageButton) view.findViewById(R.id.tomarFoto);
    btnGuardar = (Button) view.findViewById(R.id.buttonIngresarEspecimenes);
    rq = Volley.newRequestQueue(getContext());


  /*  if(validaPermisos()){
        btnCamara.setEnabled(true);
    }else{
        btnCamara.setEnabled(false);
    } */

    if (ContextCompat.checkSelfPermission(getContext(), WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(getActivity(), new String[]{WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA}, 1000);
    }


    btnGuardar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            registrar_especimen("http://localhost/BIO-UES-APP/EspecimenesController.php");
        }
    });
static final int REQUEST_TAKE_PHOTO=1;

public void tomarFoto(View view){
    Intent takePictureInent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Log.d("entra","");
    if(takePictureInent.resolveActivity(getActivity().getPackageManager())!= null){
        File photoFile=null;
        try {
            photoFile=createImageFile();
        }catch (IOException ex){

    }
        if(photoFile!=null){
        Uri photoUri=FileProvider.getUriForFile(getActivity(),"com.example.luvin.drawercero",photoFile);
        takePictureInent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
        getActivity().startActivityForResult(takePictureInent,REQUEST_TAKE_PHOTO);
        }
    }
}

And this is my XML layout:这是我的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Especimenes.EspecimenesConsultarFragment">

<!-- TODO: Update blank fragment layout -->
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_centerVertical="true"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="0dp"
    android:layout_marginEnd="-7dp"
    android:layout_marginRight="-7dp"
    android:layout_marginBottom="9dp"
    tools:layout_editor_absoluteX="393dp"
    tools:layout_editor_absoluteY="79dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <ImageButton
                android:id="@+id/tomarFoto"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_marginStart="100dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="24dp"
                android:layout_marginRight="24dp"
                android:clickable="true"
                android:src="@android:drawable/ic_menu_camera" />

            <ImageButton
                android:id="@+id/seleccionarDesdeGaleria"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_marginStart="200dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="24dp"
                android:layout_marginRight="24dp"
                android:src="@android:drawable/ic_menu_gallery" />


        </FrameLayout>
        
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="88dp"
            android:orientation="vertical">

            <Button
                android:id="@+id/buttonCancelarEspecimenes"
                android:layout_width="129dp"
                android:layout_height="41dp"
                android:layout_toRightOf="@+id/buttonIngresar"
                android:backgroundTint="#96A6A8"
                android:gravity="center|left"
                android:text="CANCELAR"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.783"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.659" />

            <Button
                android:id="@+id/buttonIngresarEspecimenes"
                android:layout_width="129dp"
                android:layout_height="41dp"
                android:layout_marginStart="68dp"
                android:text="INGRESAR"
                app:backgroundTint="#00BCD4"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/buttonCancelar"
                app:layout_constraintHorizontal_bias="0.85"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.659" />
        </androidx.constraintlayout.widget.ConstraintLayout>


    </LinearLayout>
</ScrollView>

Logcat message when button is pressed:按下按钮时的 Logcat 消息:

com.example.luvin.drawercero D/ViewRootImpl@6731d38[MainActivity]: ViewPostIme pointer 1

To complete my question, I have the permissions in the manifest.为了完成我的问题,我在清单中有权限。 I already tried to call the camera in another way, tried creating the method in the MainActivity to create the OnClick event in the XML Layout, but nothing works.我已经尝试以另一种方式调用相机,尝试在MainActivity中创建方法以在 XML 布局中创建OnClick事件,但没有任何效果。
If anyone knows how to solve this issue please help.如果有人知道如何解决这个问题,请帮忙。
Thanks.谢谢。

You forgot to add the on click listener to your photo button.您忘记将点击监听器添加到您的照片按钮。 This should solve your problem:这应该可以解决您的问题:

btnCamara = view.findViewById(R.id.tomarFoto);
btnCamera.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        tomarFoto(v);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM