片段位于android.support.v7

时间:2018-09-15 08:53:18

标签: android android-fragments

我是android新手,我试图在android支持v7库中创建一个片段 当我想从片段类中创建一个对象,并用片段Java将其新建时,发生了无法编译类型的错误。

MainActivity

mongorep:PRIMARY> rs.add("hostname")
{


    "operationTime" : Timestamp(1537000835, 1),
        "ok" : 0,
        "errmsg" : "Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2",
        "code" : 103,
        "codeName" : "NewReplicaSetConfigurationIncompatible",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1537000835, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }

}

此行有错误

public class MainActivity extends AppCompatActivity { private TextView mTextMessage; private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.navigation_home: mTextMessage.setText(R.string.title_home); return true; case R.id.navigation_dashboard: mTextMessage.setText(R.string.title_dashboard); return true; case R.id.navigation_notifications: mTextMessage.setText(R.string.title_notifications); return true; } return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextMessage = (TextView) findViewById(R.id.message); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); } public boolean test(){ Fragment fr = new navigation_page1(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.fragment_page1,fr); } }

Fragment fr = new navigation_page1();是我的片段Java代码,已在我的主布局中使用片段标签进行了初始化。

我的main_xml代码:

navigation_page1

我的navigation_page1代码:

        <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.example.aqamamad.myapplication.navigation_page1"
            android:id="@+id/fragment_page1"

    ></fragment>
    <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation"/>
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

问题解决了。

片段化Java类并删除此行-> import android.support.v4.app.Fragment;

而不是写这行-> import android.app.Fragment;