android - 如何解决不可转换类型无法将 "Android.support.v4.app.fragment"转换为 "packagename"

标签 android navigation-drawer

我正在创建一个由 android studio 中的抽屉导航组成的 android 应用程序。 我收到一个名为 inconvertable types cannot cast "How to solve inconvertable types cannot cast "Android.support.v4.app.fragment"to "packagename"" 的错误,请帮助我解决这个问题。 这是我的 activity_main.java

package sample.lakshman.com.sampleltester;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
import sample.lakshman.com.sampleltester.Fragment_navigation;




public class MainActivity extends ActionBarActivity {
    public Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);


        Fragment_navigation drawer_navigation = (Fragment_navigation)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawer_navigation.setUp((DrawerLayout)findViewById(R.id.drawer_layout),toolbar);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if(id==R.id.navigation_item)
        {
            Intent sub = new Intent(MainActivity.this,Subactivity.class);
            startActivity(sub);
        }
        return super.onOptionsItemSelected(item);
    }
}

这是我的 main_activity.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/drawer_layout"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar" />
    </RelativeLayout>
    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_fragment_navigation"
        android:name="sample.lakshman.com.sampleltester.Fragment_navigation"
        tools:layout="@layout/fragment_fragment_navigation" />

</android.support.v4.widget.DrawerLayout>

最佳答案

只需转到您的 Fragment_navigation 类并 替换

导入android.app.Fragment;

导入android.support.v4.app.Fragment;

关于android - 如何解决不可转换类型无法将 "Android.support.v4.app.fragment"转换为 "packagename",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28898996/

相关文章:

java - 长按触发点击,长按不注册

android - 非法参数异常 - DrawerLayout 必须使用 MeasureSpec.EXACTLY 进行测量

Android抽屉导航触发区域太小

java - 使用 View 绑定(bind)膨胀类 ImageView 时出错

java - 从 Java 转换为 Kotlin OnClickListener

android - 在 Android 的 ListView 上将 Action 模式显示为 LongClick

android - 为什么我在使用抽屉导航时出现错误的图标?

android - 抽屉导航更新导致崩溃(应用程序紧凑版 v7)

android - 用户无法与 Seekbar 交互

android - 如何在android中的imageviews之间切换图像?