我第二次尝试教程并得到了我无法解决的相同错误。我在MainActivityTextview上得到红色下划线。 我分配了一个文本框,其ID为“MainActivityTextview”,现在我试图将文本设置为它。 简单吧?但是我无法让它发挥作用。我错过了一个导入? 感谢
MainActivity.java
package com.Proteintracker;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(android.R.id.MainActivityTextview);
textView.setText("hello world");
}
}
r.java
package com.Proteintracker;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_action_search=0x7f020000;
public static final int ic_launcher=0x7f020001;
}
public static final class id {
public static final int MainActivityTextview=0x7f070000;
public static final int menu_settings=0x7f070001;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int activity_main=0x7f060000;
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int hello_world=0x7f040001;
public static final int menu_settings=0x7f040002;
public static final int title_activity_main=0x7f040003;
}
public static final class style {
public static final int AppTheme=0x7f050000;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/MainActivityTextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
答案 0 :(得分:1)
使用
TextView textView = (TextView) findViewById(R.id.MainActivityTextview);
而不是
TextView textView = (TextView) findViewById(android.R.id.MainActivityTextview);
更改这些行之后,您必须从Eclipse IDE上的Option菜单中的Project->Clean
刷新并清理项目
答案 1 :(得分:1)
TextView textView = (TextView) findViewById(R.id.MainActivityTextview);
删除“android。”,它是你的资源,而不是系统。