【问题标题】:Android Studio Create Action BarAndroid Studio 创建操作栏
【发布时间】:2015-04-19 04:19:26
【问题描述】:

我对 Android 开发完全陌生,这是我接触 Android 工作室的第一天。只是一个愚蠢的问题:我正在实现一个笔记应用程序,在我的主视图活动中我必须这样做

public class MainActivity extends ListActivity {}

但原来的代码是

public class MainActivity extends ActionBarActivity {}

MainActivity.java

 package com.example.sunny.mynote;

import android.app.ListActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

import com.example.sunny.mynote.com.example.sunny.mynote.data.NoteDataSource;
import com.example.sunny.mynote.com.example.sunny.mynote.data.NoteItem;

import java.util.List;


public class MainActivity extends ActionBarActivity {

    private NoteDataSource datasource;
    List<NoteItem> notesList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        datasource = new NoteDataSource(this);

        refreshDisplay();
    }

    private void refreshDisplay() {
        notesList = datasource.findAll();
        ArrayAdapter<NoteItem> adapter =
                new ArrayAdapter<NoteItem>(this, R.layout.list_item_layout, notesList);
        ListView lv = (ListView) findViewById(R.id.list);
        lv.setListAdapter(adapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.mina.mynote.MainActivity">

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

</RelativeLayout>

但是这使我的操作栏消失了,我知道我不能将 MainActivity 继承到 ActionBarActivity 因为 Java 不支持 2 继承,所以我怎样才能让我的操作栏出现?请帮忙!!

【问题讨论】:

  • 你能发布你的活动布局xml文件吗?
  • 好的,你也可以从你的 res 文件夹中发布 activity_main.xml 吗?
  • @HarperMina 创建一个扩展 ListActivity 的子类或内部类。这将解决您的问题。保持 MainActivity 原样

标签: java android inheritance android-studio android-actionbar


【解决方案1】:

您需要做的是在 ActionBarActivity 中放置一个列表视图。将public class MainActivity extends ListActivity 改回public class MainActivity extends ActionBarActivity。然后您需要更改布局,以便列表视图有一个容器。您可以使用相对布局:

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.mina.mynote.MainActivity">

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" />

</RelativeLayout>

然后,您需要更改此调用,在其中设置适配器:setListAdapter(adapter)。您的活动不再从 ListActivity 继承,因此它没有此方法。您需要先获取对您的列表视图的引用,然后在其上调用setListAdapter()

ListView lv = (ListView) findViewById(R.id.list);
//setListAdapter becomes setAdapter
lv.setAdapter(adapter);

顺便说一句,考虑为您的列表视图 ID 使用比“列表”更通用/不那么模糊的东西。

我希望这会有所帮助。

【讨论】:

  • 请检查我的最新更新。我遵循了您的建议,但现在出现错误无法解析 ListView 和列表的符号,错误无法解析 setListAdapter 的方法。
  • 哦——你需要import android.widget.ListView; Android studio 通常会提示你这样做以解决那些未解决的符号错误...
  • 您可能还需要将xml中的ListView id改为@+id/list
  • 我仍然收到错误无法解析 setListAdapter 的方法。不知道为什么
  • 哎呀——我刚刚仔细检查了——应该是setAdapter(),而不是setListAdapter()。对不起!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 2021-02-26
  • 2016-07-20
  • 2015-04-27
  • 1970-01-01
相关资源
最近更新 更多