Android Dinamik Buton

Rapor et
Soru

Bu soru rapor edilmelidir hissediyorum neden kısaca açıklayınız.

Rapor et
İptal

package com.proje.adapter;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.proje.AppController;
import com.proje.R;
import com.proje.model.Mesaj;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MesajAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Mesaj> MesajItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    public MesajAdapter(Activity activity, List<Mesaj> mesajItems) {
        this.activity = activity;
        this.mesajItems = mesajItems;
    }
    @Override
    public int getCount() {
        return mesajItems.size();
    }
    @Override
    public Object getItem(int location) {
        return mesajItems.get(location);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.mesaj_liste, null, false);
        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView profilResmi = (NetworkImageView) convertView
                .findViewById(R.id.profil_resmi);
        TextView konu = (TextView) convertView.findViewById(R.id.mesajkonu);
        TextView icerik = (TextView) convertView.findViewById(R.id.mesajicerik);
        TextView begeni = (TextView) convertView.findViewById(R.id.mesajbegeni);
        TextView tarih = (TextView) convertView.findViewById(R.id.mesajtarih);
        // Her bir itiraf satırı için bilgi alınıyor
        final Mesaj m = mesajItems.get(position);
        // Profil resmi
        profilResmi.setImageUrl(m.getProfilResmi(), imageLoader);
        // Konu
        konu.setText(m.getKonu());
        //İçerik
        icerik.setText(m.getIcerik());
        // Begeni
        begeni.setText(String.valueOf(m.getBegeni()));
        // Tatih
        tarih.setText(String.valueOf(m.getTarih()));
        butoncalistir();
        return convertView;
    }
    private void butoncalistir() {
        Button myButton = new Button(activity);
        final RelativeLayout ll = (RelativeLayout) activity.findViewById(R.id.mesaj_layout);  // butonun eklenecegi layout tanimlandi
        myButton.setId(1);   // butona id atandi
        myButton.setX(150);  // butona x koordinati verildi
        myButton.setY(300);  // butona y koordinati verildi
        myButton.setText(“Button”);
        final int id=myButton.getId();
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(150,80);  // butonun boyutlari verildi
**************************************************************************************************************************************
        ll.addView(myButton, lp);
*****************************************************************************************************************************************************
        myButton = ((Button) activity.findViewById(id));
        myButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {  // butonun click olayi
                Toast.makeText(view.getContext(),
                        “Button a tiklandi”, Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }
    private void postData(final String id) {
        RequestQueue rq = Volley.newRequestQueue(activity);
        StringRequest postReq = new StringRequest(Request.Method.POST, “http://mesaj.hol.es/begen.php“, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //beğeni işlemi sonrasındaki olumlu yanıt
                Toast.makeText(activity,response,Toast.LENGTH_LONG).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Beğeni işlemi sırasında hata olursa gösterilecek
                System.out.println(“Error [“+error+”]”);
            }
        })  {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> parametreler = new HashMap<String, String>();
                //Gönderilen itiraf id parametresi ve anahtar sözcüğü
                parametreler.put(“id”, id);
                return parametreler;
            }
        };
        //İşlemi volley kuyruğuna ekleme
        rq.add(postReq);
    }
}
Cevaplandı 0
Android 4 sene 7 Cevap 1155 görüntüleme Intern 0

Yazar HakkındaIntern

Cevaplar ( 7 )

  1. Avatar

    Bu cevap rapor edilmelidir hissediyorum neden kısaca açıklayınız.

    Rapor et
    İptal

    Verdiği hata tam olarak nedir?

  2. Avatar

    Bu cevap rapor edilmelidir hissediyorum neden kısaca açıklayınız.

    Rapor et
    İptal

    birazdan atayım hocam ..

    En iyi cevap
  3. Avatar

    Bu cevap rapor edilmelidir hissediyorum neden kısaca açıklayınız.

    Rapor et
    İptal

    tam hata bu ;

    08-15 07:41:39.937 16295-16295/com.proje E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.proje.adapter.MesajAdapter.butoncalistir(MesajAdapter.java:113)
    at com.proje.adapter.MesajAdapter.getView(MesajAdapter.java:94)
    at android.widget.AbsListView.obtainView(AbsListView.java:2267)
    at android.widget.ListView.makeAndAddView(ListView.java:1769)
    at android.widget.ListView.fillDown(ListView.java:672)
    at android.widget.ListView.fillFromTop(ListView.java:733)
    at android.widget.ListView.layoutChildren(ListView.java:1608)
    at android.widget.AbsListView.onLayout(AbsListView.java:2102)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:596)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1187)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    at android.view.Choreographer.doFrame(Choreographer.java:525)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
    08-15 07:41:39.941 16295-16299/com.proje D/dalvikvm: GC_CONCURRENT freed 465K, 6% free 8649K/9159K, paused 11ms+1ms, total 20ms

  4. Avatar

    Bu cevap rapor edilmelidir hissediyorum neden kısaca açıklayınız.

    Rapor et
    İptal

    kodu bu şekilde yazıp deneyebilirsin? Önce myButton değerinin içini doldururup sonra ll.addView yapmayı. myButton null dönüyor olmalı.

  5. Avatar

    Bu cevap rapor edilmelidir hissediyorum neden kısaca açıklayınız.

    Rapor et
    İptal

    Dediğiniz şekilde artık kodum ;

    Button myButton = new Button(activity);
    final RelativeLayout ll = (RelativeLayout) activity.findViewById(R.id.mesaj_layout); // butonun eklenecegi layout tanimlandi
    myButton.setId(1); // butona id atandi
    myButton.setX(150); // butona x koordinati verildi
    myButton.setY(300); // butona y koordinati verildi
    myButton.setText(“Button”);
    final int id=myButton.getId();
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(150,80); // butonun boyutlari verildi
    myButton = ((Button) activity.findViewById(id));
    ll.addView(myButton, lp);

    fakat hata yine addView satırında ..

    hata ;

    08-15 16:07:47.851 16724-16724/com.proje E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.proje.adapter.MesajAdapter.getView(MesajAdapter.java:102)
    at android.widget.AbsListView.obtainView(AbsListView.java:2267)
    at android.widget.ListView.makeAndAddView(ListView.java:1769)
    at android.widget.ListView.fillDown(ListView.java:672)
    at android.widget.ListView.fillFromTop(ListView.java:733)
    at android.widget.ListView.layoutChildren(ListView.java:1608)
    at android.widget.AbsListView.onLayout(AbsListView.java:2102)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:596)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1187)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:13754)
    at android.view.ViewGroup.layout(ViewGroup.java:4362)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    at android.view.Choreographer.doFrame(Choreographer.java:525)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

  6. Avatar

    Bu cevap rapor edilmelidir hissediyorum neden kısaca açıklayınız.

    Rapor et
    İptal

    cevaplandı olarak gözüküyor ama sorunum çözülmedi hocan

  7. Avatar

    Bu cevap rapor edilmelidir hissediyorum neden kısaca açıklayınız.

    Rapor et
    İptal

    konuyu kapatıp yenisini açıyorum ..

Cevap bırak