当前位置: 编程技术>移动开发
本页文章导读:
▪view的空指针错误,listview加载数据底部添加按钮 view的空指针异常,listview加载数据底部添加按钮
listview数据加载后底部添加按钮:
先写一个xml文件,里面添加按钮控件,如下listbutton。xml,按钮居中对齐的哦。
<?xml version="1.0" encoding="utf-.........
▪ TextView添加Onclick点击无效没反应解决方法 TextView添加Onclick点击无效没反应解决办法
再给TextView添加Onclick事件时,发现点击无效没反应,原因是还需要配置android:clickable="true"。
<TextView
android:id="@id/group"
android:l.........
▪ Proactor鍜孯eactor妯″紡_缁х画骞跺彂绯荤粺璁捐鐨勬壂鐩?杞 Proactor鍜孯eactor妯″紡_缁х画骞跺彂绯荤粺璁捐鐨勬壂鐩?杞?
Proactor鍜孯eactor妯″紡_缁х画骞跺彂绯荤粺璁捐鐨勬壂鐩?Proactor鍜孯eactor閮芥槸骞跺彂缂栫▼涓殑璁捐妯″紡銆傚湪鎴.........
[1]view的空指针错误,listview加载数据底部添加按钮
来源: 互联网 发布时间: 2014-02-18
view的空指针异常,listview加载数据底部添加按钮
listview数据加载后底部添加按钮:
先写一个xml文件,里面添加按钮控件,如下listbutton。xml,按钮居中对齐的哦。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:layout_gravity="center_vertical|center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/lastButton" android:layout_width="wrap_content" android:layout_height="30px" android:gravity="center_vertical" android:layout_marginTop="5px" android:text="上一页" android:onClick="lastPageDate"/> <TextView android:id="@+id/counTe" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="" /> <Button android:id="@+id/nextButton" android:layout_width="wrap_content" android:layout_height="30px" android:layout_gravity="center_vertical" android:layout_marginTop="5px" android:layout_marginLeft="10px" android:text="下一页" android:onClick="nextPageDate"/> </LinearLayout>
放listview的那个xml就不展示了,因为很简单,只是在界面里加一个listview。不过必须设置android:layout_width="fill_parent",否则按钮无法居中。
下面就是后台代码了。
MainActivity。java。
package com.example.dbconnectiontest; /*必须引用apache.http相关类来创建HTTP连接*/ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.security.auth.PrivateCredentialPermission; import org.apache.http.Header; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import com.example.dbconnectiontest.entity.people; import com.example.dbconnectiontest.jsonto.JsonToList; import android.R.integer; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; /* 必须引用java.io与java.util相关类来读写文件*/ public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private static final String URL2 = "http://192.168.1.101:8080/BootStarpImage/getList.action"; static int index = 1;//存放页码 static int indexCount = 5;//存放总页码 Button seButton,lastButton,nextButton;//获取数据按钮,上一页,下一页按钮 TextView counTextView;//显示页码 ListView lView;//listview View buView; //获取listview底部按钮的xml private int[] lay = new int[4];//存放实体类对象的text数组 private List data = new ArrayList();//存放获取到的数据 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seButton = (Button)findViewById(R.id.btnOne); seButton.setOnClickListener(new clicklist()); lView = (ListView)findViewById(R.id.listview); /** * 在listview底部添加按钮 */ buView = getLayoutInflater().inflate(R.layout.listbutton, null); lView.addFooterView(buView); /** * 必须放在lview后面,先添加listview才能找到listview上的控件,否则抛出空指针异常 */ lastButton = (Button)findViewById(R.id.lastButton); nextButton = (Button)findViewById(R.id.nextButton); counTextView = (TextView)findViewById(R.id.counTe); } /** {"people":[{"firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }, { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},{ "firstName": "Elliotte","lastName":"Harold", "email": "cccc" }]} */ @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class clicklist implements OnClickListener{ @Override public void onClick(View v) { String urlString = URL2 + "?index=1"; String string = httpToServiceSelectData(urlString); listToData(string); /** * 查询总页码 */ // urlString = ""; // string = httpToServiceSelectData(urlString); setPageButton(v);此处必须传入v及view,否则后续报出关于view的空指针 } } /** * 请求数据 传入一个url * @param urlString */ private String httpToServiceSelectData(String urlString) { HttpClient client=new DefaultHttpClient(); //创建一个GET请求 HttpGet httpGet=new HttpGet(urlString); //向服务器发送请求并获取服务器返回的结果 HttpResponse response; try { response = client.execute(httpGet); int staCode = response.getStatusLine().getStatusCode(); if(staCode == 200){ //返回的结果可能放到InputStream,http Header中等。 String string = EntityUtils.toString(response.getEntity(),HTTP.UTF_8); return string;//返回读取到的数据,用其它方法进行转换成实体 }else if(staCode == 404) Toast.makeText(getApplicationContext(), "服务器异常,请稍候再试。", Toast.LENGTH_SHORT).show(); else if (staCode == 500) Toast.makeText(getApplicationContext(), "读取数据失败,请稍候查询。", Toast.LENGTH_SHORT).show(); } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IllegalStateException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将查询到的数据添加到listview中 * @param string 返回的字符串(转成过的) */ private void listToData(String string) { JsonToList jToList = new JsonToList(); List<people> li = jToList.returnListView(string);此处调用的是一个java类中的方法,自己写的,代码就几行。要的在下面 for (people ent : li) { Map<String, String> map = new HashMap<String, String>(); map.put("id", ent.getId()+""); map.put("firstName", ent.getFirstName()); map.put("lastName", ent.getLastName()); map.put("email", ent.getEmail()); data.add(map); } SimpleAdapter sAdapter = new SimpleAdapter(MainActivity.this, data, R.layout.listview, new String[]{"id","firstName","lastName","email"}, new int[]{R.id.ids,R.id.firstName,R.id.lastName,R.id.email}); sAdapter.notifyDataSetChanged();//增加或删除操作进行后调用该方法,更改listview中的数据,否则会一直往listview下方添加 lView.setAdapter(sAdapter); } //设置页码信息,以及按钮的是否可点击 private void setPageButton(View view) { counTextView.setText(index + "/" + indexCount);//设置页码数 if(index == 1) lastButton.setClickable(false); else lastButton.setClickable(true); if(index == indexCount) nextButton.setClickable(false); else nextButton.setClickable(true); } @Override protected void onDestroy() { super.onDestroy(); } //点击过按钮过后,加载数据时按钮不可用 public void setButtonAll(Button lastButton,Button nextButton) { lastButton.setClickable(false); nextButton.setClickable(false); } /** * 上一页按钮点击事件 * @param view */ public void lastPageDate(View view) { // setButtonAll(lastButton,nextButton); if (index > 1) { index--; // String url = ""; // String string = httpToServiceSelectData(url); // listToData(string);//设置数据 setPageButton(view); } } /** * 下一页按钮点击事件 * @param view */ public void nextPageDate(View view) { // setButtonAll(lastButton,nextButton); if (index < indexCount) { index++; // String url = ""; // String string = httpToServiceSelectData(url); // listToData(string);//设置数据 Toast.makeText(MainActivity.this, index+"", Toast.LENGTH_SHORT).show();//此处的index必须+“”,转换为字符串,否则报错 System.out.println(index++); setPageButton(view); } } }
JsonToList。java
public class JsonToList { public List<people> returnListView(String string) { java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<people>>(){}.getType(); Gson gson = new Gson(); List<people> list = gson.fromJson(string, type); return list; } }
下面来看一下关于view的错误。例如上面的setPageButton(View view)方法变为setPageButton()就会报错。
[2] TextView添加Onclick点击无效没反应解决方法
来源: 互联网 发布时间: 2014-02-18
TextView添加Onclick点击无效没反应解决办法
再给TextView添加Onclick事件时,发现点击无效没反应,原因是还需要配置android:clickable="true"。
<TextView android:id="@id/group" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="1.0" android:clickable="true" android:onClick="Tuan" android:drawableTop="@drawable/ic_menu_deal_selector" android:text="@string/deal" />
[3] Proactor鍜孯eactor妯″紡_缁х画骞跺彂绯荤粺璁捐鐨勬壂鐩?杞
来源: 互联网 发布时间: 2014-02-18
Proactor鍜孯eactor妯″紡_缁х画骞跺彂绯荤粺璁捐鐨勬壂鐩?杞?
Proactor鍜孯eactor妯″紡_缁х画骞跺彂绯荤粺璁捐鐨勬壂鐩?
Proactor鍜孯eactor閮芥槸骞跺彂缂栫▼涓殑璁捐妯″紡銆傚湪鎴戠湅鏉ワ紝浠栦滑閮芥槸鐢ㄤ簬娲惧彂/鍒嗙IO鎿嶄綔浜嬩欢鐨勩€傝繖閲屾墍璋撶殑
IO浜嬩欢涔熷氨鏄濡俽ead/write鐨処O鎿嶄綔銆?娲惧彂/鍒嗙"灏辨槸灏嗗崟鐙殑IO浜嬩欢閫氱煡鍒颁笂灞傛ā鍧椼€備袱涓ā寮忎笉鍚岀殑鍦版柟
鍦ㄤ簬锛孭roactor鐢ㄤ簬寮傛IO锛岃€孯eactor鐢ㄤ簬鍚屾IO銆?
鍙互鐪嬪嚭锛屼袱涓ā寮忕殑鐩稿悓鐐癸紝閮芥槸瀵规煇涓狪O浜嬩欢鐨勪簨浠堕€氱煡(鍗冲憡璇夋煇涓ā鍧楋紝杩欎釜IO鎿嶄綔鍙互杩涜鎴栧凡缁忓畬鎴?銆傚湪缁撴瀯
涓婏紝涓よ€呬篃鏈夌浉鍚岀偣锛歞emultiplexor璐熻矗鎻愪氦IO鎿嶄綔(寮傛)銆佹煡璇㈣澶囨槸鍚﹀彲鎿嶄綔(鍚屾)锛岀劧鍚庡綋鏉′欢婊¤冻鏃讹紝灏卞洖璋僪andler銆?
涓嶅悓鐐瑰湪浜庯紝寮傛鎯呭喌涓?Proactor)锛屽綋鍥炶皟handler鏃讹紝琛ㄧずIO鎿嶄綔宸茬粡瀹屾垚锛涘悓姝ユ儏鍐典笅(Reactor)锛屽洖璋僪andler鏃讹紝琛ㄧず
IO璁惧鍙互杩涜鏌愪釜鎿嶄綔(can read or can write)锛宧andler杩欎釜鏃跺€欏紑濮嬫彁浜ゆ搷浣溿€?
Reactor閫氳繃鏌愮鍙樺舰锛屽彲浠ュ皢鍏舵敼瑁呬负Proactor锛屽湪鏌愪簺涓嶆敮鎸佸紓姝O鐨勭郴缁熶笂锛屼篃鍙互闅愯棌搴曞眰鐨勫疄鐜帮紝鍒╀簬缂栧啓璺ㄥ钩鍙?
浠g爜銆傛垜浠彧闇€瑕佸湪dispatch(涔熷氨鏄痙emultiplexor)涓皝瑁呭悓姝O鎿嶄綔鐨勪唬鐮侊紝鍦ㄤ笂灞傦紝鐢ㄦ埛鎻愪氦鑷繁鐨勭紦鍐插尯鍒拌繖涓€灞傦紝
杩欎竴灞傛鏌ュ埌璁惧鍙搷浣滄椂锛屼笉鍍忓師鏉ョ珛鍗冲洖璋僪andler锛岃€屾槸寮€濮婭O鎿嶄綔锛岀劧鍚庡皢鎿嶄綔缁撴灉鏀惧埌鐢ㄦ埛缂撳啿鍖?璇?锛岀劧鍚庡啀
鍥炶皟handler銆傝繖鏍凤紝瀵逛簬涓婂眰handler鑰岃█锛屽氨鍍忔槸proactor涓€鏍枫€傝缁嗘妧娉曞弬瑙佽繖绡囨枃绔犮€?
鍏跺疄灏辫璁℃ā寮忚€岃█锛屾垜涓汉瑙夊緱鏌愪釜妯″紡鍏跺疄鏄病鏈夊畬鍏ㄥ浐瀹氱殑缁撴瀯鐨勩€備笉鑳借鏌愪釜妯″紡閲屽氨鑲畾浼氭湁鏌愪釜绫伙紝绫讳箣闂寸殑
鍏崇郴灏辫偗瀹氭槸杩欐牱銆傚湪瀹為檯鍐欑▼搴忚繃绋嬩腑涔熷緢灏戝幓鐗瑰埆鍦板疄鐜版煇涓ā寮忥紝鍙兘璇存ā寮忎細缁欎綘鏇村鏇村ソ鐨勬灦鏋勬柟妗堛€?
鏈€杩戝湪鐪媠pserver鐨勪唬鐮侊紝鐪嬪埌鍒汉鎻愬悇绉嶅苟鍙戠郴缁熶腑鐨勬ā寮忥紝鏈夌偣鐪肩孩锛屼簬鏄墠鏉ユ壂鎵洸銆傜煡閬撲粈涔堟槸leader follower妯″紡锛?
reactor, proactor锛宮ultiplexing锛屽浜庡績涓殑閭d釜缃戠粶搴撲篃瓒婃潵瓒婃竻鏅般€?
鏈€杩戣繕骞蹭簡浜涚璋辩殑浜嬶紝鍐欎簡浼犺涓殑瀛楄妭娴佺紪鐮侊紝鐢ㄦā鏉跨殑鏂瑰紡瀹炵幇锛屼笉浣嗕繚鎸佷簡鎵╁睍鎬э紝杩樺皯鍐欏緢澶氫唬鐮侊紱澶勪簬鏁堢巼鑰冭檻锛?
鍐欎簡涓猻tatic array瀹瑰櫒(鍏跺疄灏辨槸template <typename _Tp, std::size_t size> class static_array { _Tp _con[size])锛?
鍔犱簡iterator锛岄伒寰猄TL鏍囧噯锛屽彲浠ョ粨鍚堣繘STL鐨勫悇涓猤eneric algorithm鐢紝鑷垜鎰熻涓嶉敊銆傚熀纭€妯″潡鎼缓瀹屾瘯锛岃В鏋愪簡鍏徃
鏈嶅姟鍣ㄧ綉缁滄ā鍧楃殑娑堟伅锛屾垜鏄笉鏄湡鐨勬墦绠楃敤鑷繁鐨勭綉缁滄ā鍧楅噸鍐欐垜鐨勯獙璇佹湇鍔″櫒锛熷湪鍙︿竴涓粰鍏徃鍐欑殑宸ュ叿閲岋紝鍥犱负瀹炲湪鍘屾伓
瓒婃潵瓒婂鐨勯噸澶嶄唬鐮侊紝绱㈡€у啓浜嗗嚑涓畯锛岃繕鐪熺殑鍋氬埌浜嗕唬鐮佺殑鑷姩鐢熸垚:D銆?
瀵逛紭闆呬唬鐮佺殑杩芥眰鐪熺殑鎴愪簡绉嶇櫀濂? = =|
Proactor鍜孯eactor妯″紡_缁х画骞跺彂绯荤粺璁捐鐨勬壂鐩?
Proactor鍜孯eactor閮芥槸骞跺彂缂栫▼涓殑璁捐妯″紡銆傚湪鎴戠湅鏉ワ紝浠栦滑閮芥槸鐢ㄤ簬娲惧彂/鍒嗙IO鎿嶄綔浜嬩欢鐨勩€傝繖閲屾墍璋撶殑
IO浜嬩欢涔熷氨鏄濡俽ead/write鐨処O鎿嶄綔銆?娲惧彂/鍒嗙"灏辨槸灏嗗崟鐙殑IO浜嬩欢閫氱煡鍒颁笂灞傛ā鍧椼€備袱涓ā寮忎笉鍚岀殑鍦版柟
鍦ㄤ簬锛孭roactor鐢ㄤ簬寮傛IO锛岃€孯eactor鐢ㄤ簬鍚屾IO銆?
鍙互鐪嬪嚭锛屼袱涓ā寮忕殑鐩稿悓鐐癸紝閮芥槸瀵规煇涓狪O浜嬩欢鐨勪簨浠堕€氱煡(鍗冲憡璇夋煇涓ā鍧楋紝杩欎釜IO鎿嶄綔鍙互杩涜鎴栧凡缁忓畬鎴?銆傚湪缁撴瀯
涓婏紝涓よ€呬篃鏈夌浉鍚岀偣锛歞emultiplexor璐熻矗鎻愪氦IO鎿嶄綔(寮傛)銆佹煡璇㈣澶囨槸鍚﹀彲鎿嶄綔(鍚屾)锛岀劧鍚庡綋鏉′欢婊¤冻鏃讹紝灏卞洖璋僪andler銆?
涓嶅悓鐐瑰湪浜庯紝寮傛鎯呭喌涓?Proactor)锛屽綋鍥炶皟handler鏃讹紝琛ㄧずIO鎿嶄綔宸茬粡瀹屾垚锛涘悓姝ユ儏鍐典笅(Reactor)锛屽洖璋僪andler鏃讹紝琛ㄧず
IO璁惧鍙互杩涜鏌愪釜鎿嶄綔(can read or can write)锛宧andler杩欎釜鏃跺€欏紑濮嬫彁浜ゆ搷浣溿€?
Reactor閫氳繃鏌愮鍙樺舰锛屽彲浠ュ皢鍏舵敼瑁呬负Proactor锛屽湪鏌愪簺涓嶆敮鎸佸紓姝O鐨勭郴缁熶笂锛屼篃鍙互闅愯棌搴曞眰鐨勫疄鐜帮紝鍒╀簬缂栧啓璺ㄥ钩鍙?
浠g爜銆傛垜浠彧闇€瑕佸湪dispatch(涔熷氨鏄痙emultiplexor)涓皝瑁呭悓姝O鎿嶄綔鐨勪唬鐮侊紝鍦ㄤ笂灞傦紝鐢ㄦ埛鎻愪氦鑷繁鐨勭紦鍐插尯鍒拌繖涓€灞傦紝
杩欎竴灞傛鏌ュ埌璁惧鍙搷浣滄椂锛屼笉鍍忓師鏉ョ珛鍗冲洖璋僪andler锛岃€屾槸寮€濮婭O鎿嶄綔锛岀劧鍚庡皢鎿嶄綔缁撴灉鏀惧埌鐢ㄦ埛缂撳啿鍖?璇?锛岀劧鍚庡啀
鍥炶皟handler銆傝繖鏍凤紝瀵逛簬涓婂眰handler鑰岃█锛屽氨鍍忔槸proactor涓€鏍枫€傝缁嗘妧娉曞弬瑙佽繖绡囨枃绔犮€?
鍏跺疄灏辫璁℃ā寮忚€岃█锛屾垜涓汉瑙夊緱鏌愪釜妯″紡鍏跺疄鏄病鏈夊畬鍏ㄥ浐瀹氱殑缁撴瀯鐨勩€備笉鑳借鏌愪釜妯″紡閲屽氨鑲畾浼氭湁鏌愪釜绫伙紝绫讳箣闂寸殑
鍏崇郴灏辫偗瀹氭槸杩欐牱銆傚湪瀹為檯鍐欑▼搴忚繃绋嬩腑涔熷緢灏戝幓鐗瑰埆鍦板疄鐜版煇涓ā寮忥紝鍙兘璇存ā寮忎細缁欎綘鏇村鏇村ソ鐨勬灦鏋勬柟妗堛€?
鏈€杩戝湪鐪媠pserver鐨勪唬鐮侊紝鐪嬪埌鍒汉鎻愬悇绉嶅苟鍙戠郴缁熶腑鐨勬ā寮忥紝鏈夌偣鐪肩孩锛屼簬鏄墠鏉ユ壂鎵洸銆傜煡閬撲粈涔堟槸leader follower妯″紡锛?
reactor, proactor锛宮ultiplexing锛屽浜庡績涓殑閭d釜缃戠粶搴撲篃瓒婃潵瓒婃竻鏅般€?
鏈€杩戣繕骞蹭簡浜涚璋辩殑浜嬶紝鍐欎簡浼犺涓殑瀛楄妭娴佺紪鐮侊紝鐢ㄦā鏉跨殑鏂瑰紡瀹炵幇锛屼笉浣嗕繚鎸佷簡鎵╁睍鎬э紝杩樺皯鍐欏緢澶氫唬鐮侊紱澶勪簬鏁堢巼鑰冭檻锛?
鍐欎簡涓猻tatic array瀹瑰櫒(鍏跺疄灏辨槸template <typename _Tp, std::size_t size> class static_array { _Tp _con[size])锛?
鍔犱簡iterator锛岄伒寰猄TL鏍囧噯锛屽彲浠ョ粨鍚堣繘STL鐨勫悇涓猤eneric algorithm鐢紝鑷垜鎰熻涓嶉敊銆傚熀纭€妯″潡鎼缓瀹屾瘯锛岃В鏋愪簡鍏徃
鏈嶅姟鍣ㄧ綉缁滄ā鍧楃殑娑堟伅锛屾垜鏄笉鏄湡鐨勬墦绠楃敤鑷繁鐨勭綉缁滄ā鍧楅噸鍐欐垜鐨勯獙璇佹湇鍔″櫒锛熷湪鍙︿竴涓粰鍏徃鍐欑殑宸ュ叿閲岋紝鍥犱负瀹炲湪鍘屾伓
瓒婃潵瓒婂鐨勯噸澶嶄唬鐮侊紝绱㈡€у啓浜嗗嚑涓畯锛岃繕鐪熺殑鍋氬埌浜嗕唬鐮佺殑鑷姩鐢熸垚:D銆?
瀵逛紭闆呬唬鐮佺殑杩芥眰鐪熺殑鎴愪簡绉嶇櫀濂? = =|
最新技术文章: