1

解决了;)我知道它的想法,但我使用了 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();

    }

}

主要活动:

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

4

3 回答 3

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

在这里,您收到错误消息,根据错误消息,它的值变化太大,因此它的 Long 不是 Int 值。在 MainActivity 中签入 onCreate()

已编辑

在单击项目中,您正在选择资源并存储在一个对象中,这个对象现在附加在sb对象中,现在每次单击它都会附加它。

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

您需要每次清除此值,或者您可以将其传递给意图对象中的另一个额外值

in.putExtra("resourceId", a);

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

Bitmap foto = BitmapFactory.decodeResource(getResources(),extras.getInt("resourceId"));
于 2013-01-23T12:16:47.040 回答
1
Bitmap foto = BitmapFactory.decodeResource(getResources(),
                Integer.parseInt(podatki.get(pic)));

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

Integer.parseInt(podatki.get(pic))

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

于 2013-01-23T12:16:48.127 回答
0

问题出在代码中的这一行。

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

如下更改

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

希望能帮助到你

于 2013-01-23T12:18:22.267 回答