简体   繁体   English

不幸的是,当我使用Eclipse在模拟器中启动我的应用程序时,Myapp已停止

[英]Unfortunately Myapp has stopped when i was launch My app in emulator using Eclipse

MainActivity.java//This is my main java activity file 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();
                      }

                }

            }


        });        
    }



}

Even I couldn't find any error while debugging the program,when I launch the app it shows, "Unfortunately myapp has stopped " I couldn't understand why it this is happening. 即使我在调试程序时也找不到任何错误,但是当我启动显示为“不幸的是myapp已停止”的应用程序时,我也不知道为什么会这样。 I looked some previous posts which are related to this problem,but still i couldn't find solution. 我看了以前与该问题有关的帖子,但仍然找不到解决方案。

**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; 
     }
}

When i was launching this program i would be getting "Unfortunately Myapp has stoped " so I think it was NULLPOINTER Exception but still i couldn't fix it So please guys help me 当我启动该程序时,我会收到“不幸的是Myapp停止 ”,所以我认为这是NULLPOINTER Exception,但仍然无法解决,所以请大家帮我

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>

Your getView() method is not correct.Change your getView() as follows: 您的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;
        }
}

Also initialise the inflater within the adapter constructor: 还要在适配器构造函数中初始化充气机

 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);
     }

To understand recycling mechanism of a listview correctly,you must consult this . 要正确了解ListView的回收机制,您必须参考此内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM