首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来源Not Found main.xml开始开发Android4应用程序

来源Not Found main.xml开始开发Android4应用程序
EN

Stack Overflow用户
提问于 2013-04-24 07:33:28
回答 2查看 182关注 0票数 0

我开始开发Android4应用程序,事实上,我正在阅读一本教我android的书。我是第四章,似乎遇到了一个让我发疯的问题。我正在制作一个简单的android应用程序,在复选框列表中显示总统,当您单击一个按钮时,它将显示您选择的内容。但它没有给我机会,因为当我在模拟器上打开应用程序时,它会给我一个错误"Source not Found“。这是我的应用程序代码。

代码语言:javascript
复制
package net.learn2develop.Basicviews5;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class BasicViews5Activity extends ListActivity {

    String[] presidents;

/** called when there activity is first created */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    ListView lstView = getListView();
    // lstView.setChoiceMode(ListView.CHOICE_MODE_NONE);
    //lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lstView.setTextFilterEnabled(true);

    presidents = 
            getResources().getStringArray(R.array.presidents_array);

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_checked, presidents));
}

public void onListItemClick(
        ListView parent, View v, int position, long id)
{
    Toast.makeText(this,
            "You have selceted " + presidents[position],
            Toast.LENGTH_SHORT).show();
}

public void onClick(View view) {
    ListView lstView = getListView();

    String itemsSelected = "Selcted items: \n";
    for (int i=0; i<lstView.getCount(); i++) {
        if (lstView.isItemChecked(i)) {
            itemsSelected += lstView.getItemAtPosition(i) + "\n";
        }
    }
    Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.basic_views5, menu);
    return true;
}

}

下面是我调用的main.xml文件的代码。(或者无论如何都要试着这样做。)

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
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=".BasicViews5Activity" >

<Button 
    android:id="@+id/btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Show selceted items"
    android:onClick="onClick" />

<TextView
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

下面是我的strings.xml文件,它调用要放入列表中的总统字符串。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>

代码语言:javascript
复制
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="app_name">BasicViews5</string>
<string-array name="presidents_array">
    <item>Dwight D. Eisenhower</item>
    <item>John F. Kennedy</item>
    <item>Lyndon B. Johnson</item>
    <item>Richard Nixon</item>
    <item>Gerald Ford</item>
    <item>Jimmy Carter</item>
    <item>Ronald Reagen</item>
    <item>George H. W. Bush</item>
    <item>Bill Clinton</item>
    <item>George W. Bush</item>
    <item>Barack Obama</item>
</string-array>

</resources>

我真的不知道这是怎么回事。我做了一些调查,我缩小了问题的范围,似乎只有一行代码。

代码语言:javascript
复制
setContentView(R.layout.main);

我把它拿出来,它很好,但我没有显示你所选内容的按钮。我把它放进去,它变得狂暴起来,说没有找到源,模拟器是空的。我知道我的帖子很长,但非常感谢您抽出时间来阅读它!如果您能给我任何帮助,我们将不胜感激!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-24 08:25:30

将评论转换为答案:

您正在调用getListView(),但是给了应用程序正在查找的android:id="@+id/android:list"TextView一个id。只有在没有在xml中声明的情况下才调用getListVivew(),那么它将默认创建ListView

更改此行

ListView lstView = getListView();

代码语言:javascript
复制
ListView lstView = (ListView) findViewById(R.id.listView);

然后将xml中的TextView更改为ListView

现在,它正在寻找具有适当idListView。如果您想要更改外观,还可以使用stylesthemes进一步格式化ListView

票数 1
EN

Stack Overflow用户

发布于 2013-04-24 08:16:04

在本书的第192页(第4章),在onCreate方法中有以下代码片段:

代码语言:javascript
复制
//---no need to call this---
//setContenetView(R.layout.main);

这是因为您正在使用ListActivity,这将使内容视图成为一个ListView。如果您希望定义一个自定义布局并定义其他视图:使您的类仅扩展Activity。然后,在您的main.xml文件中,您可以放入如下内容:

代码语言:javascript
复制
<ListView
    android:id="@+id/list"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

现在,在您的onCreate方法中,您可以调用:setContentView(R.layout.main)并在下面引用列表视图:

代码语言:javascript
复制
ListView list = (ListView) findViewById(R.id.list);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, presidents));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16180921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档