gpt4 book ai didi

java - 我的程序在 ListView 中第二次单击时崩溃

转载 作者:行者123 更新时间:2023-11-29 21:50:44 25 4
gpt4 key购买 nike

已解决 ;)我知道这是个好主意,但我使用了 Globals .class,现在它可以工作了;)现在可以了 ;) hurahhh

谢谢大家

主要问题

当我第二次单击“ListView”项目时发生错误并且应用程序关闭。

谁能告诉我我做错了什么,因为它第一次完美地与每个元素一起工作,而第二次它不工作,即使是第一次点击(没有任何问题)的 ListView 项目再次点击。

    package com.klemenjezakon.koceSLO;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class KocaInter extends ListActivity {

StringBuilder sb = new StringBuilder();
String temp;
InputStreamReader reader;
int n = 2;
koca[] koce = new koca[n];
ArrayList<String> kocee = new ArrayList<String>();
ArrayList<String> podatek = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, kocee));

inicjalizacijaKoc();

}

private void inicjalizacijaKoc() {

try {
reader = new InputStreamReader(getAssets().open("s_koce.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

BufferedReader br = new BufferedReader(reader);
for (int i = 0; i < 2; i++) {
try {
kocee.add("" + br.readLine());

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// TODO Auto-generated method stub


}

protected void onListItemClick(ListView lv, View v, int position, long id) {
super.onListItemClick(lv, v, position, id);


int a = 0;
switch (position) {
case 0:
a=R.drawable.slika0000;
break;
case 1:
a=R.drawable.slika0001;
break;
default:
a=R.drawable.slika0000;

}

try {
reader = new InputStreamReader(getAssets().open("koce_podatki.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

BufferedReader br = new BufferedReader(reader);
for(int i=-1;i<position;i++){
try {
temp = "" + br.readLine();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}

try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String[] nov = temp.split("\\|");

for(int i=0;i<23;i++){
podatek.add(""+nov[i]);
}
temp=""+sb.append(a);

podatek.set(0,temp);

Intent in = new Intent(this, MainActivity.class);

in.putExtra("array", podatek);

startActivity(in);

podatek.clear();

}

}

主要 Activity :

package com.klemenjezakon.koceSLO;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import android.widget.TextView;

public class MainActivity extends Activity {

ImageView slika;
ArrayList<String> podatki;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

Bundle extras = getIntent().getExtras();
if (extras != null) {
podatki = extras.getStringArrayList("array");
}

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int height = metrics.heightPixels;
int width = metrics.widthPixels;

setContentView(R.layout.koca);

int n=-1;

int pic=++n;

TextView ime = (TextView) findViewById(R.id.ime);
ime.setText("" + (podatki.get(++n).toString()));
TextView visina = (TextView) findViewById(R.id.visina);
visina.setText("" + (podatki.get(++n).toString()));
TextView odprt = (TextView) findViewById(R.id.odprt);
odprt.setText("" + (podatki.get(++n).toString()));
TextView gorPredel = (TextView) findViewById(R.id.gorPredel);
gorPredel.setText("" + (podatki.get(++n).toString()));
TextView PD = (TextView) findViewById(R.id.PD);
PD.setText("" + (podatki.get(++n).toString()));
TextView Oskrbnik = (TextView) findViewById(R.id.Oskrbnik);
Oskrbnik.setText("" + (podatki.get(++n).toString()));
TextView tel = (TextView) findViewById(R.id.Tel);
tel.setText("" + (podatki.get(++n).toString()));
TextView GSM = (TextView) findViewById(R.id.GSM);
GSM.setText("" + (podatki.get(++n).toString()));
TextView telPD = (TextView) findViewById(R.id.TelPD);
telPD.setText("" + (podatki.get(++n).toString()));
TextView mail = (TextView) findViewById(R.id.mail);
mail.setText("" + (podatki.get(++n).toString()));
TextView web = (TextView) findViewById(R.id.web);
web.setText("" + (podatki.get(++n).toString()));
TextView naslov = (TextView) findViewById(R.id.naslov);
naslov.setText("" + (podatki.get(++n).toString()));
TextView kat = (TextView) findViewById(R.id.kat);
kat.setText("" + (podatki.get(++n).toString()));
TextView lez = (TextView) findViewById(R.id.lez);
lez.setText("" + (podatki.get(++n).toString()));
TextView stJed = (TextView) findViewById(R.id.stJed);
stJed.setText("" + (podatki.get(++n).toString()));
TextView cen = (TextView) findViewById(R.id.cen);
cen.setText("" + (podatki.get(++n).toString()));
TextView dostop = (TextView) findViewById(R.id.dostop);
dostop.setText("" + (podatki.get(++n).toString()));
TextView opis = (TextView) findViewById(R.id.opis);
opis.setText("" + (podatki.get(++n).toString()));
TextView razg = (TextView) findViewById(R.id.razg);
razg.setText("" + (podatki.get(++n).toString()));
TextView pTure = (TextView) findViewById(R.id.pTure);
pTure.setText("" + (podatki.get(++n).toString()));
TextView bKoce = (TextView) findViewById(R.id.bKoce);
bKoce.setText("" + (podatki.get(++n).toString()));
TextView vzponi = (TextView) findViewById(R.id.vzponi);
vzponi.setText("" + (podatki.get(++n).toString()));

Bitmap foto = BitmapFactory.decodeResource(getResources(),
Integer.parseInt(podatki.get(pic)));
ImageView slika = (ImageView) findViewById(R.id.foto);
slika.setImageBitmap(foto);

slika.setMinimumWidth(width);
slika.setMaxWidth(width);



}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
finish();
return true;
}

return super.onKeyDown(keyCode, event);
}

}

谢谢;)

日志: http://www.file.si/pfiles/273442/Untitled.png

最佳答案

Bitmap foto = BitmapFactory.decodeResource(getResources(),
Integer.parseInt(podatki.get(pic)));

是错误所在的行,更具体地说:

Integer.parseInt(podatki.get(pic))

根据 logcat,它看起来像 podatki.get(pic) 正在返回一个字符串,该字符串太长而无法转换为整数。

关于java - 我的程序在 ListView 中第二次单击时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479161/

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