[英]App not running in Android Studio Emulator
Started learning android studios and when I try to run a simple app, I keep on getting the following errors 开始学习android studio,当我尝试运行一个简单的应用程序时,我不断遇到以下错误
Error: Cannot find symbol variable toolbar 错误:找不到符号变量工具栏
Execution Failed for task :app:CompileDebugJavawithJavac 任务:app:CompileDebugJavawithJavac执行失败
Could someone please help me. 有人可以帮我吗。
Below is the code for my activity_main.xml 下面是我的activity_main.xml的代码
<?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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.mohamedali.helloworld.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="16dp"
android:text="Mohamed First App!!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Also this is the actual code 这也是实际的代码
package com.example.mohamedali.helloworld;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}catch (Exception e){
System.out.println("Problem is: " + e.getMessage());
}
}
}
There is a great tutorial about using the App Toolbar here 有一个关于使用App工具栏一个伟大的教程在这里
You will need to start by making a toolbar layout. 您需要先制作工具栏布局。
Then include it in your layout (in your case activity_main.xml) 然后将其包括在您的布局中(在您的情况下为activity_main.xml)
Then you can use the toolbar with 然后,您可以将工具栏与
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Comment out or remove these 2 lines in MainActivity 注释掉或删除MainActivity中的这两行
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
You currently have no reference to the @id+="toolbar" in your resources for your toolbar object to reference to. 您的资源中目前没有对@id + =“ toolbar”的引用,供您的工具栏对象引用。
Simply, you would need to add a toolbar in your xml with a line of reference like: 简而言之,您需要在xml中添加带有参考线的工具栏,例如:
android:id="@+id/toolbar" android:id =“ @ + id / toolbar”
This can then be referenced by the "R.id.toolbar" in your line of code: 然后可以在您的代码行中通过“ R.id.toolbar”来引用它:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 工具栏工具栏=(工具栏)findViewById(R.id.toolbar);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.