不幸的是,当我使用Eclipse在模拟器中启动My app时,Myapp停止了

时间:2014-10-29 04:59:29

标签: android eclipse listview android-emulator onclicklistener

MainActivity.java这是我的主要java活动文件

package com.example.news;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//import java.awt.*;
import java.lang.*;
import java.io.*;

public class MainActivity extends ActionBarActivity {

    protected static final String Context = null;
    String[] data={"Times of India","Deccan chronical","The Hindu","Hindusthan Times","Indian Express"};
    int[] image_id={R.drawable.toi,R.drawable.dc,R.drawable.hindu,R.drawable.htime,R.drawable.indian};
    ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Customlistadapter adpt=new Customlistadapter(this,image_id,data);
        lv=(ListView)findViewById(R.id.list1);
       // ArrayAdapter<String> adpt=new ArrayAdapter<String>(this, R.layout.single_row,R.id.text1,data);
        lv.setAdapter(adpt);
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                if(position==0)
                {
                //Intent intent=new Intent(MainActivity.this,SendMesasage.class);
                //String message = "http:///";
               try
               {
                    Intent i=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.timesofindia.indiatimes.com"));
                startActivity(i);
               }
               catch(Exception e)
               {
                  // Log.i("soory");
                   //System.out.println("Sorry");
                   e.printStackTrace();
               }
                //intent.putExtra("ABCD", message);
               // startActivity(intent);
                }
                else if(position==1)
                {
                      try
                      {
                        Intent i=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.timesofindia.indiatimes.com"));
                    startActivity(i);
                      }
                      catch(Exception e)
                      {
                      // Log.i("soory");
                       //System.out.println("Sorry");
                       e.printStackTrace();
                      }

                }

            }


        });        
    }



}

即使我在调试程序时也找不到任何错误,当我启动它显示的应用时,&#34;不幸的是myapp已经停止&#34; 我无法理解为什么会这样。 我查看了一些与此问题相关的帖子,但我仍然无法找到解决方案。

**CustomListAdapter.java**


package com.example.news;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.*;
import java.lang.*;

public class Customlistadapter extends ArrayAdapter{
     String[] data;
     int[] image_id;
     Context context;
     public Customlistadapter(Activity context,int[] image_id2, String[] text){
     super(context, R.layout.single_row,text);
     // TODO Auto-generated constructor stub
     this.data=text;
     this.image_id=image_id2;
     this.context=context;
     }
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
     // TODO Auto-generated method stub
     LayoutInflater inflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View single_row= inflater.inflate(R.layout.single_row, null,
     true);
     TextView textView=(TextView) single_row.findViewById(R.id.text1);
     ImageView imageView = (ImageView) single_row.findViewById(R.id.imageView1);
     textView.setText(data[position]);
     imageView.setImageResource(image_id[position]);
     return single_row; 
     }
}

当我启动这个程序时,我会得到&#34;不幸的是Myapp已经停止了#34;所以我认为它是 NULLPOINTER例外,但我还是无法解决它 所以请伙计们帮帮我

activity_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.news.MainActivity" >

   <ListView 
       android:id="@+id/list1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_alignParentLeft="true">

   </ListView>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

你的getView()方法不正确。更改 getView(),如下所示:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub 

      ViewHolder view;

            if(convertView==null)
            {
                view = new ViewHolder();
                convertView=inflater.inflate(R.layout.single_row,null);
                view. textView=(TextView)convertView.findViewById(R.id.title);

                view.imageView=(ImageView)convertView.findViewById(R.id.list_image);


                convertView.setTag(view);
            }
            else
            {
                view = (ViewHolder) convertView.getTag();
            }

            view.textView.setText(data[position]);
            view.imageView.setImageResource(image_id[position]);

            return convertView;

        }

        public static class ViewHolder
        {
            public TextView textView;
            public ImageView imageView;
        }
}

还在适配器构造函数中初始化inflater:

 LayoutInflater inflater;
 public Customlistadapter(Activity context,int[] image_id2, String[] text){
     super(context, R.layout.single_row,text);
     // TODO Auto-generated constructor stub
     this.data=text;
     this.image_id=image_id2;
     this.context=context;
     inflater = LayoutInflater.from(this.context);
     }

要正确理解列表视图的回收机制,您必须咨询this