【问题标题】:<Activity name> cannot be resolved or not a field<活动名称> 无法解析或不是字段
【发布时间】:2014-04-15 12:16:13
【问题描述】:

我正在尝试使用 Intent 执行简单的导航操作。

我使用的是完整的 ADT 包,其中包括 eclipse、SDK 和 ADT 插件,因此无需在 eclipse 中单独配置 ADT。

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

2.) 对应的java文件是mainActivity.javatest2.java

3.) 现在 activity_main.xml 包含一个带有 id = "click" 的按钮。单击此按钮应导航到下一个活动,即 test2.xml

4.) mainActivity.java 中的代码如下

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends ActionBarActivity 
{

    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

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

        btn = (Button)findViewById(R.id.click);

        //Listening to button event
        btn.setOnClickListener(new View.OnClickListener() 
        {

            public void onClick(View arg0) {
            //Starting a new Intent
            Intent nextScreen = new Intent(getApplicationContext(), test2.class);

            startActivity(nextScreen);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

}

5.) 但是当我这样做时,findViewById(R.id.click) 显示错误提示“点击无法解析或不是字段”

6.) Eclipse 建议我创建一个在 id 中单击的字段,我这样做了。它修改了 R.java 文件,但没有帮助。虽然错误消失了,但模拟器抛出错误说“应用程序已停止”

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

<?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 代码是

package com.example.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

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);

     TextView txtName = (TextView) findViewById(R.id.textView1);

        txtName.setText("This is test2 activity");

}
}

即使在 setContentView(R.layout.test2) 处,它也会显示与“test2 无法解析或不是字段”类似的错误,其中 setContentView(R.layout.activity_main) 没有显示这个错误

9.) 我的 fragment_main.xml 代码如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity$PlaceholderFragment" >


<Button
    android:id="@+id/click"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_centerInParent="true"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="65dp"
    android:text="@string/button" />

</RelativeLayout>

10.) 我的 test2.xml 代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="second page" 
        android:layout_gravity="center"
        />

</LinearLayout>

运行应用程序时显示的错误如下

a.) [2014-04-15 18:39:03 - test] W/ResourceType(8160):ResXMLTree_node 标头大小 0 太小。

b.) [2014-04-15 18:39:03 - 测试] C:\Users\KC\Desktop\Android-budle\test\res\menu\main.xml:6: 错误:错误:找不到与给定名称匹配的资源(在“标题”处,值为“@string/action_settings”)。

我无法理解我错过了什么。请为我提供此问题的解决方案,在此先感谢。

【问题讨论】:

  • R.id.click 在您的activity_main.xml 文件中吗?
  • 你的按钮点击监听器在哪里?您是否尝试过清理您的项目?
  • 我评论了那部分,但不好。报错还是一样,而且eclipse默认还创建了fragment_main.xml,所以要使用main_activity.xml需要用到部分代码
  • 我有按钮监听器,但在这里它无法通过 id 找到按钮,并在我输入 findViewByID(R.id.click) 和 setContentView(R.layout.test2) 的那一刻抛出错误
  • 发布activity_main.xml。如果按钮不在那里,则会引发此错误。

标签: android


【解决方案1】:

首先检查您的布局 xml。我有任何错误然后更正它。之后press alt+p然后选择clean之后它将再次构建。

【讨论】:

    【解决方案2】:

    在你的activity_main.xml>按钮中你需要放置android:onClick="methodName"。在您的 main_activity 中,您应该实现 methodName。单击按钮后,您可以在那里处理活动。

    另一种方法是实现 onClickListener 来分离 XML 和 Java 文件。

    你能添加你的布局 XML 文件吗?

    【讨论】:

    • 这是不好的做法。不要这样做。您应该通过在 Activity 中添加一个 onClick 侦听器来使 java 和 xml 尽可能分开,而不是直接引用按钮的 xml 标记中的方法。
    • 对了,你也可以在你的Activity中实现onClicklistener,不用XML就用Java全部处理
    【解决方案3】:

    MainActivityonCreate() 方法中为您的按钮编写onClickListener,并在其中重定向到test2 活动,如下所示:

    btn = (Button)findViewById(R.id.click);
    
    btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,test2.class);
                               startActivity(intent);
                }
            });
    

    已编辑:

    除了activity_main,您还需要将视图设置为fragment_main.xml,因为您的Buttonfragment_main 布局内。

    改一下

     setContentView(R.layout.activity_main);
    

     setContentView(R.layout.fragment_main);
    

    【讨论】:

    • 它甚至没有在 R.id 中找到 click 所以这无济于事。
    • 我什至尝试过这样做。但它在第一步显示错误,即 btn = (Button) findViewById(R.id.click)
    • 感谢您对此进行更新。我做到了,但 R.layout.test2 和 R.id.click 的错误仍然存​​在
    • 请在您的问题中发布您的错误、您的 test2 布局代码和您的 btn 代码。 @user2470766
    • 我已经修改了第4、9和10点,还添加了运行应用程序后抛出的错误...
    【解决方案4】:

    您的xml files (test2 or activity_main) 存在语法错误,因此自动生成的 R 文件将不再为按钮视图生成 id。

    主要问题似乎在于activity_main,因为其中包含的按钮不再转换为其在 R 文件中的相应值。

    检查这些文件,如果无法弄清楚,请输入代码以获得帮助。

    最后但并非最不重要的一点是检查您是否有类似import android.R 的导入语句。 您不需要它存在,因为它会导致此类错误(如果存在)。

    更新:

    你正在使用 -

    1) setContentView(R.layout.activity_main);

    2) btn = (Button)findViewById(R.id.click);

    但是您定义按钮的布局是setContentView(R.layout.fragment_main);

    所以 btn 正在尝试在 activity_main 中查找不可用的视图。

    解决方案 -

    将内容视图设置为setContentView(R.layout.fragment_main)

    【讨论】:

    • 正确。文件中应该有一些语法错误。
    • 实际上是我正在创建按钮的 fragment_main.xml。当我创建新应用程序时,Eclipse 自动创建了它。我将粘贴那部分 xml 代码
    • 我已将 fragment_main.xml 代码添加为第 9 点。请检查一下
    • 那你为什么用setContentView(R.layout.activity_main)?你应该使用setContentView(R.layout.fragment_main)。这样做并检查。
    • 问题是您在 MainActivity (R.layout.activity_main) 中执行此操作,但您的活动是 MainFragment (R.layout.fragment_main)。
    猜你喜欢
    • 2022-01-09
    • 2014-03-27
    • 2014-01-05
    • 2016-05-20
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多