我有一個主菜單,其中包含4個不同的按鈕。當你點擊一個按鈕時,應該出現一個列表視圖。最後,當你在列表中選擇一個項目時,應該開始一個顯示一些文本的新活動。當我點擊Tiffins按鈕時,應用程序關閉而不是顯示列表視圖。請幫忙。ListView在Android中崩潰的應用程序
這是我的代碼。
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;
}
}
請從後拋出的異常堆棧跟蹤。 – Darwind
編輯帖子並粘貼堆棧跟蹤...我猜錯誤是由於沒有提到manifest中的活動造成的 – BLOB