【发布时间】:2014-07-25 18:46:50
【问题描述】:
我是 Android 和 Java 的新手,我知道这个网站上有很多关于单击按钮启动新活动的问题和答案。我阅读了它们,甚至在 YouTube 上观看了一些视频,但我不知道我做错了什么。我知道这对你来说很简单,希望你能帮助我。每当我运行时,我都会收到如下错误。
错误:找不到符号类视图 找不到符号类意图。 找不到符号类意图。 错误:任务 ':app:compileDebugJava' 执行失败。
编译失败;有关详细信息,请参阅编译器错误输出。
我使用的是 Android Studio(测试版)0.8.2
下面是Java代码
package com.abc.xyz;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.abc.xyz.R;
public class AddNew extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_new, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void goo(View view)
{
Intent intent=new Intent (this, AddNewWeb.class);
startActivity(intent);
}
}
以下是xml布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.abc.xyz.AddNew"
android:baselineAligned="false">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:onClick="goo"/>
</LinearLayout>
在我要打开的其他活动中我应该做什么?
提前致谢。
【问题讨论】:
-
你导入android.view.View了吗?将此添加到您的导入中,您是否在 Eclipse 中?
-
首先,您应该在处理任何类型的错误时发布整个堆栈跟踪。其次,“找不到符号”通常意味着您缺少导入或库。在这里,您缺少意图和视图的导入:
import android.content.Intent; import android.view.View;。您可以尝试添加它以查看它是否有效?第三,你能不能把你的AndroidManifest.xml的内容也贴出来? -
谢谢。我没有导入它们。我正在使用 Android Studio,它曾经在我们必须导入某些内容时显示灯泡,但现在没有显示,我是否更改了任何设置?