ListView在android中崩溃了应用程序

时间:2012-12-30 12:39:22

标签: android android-layout listview

我有一个包含4个不同按钮的主菜单。单击按钮时,应显示列表视图。最后,当您选择列表中的一个项目时,应该开始一个显示某些文本的新活动。当我点击Tiffins按钮时,应用程序关闭而不是显示列表视图。请帮忙。

这是我的代码。

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:background="@drawable/icon"
tools:context=".MainActivity" >

<Button
    android:id="@+id/curries"
    android:layout_width="150dp"
    android:layout_height="130dp"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/tiffins"
    android:drawableBottom="@drawable/curries"
    android:text="@string/Curries" 
    android:onClick="CurriesMenu"/>

<Button
    android:id="@+id/tiffins"
    style="@style/AppTheme"
    android:layout_width="150dp"
    android:layout_height="130dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="40dp"
    android:drawableBottom="@drawable/tiffins"
    android:text="@string/Tiffins" 
    android:onClick="TiffinsMenu"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="100dp"
    android:layout_height="30dp"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="19dp"
    android:text="@string/Tiffins"
    android:textSize="25dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="100dp"
    android:layout_height="30dp"
    android:layout_above="@+id/curries"
    android:layout_alignLeft="@+id/curries"
    android:layout_marginLeft="24dp"
    android:text="@string/Curries"
    android:textSize="25dp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="100dp"
    android:layout_height="30dp"
    android:layout_alignLeft="@+id/textView2"
    android:layout_centerVertical="true"
    android:text="@string/Snacks"
    android:textSize="25dp" />

<Button
    android:id="@+id/Snacks"
    android:layout_width="150dp"
    android:layout_height="130dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView3"
    android:drawableBottom="@drawable/snacks"
    android:text="@string/Snacks"
    android:onClick="SnacksMenu" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="100dp"
    android:layout_height="30dp"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignLeft="@+id/curries"
    android:layout_alignParentRight="true"
    android:text="@string/Pindivantalu"
    android:textSize="25dp" />

<Button
    android:id="@+id/pindivantalu"
    android:layout_width="150dp"
    android:layout_height="130dp"
    android:layout_alignBottom="@+id/Snacks"
    android:layout_alignParentRight="true"
    android:drawableBottom="@drawable/pindivantalu"
    android:text="@string/Pindivantalu"
    android:onClick="PindivantaluMenu" />

activity_display_tiffinsmenu.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"
tools:context=".DisplayTiffinsmenu" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@drawable/tiffins1"
    android:entries="@array/tiffinslist" >

</ListView>

MainActivity.java

  package com.example.andhrarecipies;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void TiffinsMenu(View view){
        Intent intent = new Intent(this, DisplayTiffinsmenu.class);
        startActivity(intent);
    }

    public void CurriesMenu(View view){
        Intent intent = new Intent(this, DisplayCurriesmenu.class);
        startActivity(intent);
    }

    public void SnacksMenu(View view){
        Intent intent = new Intent(this, DisplaySnacksmenu.class);
        startActivity(intent);
    }

    public void PindivantaluMenu(View view){
        Intent intent = new Intent(this, DisplayPindivantalumenu.class);
        startActivity(intent);

    }

}

DisplayTiffinsmenu.java

package com.example.andhrarecipies;

import java.util.ArrayList;

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

public class DisplayTiffinsmenu extends ListActivity {

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

        ListView TiffinsMenu = (ListView) this.findViewById(R.id.listView1);
        String[] menu = getResources().getStringArray(R.array.tiffinslist);

        setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_display_tiffinsmenu, menu));

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
            Toast.makeText(this, "you have selected" + keyword, 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.activity_display_tiffinsmenu, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:1)

使用ListActivity时,您的activity_display_tiffinsmenu.xml - 布局必须包含ListViewandroid:id属性设置为@android:id/list,如下所示:

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