Cannot resolve method placeholderfragment error

二次信任 提交于 2020-01-04 04:46:10

问题


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?


回答1:


  • 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.




回答2:


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


来源:https://stackoverflow.com/questions/25934917/cannot-resolve-method-placeholderfragment-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!