5

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

2 回答 2

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>
于 2013-01-08T13:42:27.650 回答
0

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

于 2013-08-19T18:14:31.410 回答