Listview正在崩溃我的应用程序

时间:2017-11-01 12:08:40

标签: android listview crash

很高兴成为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();
    }


};


}

1 个答案:

答案 0 :(得分:2)

chanage

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

这是工作