Eclipse,在Project中添加库。我被卡住了

时间:2014-04-08 11:49:49

标签: android eclipse add libraries

我在Eclipse中的项目中添加库时遇到困难。我正在关注此链接official android development website http://developer.android.com/tools/support-library/setup.html#libs-with-res,我完全按照其说法行事,但出于某种原因我收到了错误。这是怎么回事:

1.我从SDK Manager下载支持库 2.我将现有Android代码导入工作区,然后按两个.jar文件构建路径>添加到构建路径。
3.然后在该项目(在步骤2中制作)中,我配置Build Path并检查两个.jar文件并取消选中Android Dependencies,然后单击“完成”。现在一切都还可以。
但这里出现了主要问题=>
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>  

strings.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>  

Displaymessageactivity.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必须以大写字母开头)

我尽我所能,我永远被困在这里。我对谷歌非常沮丧:/
非常感谢和欢迎每一个帮助!

1 个答案:

答案 0 :(得分:0)

  

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

     
      
  1. 在项目浏览器中,右键单击项目并选择“属性”。
  2.   
  3. 在对话框左侧的类别面板中,选择Android。
  4.   
  5. 在“库”窗格中,单击“添加”按钮。
  6.   

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

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