Android列表视图在一个对话框中,取空指针异常

时间:2012-11-25 13:27:41

标签: android android-listview

我无法将listview放入对话框中。这是我的示例代码,

这段代码有什么问题,我正在使用空指针异常

我为列表项和列表视图创建了xml,我使用了一个适配器来放置它

public class PBActivity extends ListActivity implements OnClickListener{

HttpClient httpclnt;
HttpPost httppst; 

String message;
String message2;
String response;

ImageButton star;
ImageButton box;

Handler handler;

Runnable sendNumbers;

final Context context = this;

PBList rs = new PBList ();
PBList rs2 = new PBList ();
PBList rs3 = new PBList ();

private ProgressDialog m_ProgressDialog = null;

private ArrayList<PBList > m_results = null;

private RSAdapter m_adapter;



public class KT {

    public KT (){};

    private String experience;
    private String writtenBy;


    public String getExperience() {
        return experience;
    }

    public void setTitle(String Experience) {
        this.experience = Experience;
    }

    public String getWrittenBy() {
        return writtenBy;
    }

    public void setWrittenBy(String WrittenBy) {
        this.writtenBy = WrittenBy;
    }


}

public class PBList {

    public PBList(){};

    private String title;
    private KT kt;

    public String getTitle() {
        return title;
    }

    public void setTitle(String Title) {
        this.title = Title;
    }

    public KT getKT(){
        return kt;
    }

    public void setKT(KT KT) {
        this.kt = KT;
    }

}

private class RSAdapter extends ArrayAdapter<PBList> {

    private ArrayList<PBList> items;

    public RSAdapter(Context context, int textViewResourceId,
            ArrayList<PBList> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        PBList results = items.get(position);
        if (results!= null) {
            TextView title = (TextView) v.findViewById(R.id.title);

            if (title != null) {
                title.setText(results.getTitle());
            }
        }


        return v;
    }
}




public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pb);

        box = (ImageButton) findViewById(R.id.box);
        box.setOnClickListener(this);

        star = (ImageButton) findViewById(R.id.star);
        star.setOnClickListener(this);

        m_results = new ArrayList<PBList>();

        this.m_adapter = new RSAdapter(this, R.layout.row, m_results);
        setListAdapter(this.m_adapter);

        rs.setTitle("lOREM Ipsum);
        rs2.setTitle("lorem");
        rs3.setTitle("ipsum");

        m_results.add(rs);
        m_results.add(rs2);
        m_results.add(rs3);


        m_adapter.notifyDataSetChanged();

        }           


@Override
public void onClick(View v) {

    if(v==star)
    {

         final Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.activity_kt);




            class KTAdapter extends ArrayAdapter<KT> {

                private ArrayList<KT> items;

                public KTAdapter(Context context, int textViewResourceId,
                        ArrayList<KisiselTecrube> items) {
                    super(context, textViewResourceId, items);
                    this.items = items;
                }

                @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                    View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        v = vi.inflate(R.layout.rowkt, null);
                    }
                    KT kisisel = items.get(position);
    if(kisisel!=null) TextView title = (TextView)v.findViewById(R.id.title2);

                        if (title != null) {
                    title.setText(kisisel.getWrittenBy());
                        }
                    }


                    return v;
                }
            }

            ArrayList<KT> m_results2 = null;

            KTAdapter m_adapter2;

            KTks = new KisiselTecrube();

            ListView lv = (ListView) findViewById(R.id.listview);

            m_results2 = new ArrayList<KT>();

    m_adapter2 = new KisiselTecrubeAdapter(getApplicationContext(), 
    R.layout.rowkisiseltecrube, m_results2);
            lv.setAdapter(m_adapter2);


            ks.setWrittenBy("erdem");

            m_results2.add(ks);




            m_adapter2.notifyDataSetChanged();

            /*
            Button dialogButton2 = (Button) dialog.findViewById(R.id.Back);


        dialogButton2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();

                } });*/ 
            dialog.show();

    }


    else if(v==box)
    {

    }
}


}

我的XML文件

activity_kt.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="95dp"
    android:background="#ffffff">

  <ImageView
   android:id="@+id/imageView1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginLeft="10dp"
   android:layout_marginTop="10dp"
   android:src="@drawable/sclogo" />

  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:orientation="vertical" >

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="40dp"
       android:layout_marginTop="10dp"
       android:textSize="25sp" />

        </LinearLayout>

     </LinearLayout>

  <ListView
   android:id="@+id/listview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_below="@+id/button1"
   android:background="#000000"
   android:cacheColorHint="#00000000"
   android:fastScrollEnabled="true" 
   android:divider="@android:color/white"
   android:dividerHeight="1dp"/>

  <TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""
android:textColor="@android:color/black"/>

  </RelativeLayout>

row_kt.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

  <View 
    android:layout_width="0dp"
    android:layout_height="5dp"/>
  <TextView
     android:id="@+id/title2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textStyle="bold"
    android:singleLine="true"
    android:ellipsize="end"
    android:textColor="@android:color/white"/>

 <View 
    android:layout_width="0dp"
    android:layout_height="5dp"/>

   </LinearLayout>

row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

 <View 
    android:layout_width="0dp"
    android:layout_height="5dp"/>
 <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textStyle="bold"
    android:singleLine="true"
    android:ellipsize="end"
    android:textColor="@android:color/white"/>

 <View 
    android:layout_width="0dp"
    android:layout_height="5dp"/>

   </LinearLayout>





  11-25 17:14:28.456: E/AndroidRuntime(32029): FATAL EXCEPTION: main
  11-25 17:14:28.456: E/AndroidRuntime(32029): java.lang.NullPointerException
11-25 17:14:28.456: E/AndroidRuntime(32029):    at     com.kodatolye.sirtcantalilar.PratikBilgilerActivity.onClick(PratikBilgilerActivity.java:249)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at android.view.View.performClick(View.java:2533)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at android.view.View$PerformClick.run(View.java:9320)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at android.os.Handler.handleCallback(Handler.java:587)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at android.os.Looper.loop(Looper.java:150)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at android.app.ActivityThread.main(ActivityThread.java:4385)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at java.lang.reflect.Method.invoke(Method.java:507)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
11-25 17:14:28.456: E/AndroidRuntime(32029):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案