Android:按钮不起作用,但代码没有显示任何错误
Posted
技术标签:
【中文标题】Android:按钮不起作用,但代码没有显示任何错误【英文标题】:Android: Button doesn't work, but code doesn't show any error 【发布时间】:2021-09-22 03:02:38 【问题描述】:我正在尝试创建一个表单来捕获数据,我需要访问相机来拍照。我的问题是当我尝试使用imagenButton
调用相机时,它不起作用但代码没有显示任何错误。
这是我的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);
这是我的 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_
android:layout_
tools:context=".Especimenes.EspecimenesConsultarFragment">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:layout_
android:layout_
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_
android:layout_
android:orientation="vertical">
<FrameLayout
android:layout_
android:layout_
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageButton
android:id="@+id/tomarFoto"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:orientation="vertical">
<Button
android:id="@+id/buttonCancelarEspecimenes"
android:layout_
android:layout_
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_
android:layout_
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消息:
com.example.luvin.drawercero D/ViewRootImpl@6731d38[MainActivity]: ViewPostIme pointer 1
为了完成我的问题,我拥有清单中的权限。我已经尝试以另一种方式调用相机,尝试在 MainActivity
中创建方法以在 XML 布局中创建 OnClick
事件,但没有任何效果。
如果有人知道如何解决此问题,请提供帮助。
谢谢。
【问题讨论】:
我想你忘了将tomarFoto
函数添加到按钮的回调中。
这样的? btnCamara.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
tomarFoto(this);
);
【参考方案1】:
您忘记将点击监听器添加到您的照片按钮。这应该可以解决您的问题:
btnCamara = view.findViewById(R.id.tomarFoto);
btnCamera.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
tomarFoto(v);
);
【讨论】:
以上是关于Android:按钮不起作用,但代码没有显示任何错误的主要内容,如果未能解决你的问题,请参考以下文章