添加底部导航活动后编译错误(非法字符)
Posted
技术标签:
【中文标题】添加底部导航活动后编译错误(非法字符)【英文标题】:Compilation error after adding a Bottom Navigation Activity (illegal character) 【发布时间】:2020-09-11 02:03:45 【问题描述】:在我添加底部导航活动并尝试从另一个活动中调用它后,在编译时出现错误:
project directory\ui\dashboard\DashboardFragment.java:13: error: illegal character: '`'
import `in`.ac.somedomain.someappname.R;
而 DashboardFragment.java 中导致错误的代码是导入行:
import `in`.ac.somedomain.someappname.R;
我只添加了一个新的底部导航活动,并尝试使用:
Intent homeIntent = new Intent(getApplicationContext, HomeActivity.class);
startActivity(homeIntent);
在手动删除“`”时(在 3 个活动中:DashboardFragment、HomeFragment、NotificationsFragment),调用活动时出现以下错误(logcat,应用程序崩溃):
2020-05-24 16:29:02.711 14323-14323/in.ac.somedomain.someappname E/androidRuntime: FATAL EXCEPTION: main
Process: in.ac.somedomain.someappname, PID: 14323
java.lang.RuntimeException: Unable to start activity ComponentInfoin.ac.somedomain.someappname/in.ac.somedomain.someappname.BaseActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2943)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3078)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6854)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:48)
at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:100)
at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:218)
at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:220)
at in.ac.somedomain.someappname.BaseActivity.onCreate(BaseActivity.java:26)
at android.app.Activity.performCreate(Activity.java:7151)
at android.app.Activity.performCreate(Activity.java:7142)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2923)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3078)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6854)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
将鼠标悬停在代码中的“`in`”上时出现的错误是 Raw string literals are not supported at language level '7'
,我尝试了它的一些建议(将 Source Compatibility 切换到 1.8,这与级别 8 相同),禁用了一些没有'没有改变任何东西,现在它只是告诉我去模块设置,但我怀疑原始字符串是问题所在。
我可以做些什么来使我的底部导航活动正常工作?
编辑:活动代码(底部导航活动,称为 Main2Activity):
package in.ac.somedomain.somename;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
public class Main2Activity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
//the below line was causing the problem
// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
【问题讨论】:
能分享一下底部导航活动代码吗? 更新,默认添加了一行修改了由于主题不存在的Action Bar(已解决) 【参考方案1】:首先,替换导入代码:
import `in`.ac.somedomain.someappname.R;
开:
import ac.somedomain.someappname.R;
然后错误表明您有一个指向操作栏的链接= null。 这个答案可能对您有所帮助:answer
如果这没有帮助,请显示您的 Java 代码和清单。
还值得一提的是,action bar 和 bottom bar 是不同的东西。
【讨论】:
我实际上是在使用一个没有操作栏的主题(扩展它),并且代码尝试在默认情况下用它做一些事情(NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
),所以删除它修复了崩溃。至于导入行,删除 `in` 完全使 ac
成为未知符号,但现在只需从所有受影响的活动中删除“`”就可以了以上是关于添加底部导航活动后编译错误(非法字符)的主要内容,如果未能解决你的问题,请参考以下文章