崩溃的setContentView列表视图中的Andr​​oid应用程序 [英] setContentView crashes Android Application in listview

查看:199
本文介绍了崩溃的setContentView列表视图中的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经得到了,因为一的setContentView线的负荷可达崩溃的应用程序。
现在你问之前,其时我注释掉第15行左右的setContentView行,我得到一个工作程序。但是现在我把它放在,它的错误。现在,这是用2.1 UPDATE1机器人。

Java的:

 包com.clark.listview;进口android.app.ListActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.Toast;公共类列表视图扩展ListActivity {
    / **当第一次创建活动调用。 * /
    公共无效的onCreate(捆绑冰柱){
        super.onCreate(冰柱);
        //setContentView(R.layout.main);
        //创建一个字符串数组,将被投入到我们的ListActivity
        的String [] =名新的String [] {Linux的,Windows7的,日蚀,粟色
                Ubuntu的的Solaris,机器人,iPhone,ClarkPhone};
        //使用自己的布局和适配器指向UI元素这
        //包含标签
        this.setListAdapter(新ArrayAdapter<串GT;(这一点,R.layout.rowlayout,
                R.id.label,名));
    }    @覆盖
    保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
        super.onListItemClick(L,V,位置ID);
        //获取被点击的项目
        对象o = this.getListAdapter()的getItem(位置)。
        字符串关键字= o.toString();
        Toast.makeText(这一点,你已经选择:+关键字,Toast.LENGTH_LONG)
                。显示();
        如果(keyword.startsWith(Windows7的)|| keyword.startsWith(iPhone)
                || keyword.startsWith(中的Solaris)){
            launchTest();
        }
    }
    保护无效launchTest(){
        意图I =新意图(这一点,home.class);
        startActivity(ⅰ);
    }}

XML:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout机器人:ID =@ + ID /主
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:cacheColorHint =#00000000
    机器人:背景=@绘制/ BG>
< / LinearLayout中>


解决方案

如果你的活动是扩展 ListActivity 那么类确保您的主要布局文件必须包含一个的ListView 元素如下。

 < ListView的机器人:ID =@机器人:ID /列表机器人:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent/>

您的应用程序崩溃,因为你是一个设置适配器的ListView这确实是不是在你的布局文件存在。

查看文档:<一href=\"http://developer.android.com/reference/android/app/ListActivity.html\">http://developer.android.com/reference/android/app/ListActivity.html

Well, I've got an application that crashes on load up because of a setContentView line. Now before you ask, its when I comment out the setContentView line on line 15 or so that I get a working app. But the moment I place it in, it errors. Now this is using 2.1-update1 android.

Java:

package com.clark.listview;

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

public class listview extends ListActivity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        //setContentView(R.layout.main);
        // Create an array of Strings, that will be put to our ListActivity
        String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
                "Ubuntu", "Solaris", "Android", "iPhone", "ClarkPhone" };
        // Use your own layout and point the adapter to the UI elements which
        // contains the label
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout,
                R.id.label, names));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
                .show();
        if (keyword.startsWith("Windows7") || keyword.startsWith("iPhone")
                || keyword.startsWith("Solaris")) {
            launchTest();
        }


    }
    protected void launchTest() {
        Intent i = new Intent(this, home.class);
        startActivity(i);
    }

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:cacheColorHint="#00000000"
    android:background="@drawable/bg">
</LinearLayout>

解决方案

If your Activity is extending the ListActivity class then ensure that your main layout file must contain a ListView element as given below.

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

your application is crashing because you are setting an adapter to ListView which is indeed not exist in your layout file.

See the documentation : http://developer.android.com/reference/android/app/ListActivity.html

这篇关于崩溃的setContentView列表视图中的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆