簡體   English   中英

android studio沒有合適的.add方法

[英]android studio no suitable method for .add

嘿大家我只是想跟隨一個關於開發android項目的課程但是在這里我遇到了一個障礙(錯誤)。 但是我輸入的每一段代碼都是因為它在我遇到這個錯誤的過程中。

Error:(19, 21) error: no suitable method found for add(int,PlaceholderFragment)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable
(argument mismatch; PlaceholderFragment cannot be converted to Fragment)

這是我的activity_main布局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.recreated.wedowhatwelearn.MainActivity"
   tools:ignore="MergeRootFrame"/>

這是我的fragment_main.xml布局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.recreated.wedowhatwelearn.MainActivity$PlaceHolderFragment">

    <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listviewsorular" />


</FrameLayout>

最后這是我的MainActivity.java文件

package com.example.recreated.wedowhatwelearn;

import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

    }



    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment(){
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
           View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            return rootView;
        }
    }


}

你的PlaceholderFragment是一個android.app.Fragment 由於您使用支持FragmentManager (通過getSupportFragmentManager() ,),它需要是android.support.v4.app.Fragment

只需更改您的導入,一切都應該正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM