เราจะเรียกใช้ shared preferences ในอีกหน้านึงยังไงครับ (มันเออเรอร์)
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา เราจะเรียกใช้ shared preferences ในอีกหน้านึงยังไงครับ (มันเออเรอร์)
เราจะเรียกใช้ shared preferences ในอีกหน้านึงยังไงครับ (มันเออเรอร์)
Copy
หน้าที่ทำการเก็บข้อมูล public class LoginActivity extends AppCompatActivity { public static String username_A = null; SharedPreferences sp = getSharedPreferences("DATA", Context.MODE_PRIVATE); SharedPreferences.Editor dataUser = sp.edit(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); Button logIn = (Button) findViewById(R.id.logIn); logIn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //เข้าเมื่อกดปุ่ม Log in EditText username = (EditText) findViewById(R.id.username); EditText password = (EditText) findViewById(R.id.password); username_A = username.getText().toString(); String password_A = password.getText().toString(); //Bypass login // Intent intentID = new Intent(getBaseContext(), Main2Activity.class); // intentID.putExtra("ID", "5635512010"); // startActivity(intentID); new checkLogInStd().execute(username_A, password_A); //เรียกใช้คลาสที่ร้องขอข้อมูล // new checkLogInAdmin().execute(username_A, password_A); } }); } public class checkLogInStd extends AsyncTask<String, Void, Integer> //การทำงานหลังบ้าน { private String test; @Override protected void onPreExecute() { //ทำก่อนเข้า doIn } @Override protected Integer doInBackground(String... params) { //การดึงข้อมูลมาจาก server RequestBody body = new FormBody.Builder() .add("username_A", params[0]) .add("password_A", params[1]) .build(); OkHttpClient okh = new OkHttpClient(); //ใช้ OkHttp ติดต่อกับ server Request requested = new Request.Builder().url("http://psuclub.esy.es/PSUClubApp/checkLoginStd.php").post(body).build(); String data = null; try { Response response = okh.newCall(requested).execute(); //ตัวเเก็บทุกอย่างที่ส่งกลับมาจาก server Log.d("AAA", response.body().toString()); data = response.body().string(); return Integer.valueOf(data); } catch (IOException e) { e.printStackTrace(); } Log.d("BBB", "testBBB"); return 0; } @Override protected void onProgressUpdate(Void... values) { //ทำระหว่างที่ doIn ทำงาน } @Override protected void onPostExecute(Integer s) { //ทำหลังจากที่ doIn ทำงานเสร็จ Log.d("CCC", s.toString()); if (Integer.valueOf(s) == 1) { Log.d("DDD", "testDDD"); dataUser.putString("dataUsername", username_A); dataUser.commit(); Intent intentID = new Intent(getBaseContext(), MainActivity.class); Bundle bundle = new Bundle(); bundle.putString("ID", username_A); intentID.putExtras(bundle); startActivity(intentID); } else if (Integer.valueOf(s) == 0) { AlertDialog.Builder builder1 = new AlertDialog.Builder(LoginActivity.this); //pop up เด้งเมื่อ login failed builder1.setTitle("Login failed"); builder1.setMessage("Unable to login. Please try again"); builder1.setCancelable(true); AlertDialog alert11 = builder1.create(); alert11.show(); } } }
หน้าที่ทำการอ่านข้อมูล
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_show_club, container, false); listView = (ListView)view.findViewById(R.id.listShowClub); SharedPreferences sp = getSharedPreferences("dataUser", Context.MODE_PRIVATE); String username_A = sp.getString("dataUsername", null); new load().execute(username_A); return view; } ปัญหาคือ มันเออเรอร์ ตรงตอนอ่านข้อมูล ที่ตัวหนังสือสีเเดงอ่ะครับ ต้องเเก้ยังไงครับ หรือว่าผมเขียนรูปแบบอะไรตรงไหนผิดหรือเปล่าครับ

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา
โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ