我正在开发一个图书馆管理系统。我的应用程序有登录活动。该活动连接到MYSQL数据库,该数据库在登录成功后打开OpenProfile.java。这包含用于书籍详细信息的各种标签,例如用户名,详细信息,book1等,这些标签也可以从MYSQL数据库中获取。我希望OpenProfile活动自动将用户名的值作为登录活动的共享首选项,并发布该特定用户的书籍详细信息。我无法执行此操作。
OpenProfile.java
package com.example.test;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class OpenProfile extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// testing on Emulator:
private static final String READ_PROFILE_URL = "http://crshaggy.byethost7.com/webservice/profile.php";
// JSON IDS:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(OpenProfile.this);
private final String TAG_USERNAME = sp.getString("username", "anon");
private static final String TAG_SUCCESS = "success";
private static final String TAG_BOOK1 = "book1";
private static final String TAG_BOOK2 = "book2";
private static final String TAG_BOOK3 = "book3";
private static final String TAG_DETAILS = "DETAILS";
private static final String TAG_FINE = "fine";
private static final String TAG_POSTS = "posts";
private static final String TAG_RETURN_DATE = "return_date";
// An array of all of our comments
private JSONArray mComments = null;
// manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// note that use read_comments.xml instead of our single_post.xml
setContentView(R.layout.open_profile);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// loading the comments via AsyncTask
new LoadComments().execute();
}
/**
* Retrieves recent post data from the server.
*/
public void updateJSONdata() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_PROFILE_URL);
try {
mComments = json.getJSONArray(TAG_POSTS);
// looping through all posts according to the json object returned
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
// gets the content of each tag
String username = c.getString(TAG_USERNAME);
String details = c.getString(TAG_DETAILS);
String book1 = c.getString(TAG_BOOK1);
String book2 = c.getString(TAG_BOOK2);
String book3 = c.getString(TAG_BOOK3);
String fine = c.getString(TAG_FINE);
String return_date = c.getString(TAG_RETURN_DATE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_USERNAME, username);
map.put(TAG_DETAILS, details);
map.put(TAG_BOOK1, book1);
map.put(TAG_BOOK2, book2);
map.put(TAG_BOOK3, book3);
map.put(TAG_FINE, fine);
map.put(TAG_RETURN_DATE, return_date);
// adding HashList to ArrayList
mCommentList.add(map);
// annndddd, our JSON data is up to date same with our array
// list
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* Inserts the parsed data into the listview.
*/
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.open_profile, new String[] { TAG_USERNAME, TAG_DETAILS,
TAG_BOOK1, TAG_BOOK2, TAG_BOOK3, TAG_FINE, TAG_RETURN_DATE }, new int[] { R.id.username, R.id.details,
R.id.book1, R.id.book2, R.id.book3, R.id.fine, R.id.return_date, });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(OpenProfile.this);
pDialog.setMessage("Loading Comments...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
}
}
}
这是书籍详细信息的PHP文件 的 profile.php
<?php
require("config.inc.php");
$username=$_GET['username'];
//initial query
$query = "Select username, details, book1, book2, book3, fine, return_date from user_details WHERE username='$username'";
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
// $response["message"] = "Database Error!";
$ex->getMessage();
die("Failed to run query: " . $ex->getMessage());
// die(json_encode($response));
}
$rows = $stmt->fetchAll();
if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["username"] = $row["username"];
$post["details"] = $row["details"];
$post["book1"] = $row["book1"];
$post["book2"] = $row["book2"];
$post["book3"] = $row["book3"];
$post["fine"] = $row["fine"];
$post["return_date"] = $row["return_date"];
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}
?>
错误记录
08-07 09:14:06.018: E/AndroidRuntime(1121): FATAL EXCEPTION: main
08-07 09:14:06.018: E/AndroidRuntime(1121): Process: com.example.test, PID: 1121
08-07 09:14:06.018: E/AndroidRuntime(1121): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.OpenProfile}: java.lang.NullPointerException
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.os.Looper.loop(Looper.java:136)
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-07 09:14:06.018: E/AndroidRuntime(1121): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 09:14:06.018: E/AndroidRuntime(1121): at java.lang.reflect.Method.invoke(Method.java:515)
08-07 09:14:06.018: E/AndroidRuntime(1121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-07 09:14:06.018: E/AndroidRuntime(1121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-07 09:14:06.018: E/AndroidRuntime(1121): at dalvik.system.NativeStart.main(Native Method)
08-07 09:14:06.018: E/AndroidRuntime(1121): Caused by: java.lang.NullPointerException
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:374)
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
08-07 09:14:06.018: E/AndroidRuntime(1121): at com.example.test.OpenProfile.<init>(OpenProfile.java:45)
08-07 09:14:06.018: E/AndroidRuntime(1121): at java.lang.Class.newInstanceImpl(Native Method)
08-07 09:14:06.018: E/AndroidRuntime(1121): at java.lang.Class.newInstance(Class.java:1208)
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-07 09:14:06.018: E/AndroidRuntime(1121): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
答案 0 :(得分:0)
在onCreate()
之前调用onResume()
,O。不会知道“OpenProfile.this
”
因此,声明将使用onCreate()
中的其余变量和初始化...如:
...
SharedPreferences sp;
private final String TAG_USERNAME;
// manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// note that use read_comments.xml instead of our single_post.xml
setContentView(R.layout.open_profile);
sp = PreferenceManager.getDefaultSharedPreferences(OpenProfile.this);
TAG_USERNAME = sp.getString("username", "anon");
...
答案 1 :(得分:0)
在sp
初始化TAG_USERNAME
和onCreate
,并且不要TAG_USERNAME
final
属性:
SharedPreferences sp;
private String TAG_USERNAME;
private static final String TAG_SUCCESS = "success";
private static String READ_PROFILE_URL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// note that use read_comments.xml instead of our single_post.xml
setContentView(R.layout.open_profile);
sp = PreferenceManager.getDefaultSharedPreferences(OpenProfile.this);
TAG_USERNAME = sp.getString("username", "anon");
Log.d("mylog", "TAG_USERNAME = " + TAG_USERNAME );
READ_PROFILE_URL = String.format("http://crshaggy.byethost7.com/webservice/profile.php?username=%s", TAG_USERNAME);
Log.d("mylog", "READ_PROFILE_URL = " + READ_PROFILE_URL );
// loading the comments via AsyncTask
new LoadComments().execute();
}