【问题标题】:Eclipse, Add libraries in Project. I'm stuckEclipse,在项目中添加库。我被困住了
【发布时间】:2014-04-08 11:49:49
【问题描述】:

当我在 Eclipse 中的项目中添加库时,我被卡住了。我正在关注这个链接official android development websitehttp://developer.android.com/tools/support-library/setup.html#libs-with-res,我完全按照它所说的去做,但由于某种原因我得到了错误。它是如何发生的:

1.我从 SDK Manager 下载支持库。
2.我将它们与现有的 Android 代码一起导入工作区,然后按下两个 .jar 文件 Build Path>Add to Build Path。
3. 然后在那个项目上(在第 2 步中创建)我配置构建路径并检查两个 .jar 文件并取消选中 Android 依赖项,然后单击完成。目前一切正常。
但主要问题来了=>
4。我按下我的主项目(myfirstapp)属性,然后单击添加并选择库,然后我按下应用并出现许多错误,例如 R 无法解析以执行变量 和我的 R /gen 文件夹中的 .id 突然消失了

我现在添加一些屏幕截图,以使其更适合您。 抱歉,我没有发布图片的声誉。请复制以下链接

我所有的代码:MainActivity.xml

package com.example.myfirstapp;

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

public class MainActivity extends ActionBarActivity {
     public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    @Override
    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();
        }
    }
    public void sendMessage(View view) {
        Intent intent = new Intent(this, Displaymessageactivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

    @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;
        }
    }

}

fragment_main.xml

<LinearLayout 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:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />
</LinearLayout>  

字符串.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">myfirstapp</string>
    <string name="action_settings">Settings</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="title_activity_displaymessageactivity">Displaymessageactivity</string>
    <string name="hello_world">Hello world!</string>

</resources>  

显示消息活动.xml

package com.example.myfirstapp;

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

public class Displaymessageactivity extends ActionBarActivity {

    @Override

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Get the message from the intent
            Intent intent = getIntent();
            String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

            // Create the text view
            TextView textView = new TextView(this);
            textView.setTextSize(40);
            textView.setText(message);

            // Set the text view as the activity layout
            setContentView(textView);
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.displaymessageactivity, 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_displaymessageactivity, container, false);
            return rootView;
        }
    }

}  

fragment_displaymessageactivity.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.myfirstapp.Displaymessageactivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

到目前为止我尝试过的事情: 1.重新安装Eclipse和Java
2. 从 SDK Manager 重新下载库
3. 清理项目。
4.使我所有的xml文件都以小写开头(但MainActivity.xml和Displaymessageactivity.xml必须以大写开头)

我尽了最大的努力,我永远被困在这里。我对 Google 感到非常沮丧:/
非常感谢和欢迎每一个帮助!

【问题讨论】:

  • 在你的项目中添加一个应用程序项目后,有时你必须重新启动 ecplise..
  • 你的意思是在我从 SDK Manager 导入库之后对吗?
  • 检查顶部的导入,删除 R 导入,保存,将鼠标悬停在代码中的任何 R 上并再次导入。或者你可以检查你的导入,看看它们中是否有任何错误
  • @AngryAce no after you've added links to your project..
  • @AngryAce 在您的工作空间中,您还有 2 个项目 android.support.v7 如果您将其添加为库项目,您将收到错误消息。当您创建一个 android 项目时,android 支持的库会自动包含在 android 参考库中。在 Eclipse 中检查你的 java 构建路径。

标签: android eclipse add libraries


【解决方案1】:

将库添加到您的应用程序项目中:

  1. 在 Project Explorer 中,右键单击您的项目并选择 Properties。
  2. 在对话框左侧的类别面板中,选择 Android。
  3. 在“库”窗格中,单击“添加”按钮。

在第 3 步中,请注意,在“库”窗格中,您应该看到 appcompat_v7 已经添加(它是在您创建项目时按照 MyFirstApp 说明自动添加的,因为您已经选择了 minSdkVersion=8,而不是 11)(您的第二张图片)。

所以在添加库之前删除库窗格中所有添加的库(因此在您的第三张图像中,库窗格中只有 android-support-v7-appcompat),然后 R.java 不会消失。

【讨论】:

    猜你喜欢
    • 2019-04-30
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    相关资源
    最近更新 更多