gpt4 book ai didi

android - ListView 在 android 中使应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 03:51:26 25 4
gpt4 key购买 nike

我有一个包含 4 个不同按钮的主菜单。当您单击一个按钮时,应该会出现一个 ListView 。最后,当您选择列表中的项目之一时,应启动一个显示一些文本的新 Activity 。当我点击 Tiffins 按钮时,应用程序关闭而不是显示 ListView 。请帮忙。

这是我的代码。

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>

主 Activity .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);

}

}

显示Tiffinsmenu.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;
}

}

最佳答案

使用 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>

关于android - ListView 在 android 中使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14090768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com