我是 Android 和 Java 的新手,我知道这个网站上有很多关于点击按钮开始新 Activity 的问题和答案。我阅读了它们,甚至在 YouTube 上观看了一些视频,但我不知道我做错了什么。我知道这对你来说很简单,希望你能帮助我。每当我运行时,我都会收到如下错误。
错误:找不到符号类 View 找不到符号类 Intent 。 找不到符号类 Intent 。 错误:任务 ':app:compileDebugJava' 执行失败。
Compilation failed; see the compiler error output for details.
我正在使用 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>
在我想打开的其他 Activity 中有什么我应该做的吗?
提前致谢。
最佳答案
我看到您在这里缺少所有导入。如果您使用的是 Eclipse,请尝试按 Ctrl+Shift+o 获取所有导入。
此外,确保您的 Activity 已在您的 XML 文件中注册。它需要在您的应用程序的 list 文件中注册才能识别您的 Activity 。
关于android - 单击按钮启动新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24962549/