gpt4 book ai didi

java - ListView 崩溃 Activity

转载 作者:行者123 更新时间:2023-12-02 03:38:45 25 4
gpt4 key购买 nike

我想将从数据库检索到的一些数据添加到 ListView。

这是列表布局:list.xml

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="@string/id"
android:id="@+id/tlist1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="0dp"
android:layout_marginTop="32dp"
android:textAlignment="center"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="@string/Sujet"
android:id="@+id/tlist2"
android:layout_alignTop="@+id/tlist1"
android:layout_toRightOf="@+id/tlist1"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textAlignment="center"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="@string/texte"
android:textAlignment="center"
android:id="@+id/tlist3"
android:layout_alignTop="@+id/tlist2"
android:layout_toRightOf="@+id/tlist2"
android:layout_marginLeft="47dp"
android:layout_marginStart="47dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>

收到的包含ListView的布局:received.xml

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

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listtt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>

java文件:received.java

package com.example.oussama.setram;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
* Created by oussama on 09/03/2016.
*/
public class received extends Activity {
private ArrayList<msg> per=new ArrayList<msg>();
ListView listee;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.received);
msg msm = new msg();
msm.setSujet("aplication mobile");
msm.setTexte("hadj ben ali");
msm.setId(1);
per.add(msm);
MonAdapter adapter = new MonAdapter(getApplicationContext(), R.layout.list, per);
listee = (ListView) findViewById(R.id.listtt);
listee.setAdapter(adapter);
}




}

适配器:MonAdapter.java

package com.example.oussama.setram;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by oussama on 19/04/2016.
*/
public class MonAdapter extends ArrayAdapter<msg> {
Context context;
int idLayout;
ArrayList<msg> per;
LayoutInflater vi;
public MonAdapter(Context context, int idLayout, ArrayList<msg> per) {
super(context,idLayout,per);
this.context=context;
this.idLayout=idLayout;
this.per =per;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null){
vi = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(idLayout,parent,false);
}
msg c = per.get(position);


if(c!=null){
TextView sujet = (TextView) v.findViewById(R.id.tlist2);
TextView text = (TextView) v.findViewById(R.id.tlist3);
TextView id = (TextView)v.findViewById(R.id.tlist1);
if(sujet!=null){
sujet.setText(c.getSujet());}
if(text!=null){
text.setText(c.getTexte());}
if(id!=null){
id.setText(c.getId());
}
}
return v;
}
}

插入数据的类:msg.java

package com.example.oussama.setram;

/**
* Created by oussama on 30/04/2016.
*/
public class msg {

private int id;
private String sujet;
private String texte;
public msg(){
id=1;
sujet="";
texte="";

}
public msg(int id,String sujet,String texte) {
this.id = id;
this.sujet=sujet;
this.texte=texte;
}

public int getId(){
return id;
}
public String getSujet(){
return sujet;
}
public String getTexte(){
return texte;
}

public void setId(int id) {
this.id = id;
}

public void setTexte(String texte) {
this.texte = texte;
}

public void setSujet(String sujet) {
this.sujet = sujet;
}
}

堆栈跟踪是:

android.content.res.Resources$NotFoundException: String resource ID #0x1
com.example.oussama.setram E/AndroidRuntime: at android.content.res.Resources.getText(Resources.java:230)
com.example.oussama.setram E/AndroidRuntime: at android.widget.TextView.setText(TextView.java:3769)
com.example.oussama.setram E/AndroidRuntime: at com.example.oussama.setram.MonAdapter.getView(MonAdapter.java:44)
com.example.oussama.setram E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2159)
com.example.oussama.setram E/AndroidRuntime: at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
com.example.oussama.setram E/AndroidRuntime: at android.widget.ListView.onMeasure(ListView.java:1158)
com.example.oussama.setram E/AndroidRuntime: at android.view.View.measure(View.java:15518)
com.example.oussama.setram E/AndroidRuntime: at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
com.example.oussama.setram E/AndroidRuntime: at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
com.example.oussama.setram E/AndroidRuntime: at android.view.View.measure(View.java:15518)
com.example.oussama.setram E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
com.example.oussama.setram E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
com.example.oussama.setram E/AndroidRuntime: at android.view.View.measure(View.java:15518)
com.example.oussama.setram E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
com.example.oussama.setram E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
com.example.oussama.setram E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
com.example.oussama.setram E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
com.example.oussama.setram E/AndroidRuntime: at android.view.View.measure(View.java:15518)
com.example.oussama.setram E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
com.example.oussama.setram E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
com.example.oussama.setram E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
com.example.oussama.setram E/AndroidRuntime: at android.view.View.measure(View.java:15518)
com.example.oussama.setram E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
com.example.oussama.setram E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
com.example.oussama.setram E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
com.example.oussama.setram E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
com.example.oussama.setram E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
com.example.oussama.setram E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
com.example.oussama.setram E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:562)
com.example.oussama.setram E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:532)
com.example.oussama.setram E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
com.example.oussama.setram E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
com.example.oussama.setram E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 22:14:03.174 26668-26668/com.example.oussama.setram E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
05-08 22:14:03.174 26668-26668/com.example.oussama.setram E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
com.example.oussama.setram E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
com.example.oussama.setram E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
com.example.oussama.setram E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
com.example.oussama.setram E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
com.example.oussama.setram E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
/? I/Process: Sending signal. PID: 26668 SIG: 9

最佳答案

RelativeLayout 没有orientation 参数。并更改

id.setText(c.getId());

id.setText(String.valueOf(c.getId()));

关于java - ListView 崩溃 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37105604/

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