在 android studio 中找不到符号错误
Posted
技术标签:
【中文标题】在 android studio 中找不到符号错误【英文标题】:Cannot find symbol error in android studio 【发布时间】:2019-11-16 01:52:17 【问题描述】:我正在尝试将片段添加到导航抽屉。但我有错误:找不到符号类片段。
我添加了:import android.support.v4.app.Fragment
同时添加片段对象(Fragment fragment)
错在哪里? 我该怎么办?
Home.java
package com.example.eatit;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.view.View;
import androidx.core.view.GravityCompat;
import androidx.appcompat.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.support.v4.app.Fragment;
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
Fragment fragment = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
@Override
public void onBackPressed()
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);
else
super.onBackPressed();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, 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;
return super.onOptionsItemSelected(item);
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
/* Handle navigation view item clicks here. */
int id = item.getItemId();
if (id == R.id.nav_home)
// Handle the camera action
else if (id == R.id.nav_gallery)
else if (id == R.id.nav_slideshow)
else if (id == R.id.nav_tools)
else if (id == R.id.nav_share)
else if (id == R.id.nav_send)
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
build.grandle
apply plugin: 'com.android.application'
android
compileSdkVersion 29
buildToolsVersion "29.0.0"
defaultConfig
applicationId "com.example.eatit"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
dependencies
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-database:16.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
testİmplementation 'junit:junit:4.12'
androidTestİmplementation 'androidx.test:runner:1.2.0'
androidTestİmplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.google.firebase:firebase-core:10.2.0'
implementation 'com.google.firebase:firebase-database:17.0.0'
implementation 'info.hoang8f:fbutton:1.0.5'
implementation 'com.rengwuxian.materialedittext:library:2.1.4'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.firebaseui:firebase-ui-database:1.2.0'
//noinspection GradleCompatible
implementation 'com.android.support:design:26.+'
def nav_version = "2.1.0-alpha06"
// Java
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
apply plugin: 'com.google.gms.google-services'
apply plugin: "androidx.navigation.safeargs"
我已经尝试搜索网络和论坛,但到目前为止我没有找到任何有用的信息。
非常感谢。
【问题讨论】:
post Fragment 类或添加 Fragment 的代码也 【参考方案1】:将此行添加到您的 Build.gradle 文件中:
implementation "com.android.support:support-v4:latest_version"
例子:
implementation "com.android.support:support-v4:26.+"
【讨论】:
【参考方案2】:当您将项目配置为使用 AndroidX 时,您正在从旧的 Android 支持库 (android.support.v<x>
) 导入 Fragment
,因此请确保您正在导入和使用正确的库。
请访问this link 以查找Fragment
的正确导入。
将
android.support.v4.app.Fragment
替换为androidx.fragment.app.Fragment
。
【讨论】:
成功了!非常感谢 !我确实构建了应用程序,没有错误,但我在 'nav_home'/'nav_home' 上仍然有红色字体......并且消息是无法解析符号 'nav_home' 你知道吗? 再次出现同样的错误。请检查我附加的链接【参考方案3】:2021 年?
我不是 gradle 专家,但这似乎有帮助
implementation "androidx.navigation:navigation-fragment-ktx:2.3.4"
例如 FragmentContainerView
似乎需要?
【讨论】:
【参考方案4】:改变:
import androidx.core.app.Fragment;
import androidx.core.app.FragmentManager;
import androidx.core.app.FragmentPagerAdapter;
到:
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
来源:https://developer.android.com/reference/androidx/fragment/app/package-summary
更新:你必须通过重构自动更新到androidx,之后一些不会自动更新,需要手动更新(复制,过去),更多信息:https://developer.android.com/jetpack/androidx/migrate
【讨论】:
以上是关于在 android studio 中找不到符号错误的主要内容,如果未能解决你的问题,请参考以下文章
在 Android Studio 项目中找不到参数的方法 android()