setContentView在listview中崩溃Android应用程序

Cla*_*tor 2 android

好吧,由于setContentView行,我有一个应用程序在加载时崩溃.现在在你问之前,当我在第15行注释掉setContentView行时,我得到了一个有用的应用程序.但是当我放入它的那一刻,它就错了.现在这是使用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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Vik*_*dar 8

如果您Activity正在扩展ListActivity该类,请确保您的主布局文件必须包含ListView下面给出的元素.

<ListView android:id="@android:id/list" android:layout_height="wrap_content"
android:layout_width="match_parent" />
Run Code Online (Sandbox Code Playgroud)

您的应用程序正在崩溃,因为您正在为ListView设置适配器,这在您的布局文件中确实不存在.

请参阅文档:http://developer.android.com/reference/android/app/ListActivity.html