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

问题描述

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

我正在使用完整的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.

推荐答案

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!


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

10-22 14:20