3

I'm really stuck on this error (below):

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

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

I have the container referenced correctly to XML layout (below):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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:id="@+id/container"
    tools:context="com.customerautomotivenetwork.VehicleRecall"/>

But I keep getting an error of cannot resolve method for PlacehlderFragment, any idea of what I'm doing wrong?

Izzy
  • 6,740
  • 7
  • 40
  • 84
Marcus Lee
  • 107
  • 1
  • 7
  • 2
    Can you post the stack trace? – Gil Vegliach Sep 19 '14 at 13:27
  • This is what I get using stacktrace: – Marcus Lee Sep 19 '14 at 14:51
  • The stack trace is basically the error message when you app crashes, not the xml layout. It shows a trace of all the method calls that where on the stack just before your thread terminated. You can find it in your logcat window in Eclipse. – Gil Vegliach Sep 19 '14 at 15:12
  • I'm using Android Studio, but I will find it – Marcus Lee Sep 19 '14 at 15:34
  • ok this is what I got: Error:(34, 21) error: no suitable method found for add(int,PlaceholderFragment) method FragmentTransaction.add(int,Fragment,String) is not applicable (actual and formal argument lists differ in length) method FragmentTransaction.add(int,Fragment) is not applicable (actual argument PlaceholderFragment cannot be converted to Fragment by method invocation conversion) method FragmentTransaction.add(Fragment,String) is not applicable (actual argument int cannot be converted to Fragment by method invocation conversion) – Marcus Lee Sep 19 '14 at 18:00

2 Answers2

3
  • You probably extending the wrong version of Fragment

In your file PlaceholderFragment.java, first make sure you extend Fragment:

public class PlaceholderFragment extends Fragment { //...

Then, check the imports at the beginning of that file. There should be the line:

import android.app.Fragment;

and NOT the line:

import android.support.v4.app.Fragment;

Try not to mix "normal" and "v4" Fragments, they have the same name and do the same things but they are actually different classes.

Gil Vegliach
  • 3,542
  • 2
  • 25
  • 37
1

I had to import android.support.v4.app.Fragment to fix this problem.

In my case I was using minSdkVersion 8 and I had created a Activity class java > Package > Acitivity > BlankActivity. This created activity class had a inner class public static class PlaceholderFragment extends Fragment. This inner class was used in onCreate() in

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

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