嘿大家我只是想跟随一个关于开发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;
}
}
}
答案 0 :(得分:3)
您的PlaceholderFragment
是android.app.Fragment
。由于您使用的是支持FragmentManager
(通过getSupportFragmentManager()
),因此需要android.support.v4.app.Fragment
。
只需更改导入即可,一切正常。