无法解析 FragmentTransaction.add() 方法。为什么会出现此错误?
cannot resolve FragmentTransaction.add() method . Why am I getting this error?
我已经看过其他答案,但其中 none 对我有帮助。
我确保使用 import android.support.v4.app.Fragment;
所以这不是问题。
MainActivity.java
package com.example.nirvan.fragmentsexample2;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFragment myfragment=new myFragment();
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer,myfragment);
fragmentTransaction.commit();
}
}
错误:
Error:(20, 28) error: no suitable method found for add(int,myFragment)
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; myFragment cannot be converted to Fragment)
为什么我会收到这个?
在activity_main.xml我有一个FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:id="@+id/fragmentContainer">
</FrameLayout>
myFragment.java
package com.example.nirvan.fragmentsexample2;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class myFragment extends Fragment
{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState)
{
return inflater.inflate(R.layout.fragment,container,false);
}
public myFragment(){}
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="145dp"
android:text="fragment One"/>
</RelativeLayout>
改变这个
import android.app.Fragment;
到
import android.support.v4.app.Fragment;
在 myFragment
class
此外,我建议为您的片段制定适当的命名约定。
相对布局不需要android:orientation="vertical"
。
草率的错误。您应该调用 v4.app.Fragment
而不是 app.Fragment
.
打开myFragment.java
不要
import android.app.Fragment;
做
import android.support.v4.app.Fragment;
我已经看过其他答案,但其中 none 对我有帮助。
我确保使用 import android.support.v4.app.Fragment;
所以这不是问题。
MainActivity.java
package com.example.nirvan.fragmentsexample2;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFragment myfragment=new myFragment();
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer,myfragment);
fragmentTransaction.commit();
}
}
错误:
Error:(20, 28) error: no suitable method found for add(int,myFragment)
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; myFragment cannot be converted to Fragment)
为什么我会收到这个?
在activity_main.xml我有一个FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:id="@+id/fragmentContainer">
</FrameLayout>
myFragment.java
package com.example.nirvan.fragmentsexample2;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class myFragment extends Fragment
{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState)
{
return inflater.inflate(R.layout.fragment,container,false);
}
public myFragment(){}
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="145dp"
android:text="fragment One"/>
</RelativeLayout>
改变这个
import android.app.Fragment;
到
import android.support.v4.app.Fragment;
在 myFragment
class
此外,我建议为您的片段制定适当的命名约定。
相对布局不需要android:orientation="vertical"
。
草率的错误。您应该调用 v4.app.Fragment
而不是 app.Fragment
.
打开myFragment.java
不要
import android.app.Fragment;
做
import android.support.v4.app.Fragment;