APP目前大概分
登入頁面->首頁->功能頁面
功能頁面會引用使用者打開APP開始輸入的帳號內容
新增功能傳送參數(如會員id)或是
照引用的帳號決定要顯示的內容(顯示會員id資料)
之前都是再登入介面抓值後跳頁面
s_id =(EditText)findViewById(R.id.number);//帳號
final String str = s_id.getText().toString().trim();//抓帳號值的字串
因為s_id並沒有儲存
倘若從功能介面上一頁再下一頁進去(重複幾次)
str抓的值就不見了,同時因為內容是空值引發錯誤導致系統停止回應
//Logcat檢察的錯誤
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
嘗試用SharedPreferences儲存字串str.....
重複進入頁面和傳遞參數的次數變多了但還是有會跳出的風險
//登入頁面
package com.skypan.school;
import android.........
public class MainActivity extends AppCompatActivity {
public static EditText s_id;//帳號
public static EditText s_birth;//密碼
public static TextView Login_text;//登入失敗顯示
Button btn1;
String url="http://‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Login_text =(TextView)findViewById(R.id.login_text);
s_id =(EditText)findViewById(R.id.number);
s_birth =(EditText)findViewById(R.id.password);
final String str = s_id.getText().toString().trim();
//字串str抓取輸入帳號的值
btn1 = (Button) findViewById(R.id.btn_login);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String o=DBphp_login.DBstring
(s_id.getText().toString()
,s_birth.getText().toString()
,url);
switch(o) {
case "GO"://帳密正確進入下一頁,並儲存字串str
SharedPreferences pref = getSharedPreferences
("test", MODE_PRIVATE);
pref.edit()
.putString("USER", str)
.commit();
Intent intent=
new Intent(MainActivity.this,Studenthome.class);
startActivity(intent);
break;
default:
Login_text.setText("error");
break;
//帳密不正確,字串上顯示錯誤
}
}
});
}
}
//引用儲存的字串
final String userid = getSharedPreferences("test", MODE_PRIVATE)
.getString("USER", "");
實際測試抓到的仍是空值.......且出現了別的錯誤
顯式終止方法“關閉”未調用的情況
介面repair71行出錯
package com.skypan.school;
import ........
public class repair extends AppCompatActivity {
public static TextView Last;
String orderSting;//職位
String url1="http:/‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧/";
ListView LV1;
String cookieStr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_repair);
//嘗試讀取SharedPreferences儲存的資料
final String userid = getSharedPreferences
("test", MODE_PRIVATE).getString("USER", "");
Log.v("log_tag=",userid);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
Handler myHandler = new Handler();
myHandler.postDelayed(runTimerStop, 2000);
if(cookieStr!=null){
myHandler.removeCallbacks(runTimerStop);
}
String r = DBstring.DB1("select * from text1");//匯出資料
TextView TV=(TextView) findViewById(R.id.Last);
int integer = Integer.valueOf(r);
if(integer > 0)
{
orderSting ="1";
}else{
orderSting = "0";
}
LV1 = (ListView) findViewById(R.id.LV1);
//新增資料:↓新增按鈕功能
Button b1 = (Button) findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] et0= new String[]{userid,orderSting};//
Inphp.Interting(et0,url1);//
}
});
}
private Runnable runTimerStop=new Runnable() {
@Override
public void run() {
select(null);
}
};
//顯示表單資料
public void select(String id) {
try {
String r = DBphp.DBstring(id, url1);
JSONArray jsonArray = new JSONArray(r);
List<Map<String, Object>> items =
new ArrayList<Map<String, Object>>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonData = jsonArray.getJSONObject(i);
Map<String, Object> item = new HashMap<String, Object>();
item.put("date", jsonData.getString("date"));//日期
item.put("time",jsonData.getString("time"));//時段
item.put("state",jsonData.getString("state"));//職務
item.put("place",jsonData.getString("place"));//地點
items.add(item);// 新增到items
}
//在此輸入ListView自訂畫面,本教學為list_text.xml檔案
SimpleAdapter SA = new SimpleAdapter(this, items,
R.layout.list_text, new String[]{"date","time","state","place"}
, new int[]{R.id.text0, R.id.text1, R.id.text2, R.id.text3});
LV1.setAdapter(SA);
} catch (Exception e) {
Log.e("log_tag=", e.toString());
}
}
}
設定連結的DBstring檔28行
package com.skypan.school.ui;
import
//此Java檔案做連結
public class DBstring {
public static String DB1(String i){
String result="";
try {
HttpClient HC=new DefaultHttpClient();
HttpPost HP =new
HttpPost("http://192.192.125.197/ysp105s02/TTEST/Last.php");
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("S1",i));
HP.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse HR=HC.execute(HP);
result= EntityUtils.toString(HR.getEntity(),HTTP.UTF_8);
}catch (Exception e){
Log.i("錯誤訊息",e.toString());
}
return result;
}
}
不知道是不是因為SharedPreferences用的不對才會引發的錯誤......
再加上repair介面的button按下傳遞參數後,userid傳遞的是空值
貌似也沒儲存到的樣子.......
SharedPreferences
儲存:
SharedPreferences myrecord=getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor edit=myrecord.edit();
edit.putString("acc", acc.getText().toString());
edit.commit();
使用:
SharedPreferences myrecord=getPreferences(Activity.MODE_PRIVATE);
String name_str=myrecord.getString("acc", "");
acc.setText(name_str);
供參考
儲存前是不是要去驗證一下,該值是不是為空,是不是你要的值、型態?
目前開了個簡單的專案測試
有成功抓到值
但原先的專案還是只能抓到空值
語法檢察很多次但看不出原因orz
原頁面
switch(o) {
case "GO":
SharedPreferences pref =
getSharedPreferences("test", MODE_PRIVATE);
pref.edit()
.putString("USER", str)
.commit();
Intent intent=new
Intent(MainActivity.this,Studenthome.class);
startActivity(intent);
break;
default:
Login_text.setText("error");
break;
}
抓取值的頁面
final String userid = getSharedPreferences
("test", MODE_PRIVATE)
.getString("USER", "");
a = (TextView) findViewById(R.id.aaa);
a.setText(userid);
資料庫換了ip位置後(今天才知道orz)
抓取的值可以顯示了......