Soru & Cevap

kodlamada nerde hata yapıyorum ...

18.02.2015 - 03:28

Arkadaşlar kendi yazmış olduğum bu kodla sitemden veri çekiyorum AsyncTask içine yazdığımda program yazılım android tarafından kapanmaya zorlanıyor AsyncTask dışında onCreate içine yazdığımda çalışıyor bu sefer tabletde hata veriyor nasıl çözerim.

[code]

package com.sgs.lotocilginligi;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;


public class LotoSonuclari extends Activity {
    public ProgressDialog dialog = new ProgressDialog(LotoSonuclari.this);
    public int[] imageId = {R.drawable.image1,
            R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
            R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8,
            R.drawable.image9, R.drawable.image10, R.drawable.image11, R.drawable.image12,
            R.drawable.image13, R.drawable.image14, R.drawable.image15, R.drawable.image16,
            R.drawable.image17, R.drawable.image18, R.drawable.image19, R.drawable.image20,
            R.drawable.image21, R.drawable.image22, R.drawable.image23, R.drawable.image24,
            R.drawable.image25, R.drawable.image26, R.drawable.image27, R.drawable.image28,
            R.drawable.image29, R.drawable.image30, R.drawable.image31, R.drawable.image32,
            R.drawable.image33, R.drawable.image34, R.drawable.image35, R.drawable.image36,
            R.drawable.image37, R.drawable.image38, R.drawable.image39, R.drawable.image40,
            R.drawable.image41, R.drawable.image42, R.drawable.image43, R.drawable.image44,
            R.drawable.image45, R.drawable.image46, R.drawable.image47, R.drawable.image48,
            R.drawable.image49, R.drawable.image50, R.drawable.image51, R.drawable.image52,
            R.drawable.image53, R.drawable.image54
    };
    
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sonuclistele);

        new sonuclarial().execute("");

    }

    public class sonuclarial extends AsyncTask<String, String, String>{

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            dialog.setMessage("Yükleniyor...");
            dialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(" burada kod çektiğim link var ");
            HttpResponse response;

            try {
                response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();

                if (entity != null) {

                    try {
                        InputStream instream = entity.getContent();
                        String line = "";
                        BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                        StringBuilder json = new StringBuilder();

                        while ((line = reader.readLine()) != null) {
                            json.append(line + "\n");
                        }


                        JSONObject obj = new JSONObject(json.toString());
                        JSONArray Jsonuclar = obj.getJSONArray("sonuclar");


                        JSONObject s1 = Jsonuclar.getJSONObject(0);
                        JSONObject s2 = Jsonuclar.getJSONObject(1);
                        JSONObject s3 = Jsonuclar.getJSONObject(2);
                        JSONObject s4 = Jsonuclar.getJSONObject(3);

                        ListView lv;
                        lv = (ListView) findViewById(com.sgs.lotocilginligi.R.id.liste);

                        SatırBilgi sonucDizi[] = new SatırBilgi[]{
                                new SatırBilgi(1, imageId[s1.getInt("s1")], imageId[s1.getInt("s2")], imageId[s1.getInt("s3")], imageId[s1.getInt("s4")], imageId[s1.getInt("s5")], imageId[s1.getInt("s6")], "Tarih : " +  s1.getString("tarih"), "Hafta : " +  s1.getString("hafta"), "Model : " +  s1.getString("model")),
                                new SatırBilgi(2, imageId[s2.getInt("s1")], imageId[s2.getInt("s2")], imageId[s2.getInt("s3")], imageId[s2.getInt("s4")], imageId[s2.getInt("s5")], imageId[s2.getInt("s6")], "Tarih : " +  s2.getString("tarih"), "Hafta : " +  s2.getString("hafta"), "Model : " +  s2.getString("model")),
                                new SatırBilgi(3, imageId[s3.getInt("s1")], imageId[s3.getInt("s2")], imageId[s3.getInt("s3")], imageId[s3.getInt("s4")], imageId[s3.getInt("s5")], imageId[s3.getInt("s6")], "Tarih : " +  s3.getString("tarih"), "Hafta : " +  s3.getString("hafta"), "Model : " +  s3.getString("model")),
                                new SatırBilgi(4, imageId[s4.getInt("s1")], imageId[s4.getInt("s2")], imageId[s4.getInt("s3")], imageId[s4.getInt("s4")], imageId[s4.getInt("s5")], imageId[s4.getInt("s6")], "Tarih : " +  s4.getString("tarih"), "Hafta : " +  s4.getString("hafta"), "Model : " +  s4.getString("model"))
                        };

                        SatırAdaptor adap = new SatırAdaptor(LotoSonuclari.this, com.sgs.lotocilginligi.R.layout.list_satir, sonucDizi);
                        lv.setAdapter(adap);
                        instream.close();

                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext()," Hata : "+ e, Toast.LENGTH_LONG).show();
                    }
                }

            } catch (Exception e) {
                Toast.makeText(getApplicationContext()," Hata : "+ e, Toast.LENGTH_LONG).show();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            dialog.dismiss();
        }

    }

}

 

[/code]

24 Görüntülenme

5 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user sgsgokce
sgsgokce
24.02.2015 - 04:14

kodlamada söylenen global tanımlamaları yaptım gerekli bilgileri adaptordan lv set yaptım. Ancak yine çalışmadı sorun gözden kaçan ProgressDialog daymış bunu AsyncTask içine public olarak tanımladım onPreExecute show yaptım onPostExecute dismiss ve sorun çözüldü.

herkese yardımları için teşekkürler.

picture-31404-1421919825.png
muhcob
24.02.2015 - 11:45

Aslında arkadaşlar hatanı anlatmışlar ama ben de onların söylediklerini düzenledim. Aşağıdaki kodu dener misin.

 
 
 
    package com.sgs.lotocilginligi;
 
 
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
 
 
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONObject;
 
 
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.widget.ListView;
    import android.widget.Toast;
 
 
 
    public class LotoSonuclari extends Activity {
        private ListView lv;
        public ProgressDialog dialog = new ProgressDialog(LotoSonuclari.this);
        public int[] imageId = {R.drawable.image1,
                R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
                R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8,
                R.drawable.image9, R.drawable.image10, R.drawable.image11, R.drawable.image12,
                R.drawable.image13, R.drawable.image14, R.drawable.image15, R.drawable.image16,
                R.drawable.image17, R.drawable.image18, R.drawable.image19, R.drawable.image20,
                R.drawable.image21, R.drawable.image22, R.drawable.image23, R.drawable.image24,
                R.drawable.image25, R.drawable.image26, R.drawable.image27, R.drawable.image28,
                R.drawable.image29, R.drawable.image30, R.drawable.image31, R.drawable.image32,
                R.drawable.image33, R.drawable.image34, R.drawable.image35, R.drawable.image36,
                R.drawable.image37, R.drawable.image38, R.drawable.image39, R.drawable.image40,
                R.drawable.image41, R.drawable.image42, R.drawable.image43, R.drawable.image44,
                R.drawable.image45, R.drawable.image46, R.drawable.image47, R.drawable.image48,
                R.drawable.image49, R.drawable.image50, R.drawable.image51, R.drawable.image52,
                R.drawable.image53, R.drawable.image54
        };
 
 
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.sonuclistele);
 
            lv = (ListView) findViewById(com.sgs.lotocilginligi.R.id.liste);            
 
             SatırBilgi sonucDizi[] = new SatırBilgi[]{
            new SatırBilgi(1, imageId[s1.getInt("s1")], imageId[s1.getInt("s2")], imageId[s1.getInt("s3")], imageId[s1.getInt("s4")], imageId[s1.getInt("s5")], imageId[s1.getInt("s6")], "Tarih : " +  s1.getString("tarih"), "Hafta : " +  s1.getString("hafta"), "Model : " +  s1.getString("model")),
            new SatırBilgi(2, imageId[s2.getInt("s1")], imageId[s2.getInt("s2")], imageId[s2.getInt("s3")], imageId[s2.getInt("s4")], imageId[s2.getInt("s5")], imageId[s2.getInt("s6")], "Tarih : " +  s2.getString("tarih"), "Hafta : " +  s2.getString("hafta"), "Model : " +  s2.getString("model")),
            new SatırBilgi(3, imageId[s3.getInt("s1")], imageId[s3.getInt("s2")], imageId[s3.getInt("s3")], imageId[s3.getInt("s4")], imageId[s3.getInt("s5")], imageId[s3.getInt("s6")], "Tarih : " +  s3.getString("tarih"), "Hafta : " +  s3.getString("hafta"), "Model : " +  s3.getString("model")),
            new SatırBilgi(4, imageId[s4.getInt("s1")], imageId[s4.getInt("s2")], imageId[s4.getInt("s3")], imageId[s4.getInt("s4")], imageId[s4.getInt("s5")], imageId[s4.getInt("s6")], "Tarih : " +  s4.getString("tarih"), "Hafta : " +  s4.getString("hafta"), "Model : " +  s4.getString("model"))
            };
 
            new sonuclarial().execute();
 
 
        }
 
 
        public class sonuclarial extends AsyncTask<String, String, String>{
 
 
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                dialog.setMessage("Yükleniyor...");
                dialog.show();
            }
 
 
            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
 
 
                HttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(" burada kod çektiğim link var ");
                HttpResponse response;
 
 
                try {
                    response = httpclient.execute(httpget);
                    HttpEntity entity = response.getEntity();
 
 
                    if (entity != null) {
 
 
                        try {
                            InputStream instream = entity.getContent();
                            String line = "";
                            BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                            StringBuilder json = new StringBuilder();
 
 
                            while ((line = reader.readLine()) != null) {
                                json.append(line + "\n");
                            }
 
 
 
                            JSONObject obj = new JSONObject(json.toString());
                            JSONArray Jsonuclar = obj.getJSONArray("sonuclar");
 
 
 
                            JSONObject s1 = Jsonuclar.getJSONObject(0);
                            JSONObject s2 = Jsonuclar.getJSONObject(1);
                            JSONObject s3 = Jsonuclar.getJSONObject(2);
                            JSONObject s4 = Jsonuclar.getJSONObject(3);
 
 
                        } catch (Exception e) {
                            Toast.makeText(getApplicationContext()," Hata : "+ e, Toast.LENGTH_LONG).show();
                        }
                    }
 
 
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext()," Hata : "+ e, Toast.LENGTH_LONG).show();
                }
 
 
                return null;
            }
 
 
            @Override
            protected void onPostExecute(String result) {
                 SatırAdaptor adap = new SatırAdaptor(LotoSonuclari.this, com.sgs.lotocilginligi.R.layout.list_satir, sonucDizi);
                lv.setAdapter(adap);
                instream.close();
                dialog.dismiss();
            }
 
 
        } 
    }

Serkan GÖKÇE
24.02.2015 - 12:24
buda olmadı kapanmaya zorlandı diye hata veriyor. neyse artık biz bu android2i öğrenemiyecez.
Serkan GÖKÇE
24.02.2015 - 04:07
muhammed kardeşimizin vermiş olduğu cevap sonuc vermedi ama ondan feys alarak sorunu kendim hallettim...
Profile picture for user sgsgokce
sgsgokce
19.02.2015 - 04:15

Yardımda bulunan GökhanT ile BurhanAras beylere çok teşekkür ederim ancak ben bu işi beceremedim ve android kod yazmakdan sıkıldım.... Vaz geçeceğim gibi projeden.

picture-7541-1521024507.jpg
burhanaras
18.02.2015 - 10:36

1.
ListView lv; lv = (ListView) findViewById(com.sgs.lotocilginligi.R.id.liste);

satırlarını  LotoSonuclari içine taşı. lv global olmalı yalnız. 

 

2.  sonucDizi[] dizisini  Sonuclarial classi içinde global tanımla, onPostExecute() methodu içinde adaptorle listView'e set et.

NOT: Lütfen class isimlerini büyük harfle başlatın. Rüküş kadın görmüş Cemil İpekçi gibi oluyorum bu tür classları görünce.
 

Serkan GÖKÇE
19.02.2015 - 10:18
yardımlarınız için teşekkürler deniyeceğim
Serkan GÖKÇE
20.02.2015 - 02:26
Aras Bey ; Ne yaptıysam olmadı kodu doğru şekle çevirme şansınız varmı hem bende nerde haya yaptığımı öğrenirim. Yardımlarınız için şimdiden tşk.
Burhan Aras
20.02.2015 - 03:05
projeni full olarak upload et ya da zipleyip yolla. inceleyelim.
Profile picture for user gokhan_t
gokhan_t
18.02.2015 - 04:08


Merhaba, doInBackground içerisinde listView kullandığınız için hata alıyorsunuz. Listview e ekleyeceğiniz verileri bir listede toplayıp onpostexecute methodunda lisview e eklerseniz sorun çözülecektir.

Serkan GÖKÇE
18.02.2015 - 04:49
ben yapamadım yardımcı olursanız sevinirim olmazsanızda çok teşekkürler....