【问题标题】:Cannot resolve method placeholderfragment error无法解决方法占位符片段错误
【发布时间】:2014-11-14 02:34:45
【问题描述】:

我真的被这个错误困住了(下):

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

我将容器正确引用到 XML 布局(如下):

<?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"/>

但我不断收到无法解析 PlacehlderFragment 的方法的错误,知道我做错了什么吗?

【问题讨论】:

  • 你能发布堆栈跟踪吗?
  • 这是我使用stacktrace得到的:schemas.android.com/apk/res/android" xmlns:tools="@ 987654322@" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container" tools:ignore="MergeRootFrame" tools:context="com.customerautomotivenetwork.VehicleRecall"/>跨度>
  • 堆栈跟踪基本上是应用程序崩溃时的错误消息,而不是 xml 布局。它显示了线程终止之前堆栈上的所有方法调用的跟踪。您可以在 Eclipse 的 logcat 窗口中找到它。
  • 我正在使用Android Studio,但我会找到它
  • 好的,这就是我得到的:错误:(34, 21) 错误:找不到适合 add(int,PlaceholderFragment) 方法的方法 FragmentTransaction.add(int,Fragment,String) 不适用(实际参数列表和形式参数列表长度不同)方法 FragmentTransaction.add(int,Fragment) 不适用(实际参数 PlaceholderFragment 无法通过方法调用转换转换为 Fragment)方法 FragmentTransaction.add(Fragment,String) 不适用(实际参数int不能通过方法调用转换为Fragment)

标签: java android android-activity android-fragments error-handling


【解决方案1】:
  • 您可能扩展了错误版本的Fragment

在您的文件PlaceholderFragment.java 中,首先确保您扩展了Fragment

public class PlaceholderFragment extends Fragment { //...

然后,检查该文件开头的导入。应该有一行:

import android.app.Fragment;

NOT 行:

import android.support.v4.app.Fragment;

尽量不要混用“普通”和“v4”片段,它们有相同的名称和做相同的事情,但它们实际上是不同的类。

【讨论】:

    【解决方案2】:

    我必须导入 android.support.v4.app.Fragment 来解决这个问题。

    在我的例子中,我使用的是 minSdkVersion 8,并且我创建了一个 Activity 类 java > Package > Acitivity > BlankActivity。这个创建的活动类有一个内部类公共静态类 PlaceholderFragment 扩展片段。这个内部类在

    中的 onCreate() 中使用
    @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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多