Android的 - 在启动时我的简单的ListView /吐司应用程序崩溃...任何想法? [英] Android - My Simple ListView/Toast app crashes on startup... any ideas?

查看:254
本文介绍了Android的 - 在启动时我的简单的ListView /吐司应用程序崩溃...任何想法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的ListView的应用程序,以了解更多关于编程的机器人。

I am creating a simple ListView app to learn more about programming for android.

然而,这个简单的code(这是显示在Eclipse中没有任何错误)仅仅是在崩溃在模拟器启动。任何想法?

However, this simple code (which is showing no errors whatsoever in Eclipse) is simply crashing on startup in the emulator. Any ideas?

public class MainActivity extends Activity {

static final String[] FRUITS = { "Apple", "Avocado", "Banana",
    "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
    "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_fruit);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_fruit, FRUITS);

    ListView listView = (ListView) findViewById(R.layout.list_fruit);
    listView.setTextFilterEnabled(true);
    listView.setAdapter(adapter);



    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });

}

}

list_fruit.xml

list_fruit.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:padding="10dp"
     android:textSize="20sp" >
</TextView>

试图找到下您的指示堆栈跟踪,即AME了唯一的消息是致命的异常:主要的。

Tried to find the stacktrace following your instructions, the only message that ame up was FATAL EXCEPTION: main.

推荐答案

更改此

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_fruit, FRUITS);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, FRUITS);

setContentView(R.layout.list_fruit); // list_fruit.xml

和您使用相同的 ArrayAdapter

同样改变

ListView listView = (ListView) findViewById(R.layout.list_fruit);

ListView listView = (ListView) findViewById(R.id.listview);

也改为

Toast.makeText(getApplicationContext(),FRUITS[postion], Toast.LENGTH_SHORT).show();

编辑: list_fruit.xml 没有一个ListView

list_fruit.xml does not have a ListView.

您需要下面的 list_fruit.xml

<ListView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> 

然后在的onCreate

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_fruit);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, FRUITS);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setTextFilterEnabled(true);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          Toast.makeText(getApplicationContext(),FRUITS[postion], Toast.LENGTH_SHORT).show();
    }
});
}

这篇关于Android的 - 在启动时我的简单的ListView /吐司应用程序崩溃...任何想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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