2011-07-06 133 views
1

只要我使用setOnClickListener在我的代码的按钮,当我启动应用程序,它说the application has stopped unexpectedlysetOnClickListener - 应用程序意外停止

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

这是我的主网页代码:

public class Khumbayatabbed extends TabActivity 
{ /** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Reusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 

    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, Category.class); 

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("category").setIndicator("Category", 
         res.getDrawable(R.drawable.ic_tab)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    // Do the same for the other tabs 
    intent = new Intent().setClass(this, Event.class); 
    spec = tabHost.newTabSpec("event").setIndicator("Event", 
         res.getDrawable(R.drawable.ic_tab)) 
        .setContent(intent); 
    tabHost.addTab(spec); 
    tabHost.setCurrentTab(0); 
} 
} 

main.xml中看起来是这样的:

+0

此代码的工作,直到我为按钮添加onclick事件。即使我为EditText添加一个动作侦听器,它说应用程序意外停止...请帮助。 – Himanshu

回答

5

你没有wrtie为您的setContentView你layout.So无法访问您的editText1和同为Button1的。 喜欢写东西

super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

在您的主页来调用这个使用

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

嘿谢谢你的快速回复..但正如你可以看到我的代码中我已经评论了setContentView(R.layout.main);声明。如果我取消注释,它会再次显示“应用程序已意外停止”。上面的代码是针对一个名为category的类。我有我称之为这个页面的主页。 – Himanshu

+0

这是一个活动。如何从另一个活动调用此方法。您必须使用startActivity,并且必须将此活动名称放入清单文件 – Rasel

+0

中,您必须添加setContentView(R.layout.main);在从mail.xml文件中绑定任何权重之前。所以在绑定你的编辑文本之前使用这一行,如果它没有解决发送然后发送我在logcat中打印的错误消息。 –