Android setOnClickListener-应用程序意外停止

Android setOnClickListener-应用程序意外停止,android,onclick,buttonclick,application-shutdown,Android,Onclick,Buttonclick,Application Shutdown,当我在代码中使用setOnClickListener作为按钮时,当我启动应用程序时,它会显示应用程序意外停止 package example.khumbayatabbed; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import org.apa

当我在代码中使用setOnClickListener作为按钮时,当我启动应用程序时,它会显示
应用程序意外停止

package example.khumbayatabbed;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Category extends ListActivity{
    int categ=0;
    String[] categories = new String[100];
     Button button1;
     EditText edittext1;
       ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
edittext1= (EditText) findViewById(R.id.edittext);
        button1 = (Button) findViewById(R.id.button);
button1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
              edittext1.setText("");
              Toast.makeText(getApplicationContext(), "Hello",
                      Toast.LENGTH_SHORT).show();
            }
          });

       // setContentView(R.layout.main);
        // Create a crude view - this should really be set via the layout resources 
        // but since its an example saves declaring them in the XML.

        // Set the text and call the connect function. 
      //call the method to run the data retreival
        final String KEY_121 = "http://myurl";
        getServerData(KEY_121);
        if(mylist.isEmpty())
            Toast.makeText(getApplicationContext(), "No more sub categories",
                      Toast.LENGTH_SHORT).show();
        else
        display(mylist);
    }

    private void getServerData(String jason) {
        mylist = new ArrayList<HashMap<String, String>>();
       InputStream is = null;
       String returnString="";
       String result = "";
       //mylist.clear();
        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(jason);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

        //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }

        //parse json data
        try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                    HashMap<String, String> map = new HashMap<String, String>();
                        JSONObject json_data = jArray.getJSONObject(i);
                        Log.i("log_tag","url: "+json_data.getString("url")+
                                ", name: "+json_data.getString("value")
                        );
                        //Get an output to the screen
                        categories[categ++]=json_data.getString("value");
                        map.put("name", json_data.getString("value"));
                        map.put("url", json_data.getString("url"));
                        mylist.add(map);
                        returnString += jArray.getJSONObject(i).getString("url") + "\n";
                }

        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
    }
    private void display(ArrayList<HashMap<String, String>> list)
    {
        ListAdapter adapter=new SimpleAdapter(this,list,R.layout.list_item2, new String[] {"name"}, new int[]{R.id.cat_name});
        setListAdapter(adapter);
     final ListView lv = getListView();
     lv.setTextFilterEnabled(true);
     lv.setOnItemClickListener(new OnItemClickListener() {
         @SuppressWarnings("unchecked")
        public void onItemClick(AdapterView<?> parent, View view,
             int position, long id) {
             HashMap<String, String> tmp = new HashMap<String, String>();
             tmp=(HashMap<String, String>)parent.getItemAtPosition(position);
             String cat_url=tmp.get("url");
             final String KEY_121 = "http://myurl/"+cat_url;
             getServerData(KEY_121);
             if(mylist.isEmpty())
                Toast.makeText(getApplicationContext(), "No more sub categories",
                          Toast.LENGTH_SHORT).show();
            else
            display(mylist);
         }
       });
        }
}
main.xml如下所示:


您没有为版面编写setContentView。因此,您无法访问editText1和按钮1。 写一些像

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
在您的主页中调用此用法

Intent myIntent = new Intent(MainPage.this,Category.class);
                                        MainPage.this.startActivity(myIntent);

您没有为布局编写setContentView。因此,您无法访问editText1和按钮1。 写一些像

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
在您的主页中调用此用法

Intent myIntent = new Intent(MainPage.this,Category.class);
                                        MainPage.this.startActivity(myIntent);

在我为按钮添加onclick事件之前,此代码一直有效。即使我为EditText添加了一个操作侦听器,它也会说应用程序意外停止。。。请帮助。在我为按钮添加onclick事件之前,此代码一直有效。即使我为EditText添加了一个操作侦听器,它也会说应用程序意外停止。。。请帮忙。嘿,谢谢你的快速回复。。但是正如您在我的代码中看到的,我已经对setContentView(R.layout.main)进行了注释;陈述如果我取消注释它,它会再次显示“应用程序意外停止”。上面的代码是针对名为category的类的。我有一个主页,我从中调用此页面。这是一个活动。您如何从另一个活动调用此页面。您必须使用startActivity,还必须将此活动名称放入清单文件中您必须添加setContentView(R.layout.main);在绑定mail.xml文件中的任何权重之前。因此,请在绑定编辑文本之前使用这一行,如果它不能解决发送问题,请向我发送在logcat中打印的错误消息。如果我在代码中取消对setContentView(R.layout.main)的注释,并删除与按钮对应的代码和编辑文本,然后删除在取消对setContentView(R.layout.main)的注释之前起作用的代码。还开始给出错误“应用程序意外停止”…在super.onCreate(savedInstanceState)之后立即使用setContentView(R.layout.main);嘿,谢谢你的快速回复。。但是正如您在我的代码中看到的,我已经对setContentView(R.layout.main)进行了注释;陈述如果我取消注释它,它会再次显示“应用程序意外停止”。上面的代码是针对名为category的类的。我有一个主页,我从中调用此页面。这是一个活动。您如何从另一个活动调用此页面。您必须使用startActivity,还必须将此活动名称放入清单文件中您必须添加setContentView(R.layout.main);在绑定mail.xml文件中的任何权重之前。因此,请在绑定编辑文本之前使用这一行,如果它不能解决发送问题,请向我发送在logcat中打印的错误消息。如果我在代码中取消对setContentView(R.layout.main)的注释,并删除与按钮对应的代码和编辑文本,然后删除在取消对setContentView(R.layout.main)的注释之前起作用的代码。还开始给出错误“应用程序意外停止”…在super.onCreate(savedInstanceState)之后立即使用setContentView(R.layout.main);