Estaba intentando ir a otra página usando button
, pero siempre falla.
Aquí está mi primera clase con su XML:
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
public void goToAttract(View v)
{
Intent intent = new Intent(getActivity().getApplication(), MainActivityList.class);
startActivity(intent);
}
}
Aquí está mi XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_marginBottom="48dp"
android:onClick="goToAttract"
android:text="Button" /></RelativeLayout>
Aquí está mi stacktrace..este es el resultado cuando usé onclicklistener
12-30 16:54:28.006: E/AndroidRuntime(992): FATAL EXCEPTION: main
12-30 16:54:28.006: E/AndroidRuntime(992): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivityList}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
12-30 16:54:28.006: E/AndroidRuntime(992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.os.Looper.loop(Looper.java:137)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-30 16:54:28.006: E/AndroidRuntime(992): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 16:54:28.006: E/AndroidRuntime(992): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 16:54:28.006: E/AndroidRuntime(992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-30 16:54:28.006: E/AndroidRuntime(992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-30 16:54:28.006: E/AndroidRuntime(992): at dalvik.system.NativeStart.main(Native Method)
12-30 16:54:28.006: E/AndroidRuntime(992): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
12-30 16:54:28.006: E/AndroidRuntime(992): at android.view.LayoutInflater.createView(LayoutInflater.java:606)
12-30 16:54:28.006: E/AndroidRuntime(992): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.view.LayoutInflater.rInflate(LayoutInflater.java:
Prueba este código una vez-
public class FindPeopleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home,
container, false);
Button button = (Button) rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateDetail();
}
});
return rootView;
}
public void updateDetail() {
Intent intent = new Intent(getActivity(), MainActivityList.class);
startActivity(intent);
}
}
Y como se sugiere por Raghunandan eliminar el código de abajo de su fragment_home.xml
-
android:onClick="goToAttract"
Quitar esto
android:onClick="goToAttract"
Entonces
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Button b = (Button)rootView.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(getActivity(), MainActivityList.class);
startActivity(intent);
}
});
return rootView;
El error dice que necesitas public void goToAttract(View v)
en la clase Activity
Could not find method goToAttract(View) in the activity class
Razón pls comprobar la respuesta por PareshMayani @
https://stackoverflow.com/questions/14139774/android-app-crashing-fragment-and-xml-onclick
Editar:
Caused by: java.lang.OutOfMemoryError
Supongo que tienes una imagen que es demasiado grande para caber y hay que reducirla. De ahí el OutOfMemoryError
.