Android的 - 在启动时我的简单的ListView /吐司应用程序崩溃...任何想法?(And

2019-10-19 11:58发布

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

然而,这个简单的代码(该图是在Eclipse中没有任何错误)简单地拍击在模拟器启动。 有任何想法吗?

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

<?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了国内唯一的消息是致命的异常:主要的。

Answer 1:

更改此

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

<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();
    }
});
}


Answer 2:

这个:

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

是错误的,肯定的,因为你需要查找id与findViewById,因此,例如像这样,但你需要确保关于有与ID,然后在列表视图元素:

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


Answer 3:

举例来说,这是错误的:

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

它应该是这样的:

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

其中my_list_fruit只是机器人:在XML分配的ID标签:

<ListView
    android:id="@+id/my_list_fruit"
.../>


Answer 4:

试试这个,采用了android simple_list_view_1

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);
ListView listView = (ListView) findViewById(R.id.list_fruit);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, FRUITS);


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();
    }
});

}

}

或定义与它的前一个TextView的list_item.xml:

MainActivity.java

    public class MainActivity extends Activity {

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

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_fruit);
    listView = (ListView) findViewById(R.id.list_fruit);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.list_item, R.id.textView1, FRUITS);

    listView.setTextFilterEnabled(true);
    listView.setAdapter(adapter);

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

}}

list_fruit.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/list_fruit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" >

</ListView>

</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


文章来源: Android - My Simple ListView/Toast app crashes on startup… any ideas?