gpt4 book ai didi

android - ListView 使我的应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 04:46:29 25 4
gpt4 key购买 nike

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

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


};


}

最佳答案

改变

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

开始工作了

关于android - ListView 使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47054468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com