Ant编译错误 - NavUtils

时间:2012-12-28 05:40:19

标签: android ant

Ant似乎找不到包:android.support.v4.app.NavUtils

以下是编译器错误:

C:\Android\MyFirstApp\src\com\example\myfirstapp\DisplayMessageActivity.java:7: error: package  android.support.v4.app does not exist

C:\Android\MyFirstApp\src\com\example\myfirstapp\DisplayMessageActivity.java:22: error: cannot find symbol: NavUtils.navigateUpFromSameTask(this);

我在sdk中有一个带有类文件的android目录。

MainActivity.java

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.content.Intent;
import android.widget.TextView;
import android.widget.EditText;
import android.view.MenuItem;

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
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);
}
}

DisplayMessageActivity.java

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.NavUtils;

public class DisplayMessageActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
return super.onOptionsItemSelected(item);
}
}

2 个答案:

答案 0 :(得分:10)

Coolcfan(几乎)是正确的。 默认情况下不包含android.support.v4.NavUtils。

无论如何,您需要将库添加到构建路径中。它位于:

android-sdk\extras\android\support\v4\android-support-v4.jar

一旦你完成了,它应该都可以正常工作。

注意:还要确保您使用元数据标记在Manifest中定义了父类:

<activity
    android:name=".ChildActivity"
    android:label="@string/childActivity_title" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".ParentActivity" >
    </meta-data>
</activity>

答案 1 :(得分:0)

简单...搜索android-support-v4.jar文件。它必须存在于android-sdk \ extras \ android \ support \ v4文件夹中... 然后复制该jar文件并将其粘贴到应用程序的lib目录中。 然后编译! 希望有所帮助!!!