【问题标题】:Listview is crashing my appListview 正在使我的应用程序崩溃
【发布时间】:2017-11-01 12:08:39
【问题描述】:

很高兴成为 Stack Overflow 的一员。我正在尝试为 Android 编写一个应用程序;我的问题是列表视图。每次我尝试运行应用程序时它都会崩溃。当我将 listview 代码注释掉时,应用程序不会崩溃。我已经尝试了许多版本的代码。下面是我能写的最小的列表视图代码,它仍然崩溃。也许有人知道为什么。 谢谢 杰克·弗莱克

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="john.wynn.gulmayinc.com.viewstuff">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest> 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="john.wynn.gulmayinc.com.viewstuff.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0.837" />

<ListView
    android:id="@+id/listofstuff"
    android:layout_width="138dp"
    android:layout_height="495dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

import android.app.Activity;
import android.app.ListActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.util.*;
import java.util.ArrayList;

public class MainActivity extends Activity {

    private  final String TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    setContentView(android.R.layout.activity_list_item);

    final ListView listview = (ListView) findViewById(R.id.listofstuff);

    String[] values = new String[] {"Georgia", "Idaho","New York","Texas"};

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, 
         R.layout.activity_main, R.id.listofstuff, values);

    listview.setAdapter(arrayAdapter);

    listview.setOnItemClickListener(myList);
}

private AdapterView.OnItemClickListener myList = new 

AdapterView.OnItemClickListener(){
    public void onItemClick(AdapterView<?> adapterView, View view, int i, 
    long l) {
    Toast.makeText(getApplicationContext(),
                    "Click ListItem Number " + i +" "+ l, Toast.LENGTH_LONG)
                    .show();
    }


};


}

【问题讨论】:

  • 检查设置为您的活动的 xml 以及 ArrayAdapter 的参数
  • 显示你的 logcat
  • 检查下面的答案。

标签: android listview crash


【解决方案1】:

改变

setContentView(android.R.layout.activity_list_item);

setContentView(R.layout.activity_main);

还有变化

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, 
     R.layout.activity_main, R.id.listofstuff, values);

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, android.R.id.text1, values); 

它的工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多