为什么我的主函数在声明时没有在我的活动文件中被选取?
我正在对某人向我提供代码的应用程序进行测试,除了我的 Activity.java 之外,一切正常,它显示 R.layout.main 并且显示 main 无法解析。已经声明了,为什么检测不到呢?这里是
Activity.java
package magic.test.namespace;
import android.R;
import android.app.Activity;
import android.os.Bundle;
public class SaytheMagicWordActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
main.xml下面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/bt_speak"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Speak"
/>
<TextView
android:id="@+id/tv_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
I am running a test on an app that someone provided me the code for, and I got everything going except my activity.java, it says R.layout.main and it says main can't be resolved. Its declared, why won't it detect it? Here it is below
Activity.java
package magic.test.namespace;
import android.R;
import android.app.Activity;
import android.os.Bundle;
public class SaytheMagicWordActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/bt_speak"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Speak"
/>
<TextView
android:id="@+id/tv_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除
import android.R
,以便它将在您的项目中查找R.layout.main
而不是android.R
Remove
import android.R
so it will look forR.layout.main
in your project instead ofandroid.R
R.java 文件会根据资源目录中的修改自动生成。如果您的 SaytheMagicWordActivity 类在解析 R.layout.main 时显示编译错误,则表明此自动生成已被某些内容搞砸。
您的项目中应该有一个名为“res”的目录,并且它应该有一个子目录“layout”,其中有一个 main.xml 文件(res/layout/main.xml)。你看到这个文件了吗?当您更改此文件时会发生什么?如果您强制重新编译 R.java,并且您的目录结构设置正确,您应该会在其中看到 main 条目。
只需运行 Andriod HelloWorld 应用程序,您就会看到基本结构。
The R.java file gets auto-generated in response to modifications in the resource directory. If your SaytheMagicWordActivity class is showing a compile error resolving R.layout.main, something has screwed up this auto-gen.
You should have a directory in your project should name "res" and it should have a subdirectory "layout" in which there is a main.xml file (res/layout/main.xml). Do you see this file? What happens when you change this file? If you force a recompile of the R.java, you should see the entry for main in there if your directory structure is set up correctly.
Just do the Andriod HelloWorld app and you'll see the basic structure.