Tengo un problema con una app que estoy desarrollando, no es muy complicada, resulta que desde un Fragment trato de ejecutar una activity utilizando un botón, no hay errores de compilación, pero cuando pincho un botón la activity correspondiente no se ejecuta, sale de la aplicación y muestra el siguiente error Unfortunately has stopped
.
A continuación el código del fragment.
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link PresentacionFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link PresentacionFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class PresentacionFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public PresentacionFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment AcercadeFragment.
*/
// TODO: Rename and change types and number of parameters
public static PresentacionFragment newInstance(String param1, String param2) {
PresentacionFragment fragment = new PresentacionFragment();
Bundle args = new Bundle();
args.putString( ARG_PARAM1, param1 );
args.putString( ARG_PARAM2, param2 );
fragment.setArguments( args );
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
if (getArguments() != null) {
mParam1 = getArguments().getString( ARG_PARAM1 );
mParam2 = getArguments().getString( ARG_PARAM2 );
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((MainActivity) getActivity()).setActionBarTitle( "Inicio..." );
// Inflate the layout for this fragment
return inflater.inflate( R.layout.fragment_presentacion, container, false );
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction( uri );
}
}
@Override
public void onAttach(Context context) {
super.onAttach( context );
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException( context.toString()
+ " must implement OnFragmentInteractionListener" );
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public void clickBoton(View view) {
switch (view.getId()){
case R.id.btn_areal:
Intent i = new Intent(getActivity(), AudioRealActivity.class);
getActivity().startActivity(i);
break;
case R.id.btn_sitiow:
Intent j = new Intent(getActivity(), NoticiasActivity.class);
getActivity().startActivity(j);
break;
case R.id.btn_pods:
Intent k = new Intent(getActivity(), InvooxActivity.class);
getActivity().startActivity(k);
break;
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
En el diseño del layout correspondiente se obtiene el siguiente código del xml
<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=".PresentacionFragment">
<!-- TODO: Update blank fragment layout -->
<ImageView
android:id="@+id/logo85rcm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:src="@drawable/logo85rcm" />
<ImageView
android:id="@+id/rcmdia2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="clip_horizontal"
android:src="@drawable/rcmdia" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="88dp"
android:layout_marginRight="26dp"
android:gravity="left|start"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/nav_header_subtitle"
android:textColor="@color/colorPrimary"
android:textSize="25sp"
android:textStyle="bold|italic" />
<Button
android:id="@+id/btn_areal"
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_margin="17dp"
android:layout_marginBottom="102dp"
android:background="@android:color/holo_blue_light"
android:onClick="clickBoton"
android:text="Audio Real"/>
<Button
android:id="@+id/btn_sitiow"
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_areal"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_blue_light"
android:onClick="clickBoton"
android:text="Sitio web" />
<Button
android:id="@+id/btn_pods"
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/btn_areal"
android:layout_marginRight="16dp"
android:background="@android:color/holo_blue_light"
android:onClick="clickBoton"
android:text="PodCast" />
</RelativeLayout>
Al revisar el log, encuentro el siguiente error: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
Lo que necesito es que se ejecute la activity correspondiente al botón seleccionado. Gracias
Más del Logcat, es lo que sale de color rojo.
05-20 01:44:26.282 2399-2399/cu.radiociudaddelmar.linor.rcmapk E/AndroidRuntime: FATAL EXCEPTION: main Process: cu.radiociudaddelmar.linor.rcmapk, PID: 2399 java.lang.IllegalStateException: Could not find method daleClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btn_areal' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)
Recordar que estoy haciendo la llamada desde un fragment, no desde una Activity. Gracias