Android教程 - 添加操作栏,重复类?

时间:2015-08-11 23:09:39

标签: android

我正在尝试按照Android Studio教程创建Action Bar。我熟悉VBA,C ++和Python,但这是我第一次冒险进入一个使用java,xml等的App。我之所以这么说,是因为我认为我的代码中存在错误(或错误命名)的内容,因为当我尝试运行此代码时,我收到错误error: duplicate class: com.example.batman.myfirstapp.MyActivity

我不明白它是如何复制的,我只有一个“MyActivity.java”,所以我认为每个.java文件不能只有一个public class MyActivity extends ...

这就是我所拥有的(如果我需要包含其他代码,请告诉我):

MyActivity.java

package com.example.batman.myfirstapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.support.v7.app.*;

public class MyActivity extends ActionBarActivity{
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
//              openSearch();
                return true;
            case R.id.action_settings:
//              openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

public class MyActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = ".com.batman.myfirstapp.MESSAGE";

    /**
     * Called when the user clicks the Send button
     */
    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 void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

//         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // If your minSdkVersion is 11 or higher, instead use:
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myfirstapp="http://schemas.android.com/apk/res-auto">
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          myfirstapp:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          myfirstapp:showAsAction="never" />
</menu>

同样,我不确定其他文件(strings.xml,DisplayMessageActivity.java等)对调试上述内容有什么帮助,所以让我知道我还能包含什么。

感谢您的任何建议/帮助!

2 个答案:

答案 0 :(得分:2)

你的直觉是正确的!您的文件中只能有一个MyActivity类,这里的内容可能是:

public class MyActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = ".com.batman.myfirstapp.MESSAGE";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                return true;
            case R.id.action_settings:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

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

答案 1 :(得分:1)

你的假设是正确的。在.java文件中,您有两个名为MyActivity的类。你只能有一个。