Android Fragments Tutorial App Crash

+1 Aloysius Lee · January 29, 2015
I followed bucky's tutorial on fragments (23 - 30) closely and whenever i click the button to create the meme, it crashes saying "Unfortunately, Fragments tutorial(appname) has stopped". I don't know what the problem is. Here's the crash log.

01-29 23:09:26.382    1824-1824/com.cyanoise.fragmentstutorial E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.cyanoise.fragmentstutorial, PID: 1824
    java.lang.ClassCastException: com.cyanoise.fragmentstutorial.TopSectionFragment cannot be cast to com.cyanoise.fragmentstutorial.BottomPictureFragment
            at com.cyanoise.fragmentstutorial.MainActivity.createPic(MainActivity.java:19)
            at com.cyanoise.fragmentstutorial.TopSectionFragment.buttonClicked(TopSectionFragment.java:54)
            at com.cyanoise.fragmentstutorial.TopSectionFragment$1.onClick(TopSectionFragment.java:45)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


Help would be appreciated. Thanks!

Post a Reply

Replies

Oldest  Newest  Rating
+1 Neil Cannon · February 19, 2015
FULL MEME CODE!

MainActivity.java

package com.example.ccuk.memecreator;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity implements TopSectionFragment.TopSectionListener{

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

    // This gets called by the Top Fragment when the user clicks the button
    @Override
    public void createMeme(String top, String bottom) {

    BottomPictureFragment bottomFragment = (BottomPictureFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
    bottomFragment.setMemeText(top, bottom);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


TopSectionFragment.java

package com.example.ccuk.memecreator;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;


public class TopSectionFragment extends Fragment {

    private static EditText topTextInput;
    private static EditText bottomTextInput;


    TopSectionListener activityCommander;

    public interface TopSectionListener {

        public void createMeme(String top,String bottom);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {

            activityCommander = (TopSectionListener) activity;
        }catch(ClassCastException e){
            throw new ClassCastException(activity.toString());

        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.top_section_fragment, container, false);

        topTextInput = (EditText) view.findViewById(R.id.topTextInput);
        bottomTextInput = (EditText) view.findViewById(R.id.bottomTextInput);
        final Button button = (Button) view.findViewById(R.id.button);

        button.setOnClickListener(
                new View.OnClickListener() {
            public void onClick (View v){
            buttonClicked(v);

            }
        }
        );
        return view;
    }

    /// Calls this when the button is clicked

    public void buttonClicked(View view){
        activityCommander.createMeme(topTextInput.getText().toString(), bottomTextInput.getText().toString());

    }
}

BottomPictureFragment.java


package com.example.ccuk.memecreator;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class BottomPictureFragment extends Fragment {


    private static TextView topMemeText;
    private static TextView bottomMemeText;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bottom_picture_fragment, container, false);

        topMemeText = (TextView) view.findViewById(R.id.topMemeText);
        bottomMemeText = (TextView) view.findViewById(R.id.bottomMemeText);


        return view;
    }

    public void setMemeText(String top, String bottom){
        topMemeText.setText(top);
        bottomMemeText.setText(bottom);

    }

}


activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_horizontal_margin">

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.ccuk.memecreator.TopSectionFragment"
        android:id="@+id/fragment"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/top_section_fragment" />

    <fragment
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:name="com.example.ccuk.memecreator.BottomPictureFragment"
        android:id="@+id/fragment2"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/bottom_picture_fragment" />
</RelativeLayout>

top_section_fragment.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">



    <EditText
        android:id = "@+id/topTextInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:width="300dp"/>

    <EditText
        android:id = "@+id/bottomTextInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/topTextInput"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:width="300dp"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text"
        android:layout_below="@+id/bottomTextInput"
        android:layout_marginTop="15dp"
        android:layout_centerHorizontal="true"/>

    
</RelativeLayout>


bottom_picture_fragment.xml



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/jupiter">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/top_text"
        android:id="@+id/topMemeText"
        android:layout_centerHorizontal="true"
        android:textColor="#FFF"
        android:gravity="center_horizontal" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/bottom_text"
        android:id="@+id/bottomMemeText"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:textColor="#FFF"
        android:gravity="center_horizontal" />
</RelativeLayout>
+1 Julian Gaines · February 19, 2015
I am trying to simulate my fragment code based off bucky's video.  I followed his steps in creating the topsectionfragment and the bottompicture fragment.  In the MainActvity.java when I created the createMeMe method and tried to to import the bottompicture fragment into java it gives me an error saying that: inconvertible types: cannot cast 'android.support.v4.app.Fragment' to 'com......BottomPictureFragment'.  I not sure what this message means.  Can someone help please.  Here is my MainActivity code below.

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity implements TopSectionFragment.TopSectionListener{

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

    //this gets called by the Top Fragment when the user clicks the button
    @Override
    public void createMeme(String top,String bottom) {
        BottomPictureFragment bottomFragment = (BottomPictureFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
        //bottomFragment.setMemeText(top, bottom);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
+1 Aloysius Lee · January 30, 2015
Yep i accidentally used a topFragment instead of a bottomFragment. Thanks for your help!
  • 1

Java / Android Development

107,307 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator