活动无法解析或不是字段 [英] Activity cannot be resolved or is not a field

查看:89
本文介绍了活动无法解析或不是字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行从一个活动到另一个活动的简单导航操作.

我正在使用完整的ADT捆绑包,其中包括eclipse,SDK和ADT插件,因此无需在eclipse中单独配置ADT.

1.)我首先创建两个名为 activity_main.xml test2.xml 的布局.

2.)对应的Java文件是 mainActivity.java test2.java .

3.)现在 activity_main.xml 包含一个 id ="click" 的按钮.单击此按钮后,应加载下一个活动,即 test2.xml .

4.)用onCreate()方法编写的代码为

I was trying to perform a simple navigation action from an activity to another.

I''m using the complete ADT bundle which includes eclipse, SDK and ADT plugins, so no need to configure ADT separately in eclipse.

1.) I started by creating two layouts named activity_main.xml and test2.xml.

2.) Corresponding java files are mainActivity.java and test2.java.

3.) Now activity_main.xml contains a button with id = "click" . On clicking this button next activity i.e test2.xml should be loaded.

4.) The code written in onCreate() method was

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
    btn = (Button)findViewById(R.id.click);

    //Some other code to navigate to test2 using Intent
}



5.)但是,当我这样做时, R.id.click 显示错误,提示单击无法解决或字段不可用".

6.)Eclipse建议我创建一个在ID中单击的字段.它将按钮添加到R.java文件中,但没有帮助.尽管错误消失了,但是模拟器抛出了错误,提示应用程序已停止"

7.)我的manifest.xml文件如下



5.) But when i did so the R.id.click showed error saying "click cannot be resolved or not a field"

6.) Eclipse suggested me to create a field click in id which i did. It added the button into the R.java file but it did not help. Though the errors were gone but the emulator threw error saying "the application has stopped"

7.) My manifest.xml file is as below

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
            android:name=".test2"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.test.test2" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest> 




8.)现在我的 test2.java 代码是




8.) Now my test2.java code is

 package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import com.example.test.R;
public class test2 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test2);
		
               // some other code
		
	}
}



即使在 R.layout.test2 ,它也显示出与"test2无法解析或字段不存在"类似的错误.


我不明白我在想什么.请为我提供解决此问题的方法,在此先谢谢您.



Even here at R.layout.test2 it shows the similar error as "test2 cannot be resolved or not a field"


I''m not able to understand what am I missing. Please provide me a solution for this issue, Thanks in advance.

推荐答案

我认为点击不是按钮的好名字.另外,不要自己编辑R.java文件.只需在res\layout文件夹中打开相应的xml文件.然后打开gui编辑器,只需在组件"选项卡上单击一个按钮,然后将其放在布局上即可.给它一个好听的名字,例如button_test2.保存布局.

在OnCreate中,我们需要获取按钮参考,就像您已经尝试过的那样.当您开始输入r.id.时,按钮的ID将自动可用(如果没有,则您没有保存布局)

以下是您需要添加到OnCreate和新方法openTest2的一些代码.我将代码放在单独的方法中打开新活动,因为更容易掌握this.如果相反,如果您调用openTest2,则尝试直接打开活动,则该引用为View.OnClickListener的匿名实例,将不起作用.另外,我将活动命名为Test2Activity.我将在此示例代码下方对此进行详细说明.
I don''t think click is a very good name for a button. Also, don''t edit the R.java file yourself. Just open the corresponding xml file in the res\layout folder. Then open the gui editor and simply take a button from the component tab and put it on the layout. Give it a nice name like button_test2. Save the layout.

In the OnCreate we need to get the button reference, like you already tried. The id of the button will automatically be available when you start typing r.id. (if not, yo didn''t save the layout)

Below is some code you need to add to the OnCreate and a new method openTest2. I placed the code to open the new activity in a separate method because it is easier to get hold of this. If instead if calling openTest2 you would try to open the activity directly, this references to the anonymous instance of View.OnClickListener and won''t work. Also, I named the activity Test2Activity. I explain more on that below this example code.
btn = (Button)findViewById(R.id.button_test2);
btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          openTest2();
      }
    });


void openTest2() {
    Intent intent = new Intent(this, Test2Activity.class);
    startActivity(intent);
}



同样,您的活动名称定义android:name=".test2"通常会被命名为com.example.test.Test2Activity之类的名称(就像MainActivity定义的一样).

希望这会对您有所帮助.您也可以检查此链接以获取更多信息:
http://www.mkyong.com/android/android-button-example/ [ ^ ]

祝你好运!



Also, your activity name definition android:name=".test2" would normally be named something like com.example.test.Test2Activity (just like MainActivity defined avove that).

Hopefully this will help you out. You can also check this link for more info:
http://www.mkyong.com/android/android-button-example/[^]

Good luck!


这篇关于活动无法解析或不是字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆