2

我正在尝试从 Android 开发者网站关注构建您的第一个应用程序,但我被困在课程:开始另一个活动

我下载并安装了 Eclipse SDK 和 ADT 插件,这就是我正在使用的。

我被挂断在上面链接的页面上的步骤上,上面写着:

打开 MainActivity 类,添加对应的方法:

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
}

提示:在 Eclipse 中,按 Ctrl + Shift + O 导入缺失的类(Mac 上的 Cmd + Shift + O)。

我不知道我应该打开什么文件。我正在使用 Windows,所以我尝试按 CTRL + Shift + O 并没有发生任何事情。我尝试查找 *.class 文件但找不到。我看到的唯一类文件是 classes.dex,我认为不是。有人可以指出我正确的方向吗?

4

6 回答 6

7

I too had this problem and spent over an hour searching for the remedy. Which I found herewith:

Go to left navigation bar and go to /src/com.example/MainActivity.java

Double click MainActivity.Java

File opens up in main window, OK, before the last "}", insert your code

yellow lines appear (indicating some sort of error!)

to resolve this, hover the mouse over the yellow lines and you will see a popup. click the option that includes "import view".

You can now continue with the tutorial...

edit:typo

于 2012-08-21T02:58:33.847 回答
1

查看Eclipse 屏幕左侧包资源管理器顶部附近的文件标题src下方。MyFirstApp然后查看打开com.example.MyFirstApp文件夹,它就在那里。打开它并开始编辑。

于 2012-08-14T05:57:11.033 回答
1

刚刚遇到同样的问题:

在包资源管理器中,该文件位于 src\com.example.my.first.app\MainActivity.java 下

com.example.my.first.app 就是我得到的。如果您以不同的方式命名项目,它应该反映这一点。

希望这可以帮助!

于 2012-08-06T16:18:22.467 回答
1

也卡在那里,但这个答案有很大帮助。不要忘记添加“import android.view.View;” 否则,您将收到“无法将视图解析为类型”的消息。

于 2012-08-16T17:21:23.397 回答
0

在按钮 onclick() 上写这个

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);

要传递信息,只需使用

myIntent.putExtra("key", value);

可以通过在另一侧检索

@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String value = intent.getStringExtra("key");

不要忘记在 AndroidManifest.xml 中添加您的新活动:

<activity android:label="@string/app_name" android:name="NextActivity"/>
于 2012-07-30T10:09:45.537 回答
0
  1. 如果您在左侧的包资源管理器中检查了 /src/ 下的每个文件夹/包,但您确定没有,这就是帮助我继续教程而无需重新开始的原因:

  2. 我也被困在那里,因为唯一的 MainActivity.java文件位于 /gen/... 而不是 /src/...,但我通过删除现有的文件并在 /src/com 创建一个新文件来修复它。 example.whatever/。新建 > 类,命名 MainActivity 并粘贴以下代码:

    package com.example.androidtest;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;    
    
    public class MainActivity extends Activity {    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }    
    

    ..现在你有你的 MainActivity.java 并且可以继续了!(您可以创建一个新的 Android 项目并将 MainActivity 复制并粘贴到您现有的项目中。您只需调整 )

  3. 万一这没有帮助,您可能一开始就没有 MainActivity 类。要解决此问题,请单击 New > Android > Android Activity,选择 Blank Activity 并在下一个面板中输入 MainActivity 作为名称。我首先尝试了这个,但出现错误,布局名称“activity_main”已经存在。

祝你好运!

于 2013-08-30T21:27:57.340 回答