当我导入 android.R
时,我收到 Error_1 并且 Error_2 已解决。那么,我该如何解决Error_1.我尝试更改 import android.R
--> import com.example.testing.R
。然而它仍然不起作用。
甚至我的login.xml似乎也能找到。没有错误。我也尝试过清理该项目。还是什么也没发生。
LoginActivity.java
package com.example.testing;
import android.R; //Error_1 : Don't include android.R here; use a fully qualified name for each usage instead
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class LoginActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setting default screen to login.xml
setContentView(R.layout.login); //Error_2 : login cannot be resolved or is not a field
TextView registerScreen = (TextView) findViewById (R.id.link_to_register);
//Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//Switching to Register Screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
}
}
这是我的login.xml
login.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<LinearLayout android:id="@+id/header"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header_gradient"
android:paddingTop="5dip"
android:paddingBottom="5dip">
</LinearLayout>
<!-- Footer -->
<LinearLayout android:id="@+id/footer"
android:orientation="horizontal"
android:contentDescription="@string/description"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="@layout/footer_repeat"
android:layout_alignParentBottom="true">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_below="@id/header">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="@string/name" />
<EditText android:id="@+id/editText1"
android:hint="@string/edit_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="20dip"
android:singleLine="true"/>
<!-- Password Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="@string/password"/>
<EditText android:id="@+id/editText2"
android:hint="@string/edit_message1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:inputType="textVisiblePassword"/>
<Button android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/login"/>
<TextView android:id="@+id/link_to_register"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:layout_marginBottom="40dip"
android:text="@string/loginDesc"
android:gravity="center"
android:textSize="20sp"
android:textColor="#0b84aa"/>
</LinearLayout>
<!-- Login Form Ends -->
</RelativeLayout>
</ScrollView>
最佳答案
- 确保您的 XML 中没有错误
- 如果有任何正确的话,然后项目->构建全部
- 如果一切顺利,您的 XML 附近将不会出现红叉[我指的是 eclipse IDE]
- 您不必导入 android.R,它在每个源文件中都是可见且可访问的,除非您更改了属性
注意:您可能会遇到 res 文件夹中存在 .out 文件的问题,这是 Eclipse 的一个错误,当您在 XML 文件打开时尝试编译项目时会发生该错误。如果是这样,请删除它并尝试
关于java - 此处不要包含 android.R;为每个用法使用完全限定名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19872651/