ChaL
ChaL

Reputation: 11

Java - Java.lang.RuntimeException: Unable to start activity ComponentInfo

I already saw this kind of question on the forum, tried a lot of things, but never found a solution to my problem. I am simply trying to insert a fragment in a main activity and can't get rid of this exception message... I am coding on Android Studio version 0.9.9. with jdk 1.7

Here is the code for the main activity and the PlaceholderFragment:

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

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

    if(savedInstanceState == null) {
        getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment())
                .commit();
    }

}


@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);
}

/**
 * A Placeholder fragment that contains a simple view
 */
public static class PlaceholderFragment extends Fragment {

    private ArrayAdapter<String> mTagListAdapter;

    public PlaceholderFragment() {
        super();
    }


    public View OnCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.library_fragment, container, false);

        final TextView answerLabel=(TextView)rootView.findViewById(R.id.listview_tag);


        return rootView;

    }

}

}

I have to precise that I couldn't override the OnCreateView() method ("method cannot override on the superclass method"), but I saw on the internet that it doesn't matter.

Here are my two xml files: activity_three.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"
tools:context=".MainActivity"
android:background="@color/background_grey">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="81dp" />

And library_fragment.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="64dp"
android:paddingRight="64dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context=".PlaceholderFragment">

<TextView
    android:id="@+id/listview_tag"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_world"
    />

Finally, I got this as log message:

java.lang.RuntimeException: Unable to start activity   
ComponentInfo{com.eseoteam.android.tagfinderbeta/com.eseoteam.android.tagfinderbeta.MainActivity}:
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup

Thank you a lot for your help, I am kind of desperate right now...

Upvotes: 0

Views: 259

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Here:

getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment())

You are passing TextView to add method of FragmentManager.

You need pass View Container id which is able to hold other views like RelativeLayout, LinearLayout, FrameLayout,... to add method of FragmentManager.

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157437

container has to be a ViewGroup. In activity_three.xml change

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="81dp" />

to

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="81dp" />

Upvotes: 0

Related Questions