我的程序在listview中第二次单击时崩溃

时间:2013-01-23 11:57:05

标签: java android android-intent bufferedreader

解决了;) 我知道它的蝙蝠想法,但我使用Globals .class,现在它工作;) 现在它起作用;)hurahhh

谢谢你们

主要问题

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

有人能告诉我我做错了什么,因为它第一次完美地适用于每个元素,而第二次它甚至无法工作,即使是最初点击的ListView项目(没有任何问题)也会再次点击。< / p>

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

    }

}

主要活动:

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

    }

谢谢;)

logcat的: http://www.file.si/pfiles/273442/Untitled.png

3 个答案:

答案 0 :(得分:1)

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

在这里你得到错误我的东西根据错误消息它太变化的值所以它的Long不是Int值。在MainActivity中签入onCreate()

<强>被修改

在点击项目中,您选择资源并存储在一个对象中此对象附加在 sb 对象中,现在每次点击它时会发生什么事情会附加它。< / p>

temp=""+sb.append(a); // here appended a (selected resource value)

每次都需要清除此值,或者您可以在intent对象的另一个额外值中传递此值

in.putExtra("resourceId", a);

然后在MainActivity上不需要解析和类型转换问题只需获取int值并直接在BitmapFactory中使用,就像这样

Bitmap foto = BitmapFactory.decodeResource(getResources(),extras.getInt("resourceId"));

答案 1 :(得分:1)

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

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

Integer.parseInt(podatki.get(pic))

根据logcat,看起来podatki.get(pic)返回的字符串太长而无法转换为整数。

答案 2 :(得分:0)

问题在于您的代码中的这一行。

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

将其更改为

 Bitmap foto = BitmapFactory.decodeResource(getResources(),
                    R.drawable.yourpic);

希望有所帮助