我正在使用 json 将我的应用程序连接到本地服务器。在我的应用程序中,用户可以登录,并且登录的用户可以看到来自不同网站的评论,例如 google+、yahoo,这些评论存储在本地服务器的数据库中。一切正常,但唯一的问题是在我尝试再次启动我的应用程序时重新启动我的 android 设备后,它向我显示一条消息“不幸的是,您的应用程序已停止”并且我的应用程序正在关闭。
这是我的 logcat 错误:
03-18 15:06:39.747: E/AndroidRuntime(1182): FATAL EXCEPTION: main
03-18 15:06:39.747: E/AndroidRuntime(1182): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.DashboardActivity}: java.lang.NullPointerException
03-18 15:06:39.747: E/AndroidRuntime(1182): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-18 15:06:39.747: E/AndroidRuntime(1182): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-18 15:06:39.747: E/AndroidRuntime(1182): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-18 15:06:39.747: E/AndroidRuntime(1182): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-18 15:06:39.747: E/AndroidRuntime(1182): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 15:06:39.747: E/AndroidRuntime(1182): at android.os.Looper.loop(Looper.java:137)
03-18 15:06:39.747: E/AndroidRuntime(1182): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-18 15:06:39.747: E/AndroidRuntime(1182): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 15:06:39.747: E/AndroidRuntime(1182): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 15:06:39.747: E/AndroidRuntime(1182): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-18 15:06:39.747: E/AndroidRuntime(1182): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-18 15:06:39.747: E/AndroidRuntime(1182): at dalvik.system.NativeStart.main(Native Method)
03-18 15:06:39.747: E/AndroidRuntime(1182): Caused by: java.lang.NullPointerException
03-18 15:06:39.747: E/AndroidRuntime(1182): at com.example.androidhive.DashboardActivity.onCreate(DashboardActivity.java:127)
03-18 15:06:39.747: E/AndroidRuntime(1182): at android.app.Activity.performCreate(Activity.java:5008)
03-18 15:06:39.747: E/AndroidRuntime(1182): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-18 15:06:39.747: E/AndroidRuntime(1182): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-18 15:06:39.747: E/AndroidRuntime(1182): ... 11 more
我的dashboardActivity 类是:
公共类 DashboardActivity 扩展 ListActivity {
ListView lv;
ListAdapter adapter;
ProgressDialog pDialog;
UserFunctions userFunctions;
Button btnLogout;
Spinner clientsSpinner;
Spinner sitesSpinner;
Button txFB;
Button txTW;
TextView title;
static String dealer = "dealer_rater";
static String google = "google_places";
static String twitter = "Twitter";
static String facebook = "Facebook";
static int incr;
static int no;
static int review_length;
static int length = 90;
int review_profile_id;
JSONArray review;
JSONArray mentions;
JSONArray profile;
int x = 0;
ArrayAdapter<String> heightUnitAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check login status in database
userFunctions = new UserFunctions();
if (userFunctions.isUserLoggedIn(getApplicationContext())) {
setContentView(R.layout.dashboard);
btnLogout = (Button) findViewById(R.id.btnLogout);
clientsSpinner = (Spinner) findViewById(R.id.spinnerTitle1);
sitesSpinner = (Spinner) findViewById(R.id.spinnerTitle2);
txFB = (Button) findViewById(R.id.btnFB);
txTW = (Button) findViewById(R.id.btnTW);
profile = LoginActivity.profiles;
title = (TextView) findViewById(R.id.title);
Typeface myTypeface = Typeface.createFromAsset(this.getAssets(),
"ArchivoNarrow-Regular.ttf");
btnLogout.setTypeface(myTypeface);
title.setTypeface(myTypeface);
String[] heightunit = new String[10];
heightunit[0] = "All Reviews";
heightunit[1] = "Dealer Rate";
heightunit[2] = "Google+ Local";
heightUnitAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, heightunit);
sitesSpinner.setAdapter(heightUnitAdapter);
ArrayAdapter<String> clientadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item);
for (int i = -1; i < profile.length()+1; i++) {
try {
x++;
if(i==-1){
// array_spinner[i+1] = "All Profiles";
clientadapter.add("All Profiles");
}else {
JSONObject c = profile.getJSONObject(i);
String client_name = c
.getString(LoginActivity.TAG_REVIEWS_PRIOFILE_NAME);
clientadapter.add(client_name);
// array_spinner[i+1] = client_name;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
final String[] str = new String[] {
LoginActivity.TAG_REDIRECT,
LoginActivity.TAG_URL,
LoginActivity.TAG_AUTHOR,
LoginActivity.TAG_RATING,
LoginActivity.TAG_DATE,
LoginActivity.TAG_REVIEW };
final int[] place = new int[] {R.id.link, R.id.urlImage,R.id.author, R.id.rating,
R.id.date, R.id.review, };
// clientadapter.setDropDownViewResource(R.layout.spinner_item);
clientsSpinner.setAdapter(clientadapter);
clientsSpinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,
View view, int i, long arg3) {
int color = getResources().getColor(R.drawable.white);
((TextView) arg0.getChildAt(0)).setTextColor(color);
Typeface myTypeface = Typeface.createFromAsset(getApplicationContext().getAssets(),
"ArchivoNarrow-Regular.ttf");
((TextView) arg0.getChildAt(0)).setTypeface(myTypeface);
sitesSpinner.setSelection(0);
String selectedClientName = (String) clientsSpinner
.getSelectedItem();
for (int k = 0; k < profile.length(); k++) {
// Storing each json item in variable
try {
JSONObject c = profile.getJSONObject(k);
String client_name = c
.getString(LoginActivity.TAG_REVIEWS_PRIOFILE_NAME);
if (selectedClientName.equals(client_name)) {
review_profile_id = Integer.parseInt(c
.getString(LoginActivity.TAG_REVIEWS_PRIOFILE_ID));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
review = LoginActivity.reviews;
ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
// RatingBar ratingBar = (RatingBar)
// findViewById(R.id.rating);
for (int j = 0; j < review.length(); j++) {
try {
JSONObject c = review.getJSONObject(j);
{
HashMap<String, String> map = new HashMap<String, String>();
int review_profile_id2 = Integer.parseInt(c
.getString(LoginActivity.TAG_REVIEWS_PRIOFILE_ID));
if (review_profile_id == review_profile_id2) {
String url = c
.getString(LoginActivity.TAG_URL);
String author = c
.getString(LoginActivity.TAG_AUTHOR);
String author1 = Html.fromHtml(author).toString();
String review1 = c
.getString(LoginActivity.TAG_REVIEW);
String review = Html.fromHtml(review1).toString();
String date = c
.getString(LoginActivity.TAG_DATE);
String rating = c
.getString(LoginActivity.TAG_RATING);
String link_raw = c.getString(LoginActivity.TAG_REDIRECT);
String link = Html.fromHtml(link_raw).toString();
if (!review.equals("null")) {
if (url.equals(dealer)) {
map.put("review", review);
map.put("author", author);
map.put("date", date);
map.put("rating", rating);
map.put("service", link);
map.put("url", Integer.toString(dealer_image));
listItems.add(map);
} else if (url.equals(google)) {
map.put("review", review);
map.put("author", author);
map.put("date", date);
map.put("rating", rating);
map.put("service", link);
map.put("url", Integer.toString(google_image));
listItems.add(map);
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Getting adapter
adapter = new SimpleAdapter(DashboardActivity.this,
listItems, R.layout.list_item, str, place);
((SimpleAdapter) adapter)
.setViewBinder(new MyBinder());
setListAdapter(adapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sitesSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View view, int l,
long arg3) {
if (clientsSpinner.getSelectedItemPosition() == 0) {
int color = getResources().getColor(R.drawable.white);
((TextView) arg0.getChildAt(0)).setTextColor(color);
Typeface myTypeface = Typeface.createFromAsset(getApplicationContext().getAssets(),
"ArchivoNarrow-Regular.ttf");
((TextView) arg0.getChildAt(0)).setTypeface(myTypeface);
int a = sitesSpinner.getSelectedItemPosition();
ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
switch (a) {
case (0):
review = LoginActivity.reviews;
for (int i = 0; i < review.length(); i++) {
try {
JSONObject c = review
.getJSONObject(i);
{
HashMap<String, String> map = new HashMap<String, String>();
String url = c
.getString(LoginActivity.TAG_URL);
String author = c
.getString(LoginActivity.TAG_AUTHOR);
String author1 = Html.fromHtml(author).toString();
String date = c
.getString(LoginActivity.TAG_DATE);
String review1 = c
.getString(LoginActivity.TAG_REVIEW);
String review = Html.fromHtml(review1).toString();
String rating = c
.getString(LoginActivity.TAG_RATING);
String link_raw = c.getString(LoginActivity.TAG_REDIRECT);
String link = Html.fromHtml(link_raw).toString();
if (!review
.equals("null")) {
if (url.equals(dealer)) {
map.put("review",
review);
map.put("author", author);
map.put("date",
date);
map.put("rating",
rating);
map.put("service", link);
map.put("url", Integer.toString(dealer_image));
listItems
.add(map);
} else if (url
.equals(google)) {
map.put("review",
review);
map.put("author", author);
map.put("date",
date);
map.put("rating",
rating);
map.put("service", link);
map.put("url", Integer.toString(google_image));
listItems
.add(map);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Getting adapter
adapter = new SimpleAdapter(
DashboardActivity.this,
listItems,
R.layout.list_item, str,
place);
((SimpleAdapter) adapter)
.setViewBinder(new MyBinder());
setListAdapter(adapter);
break;
case (1):
review = LoginActivity.reviews;
for (int i = 0; i < review.length(); i++) {
try {
JSONObject c = review
.getJSONObject(i);
String url = c
.getString(LoginActivity.TAG_URL);
HashMap<String, String> map = new HashMap<String, String>();
{
if (url.equals(dealer)) {
String review1 = c
.getString(LoginActivity.TAG_REVIEW);
String review = Html.fromHtml(review1).toString();
String author = c
.getString(LoginActivity.TAG_AUTHOR);
String date = c
.getString(LoginActivity.TAG_DATE);
String rating = c
.getString(LoginActivity.TAG_RATING);
String link_raw = c.getString(LoginActivity.TAG_REDIRECT);
String link = Html.fromHtml(link_raw).toString();
if (!review
.equals("null")) {
map.put("review",
review);
map.put("author", author);
map.put("date",
date);
map.put("rating",
rating);
map.put("service", link);
map.put("url", Integer.toString(dealer_image));
listItems
.add(map);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Getting adapter
adapter = new SimpleAdapter(
DashboardActivity.this,
listItems,
R.layout.list_item, str,
place);
((SimpleAdapter) adapter)
.setViewBinder(new MyBinder());
setListAdapter(adapter);
break;
}
}
else{
int color = getResources().getColor(R.drawable.white);
((TextView) arg0.getChildAt(0)).setTextColor(color);
Typeface myTypeface = Typeface.createFromAsset(getApplicationContext().getAssets(),
"ArchivoNarrow-Regular.ttf");
((TextView) arg0.getChildAt(0)).setTypeface(myTypeface);
int a = sitesSpinner.getSelectedItemPosition();
ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
switch (a) {
case (0):
review = LoginActivity.reviews;
for (int i = 0; i < review.length(); i++) {
try {
JSONObject c = review
.getJSONObject(i);
int review_profile_id2 = Integer.parseInt(c
.getString(LoginActivity.TAG_REVIEWS_PRIOFILE_ID));
if (review_profile_id == review_profile_id2){
HashMap<String, String> map = new HashMap<String, String>();
String url = c
.getString(LoginActivity.TAG_URL);
String author = c
.getString(LoginActivity.TAG_AUTHOR);
String author1 = Html.fromHtml(author).toString();
String date = c
.getString(LoginActivity.TAG_DATE);
String review1 = c
.getString(LoginActivity.TAG_REVIEW);
String review = Html.fromHtml(review1).toString();
String rating = c
.getString(LoginActivity.TAG_RATING);
String link_raw = c.getString(LoginActivity.TAG_REDIRECT);
String link = Html.fromHtml(link_raw).toString();
if (!review
.equals("null")) {
if (url.equals(dealer)) {
map.put("review",
review);
map.put("author", author);
map.put("date",
date);
map.put("service", link);
map.put("rating",
rating);
map.put("url", Integer.toString(dealer_image));
listItems
.add(map);
} else if (url
.equals(google)) {
map.put("review",
review);
map.put("author", author);
map.put("date",
date);
map.put("rating",
rating);
map.put("service", link);
map.put("url", Integer.toString(google_image));
listItems
.add(map);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Getting adapter
adapter = new SimpleAdapter(
DashboardActivity.this,
listItems,
R.layout.list_item, str,
place);
((SimpleAdapter) adapter)
.setViewBinder(new MyBinder());
setListAdapter(adapter);
break;
case (1):
review = LoginActivity.reviews;
for (int i = 0; i < review.length(); i++) {
try {
JSONObject c = review
.getJSONObject(i);
String url = c
.getString(LoginActivity.TAG_URL);
HashMap<String, String> map = new HashMap<String, String>();
int review_profile_id2 = Integer.parseInt(c
.getString(LoginActivity.TAG_REVIEWS_PRIOFILE_ID));
if (review_profile_id == review_profile_id2) {
if (url.equals(dealer)) {
String review1 = c
.getString(LoginActivity.TAG_REVIEW);
String review = Html.fromHtml(review1).toString();
String author = c
.getString(LoginActivity.TAG_AUTHOR);
String date = c
.getString(LoginActivity.TAG_DATE);
String rating = c
.getString(LoginActivity.TAG_RATING);
String link_raw = c.getString(LoginActivity.TAG_REDIRECT);
String link = Html.fromHtml(link_raw).toString();
if (!review
.equals("null")) {
map.put("review",
review);
map.put("author", author);
map.put("date",
date);
map.put("rating",
rating);
map.put("service", link);
map.put("url", Integer.toString(dealer_image));
listItems
.add(map);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Getting adapter
adapter = new SimpleAdapter(
DashboardActivity.this,
listItems,
R.layout.list_item, str,
place);
((SimpleAdapter) adapter)
.setViewBinder(new MyBinder());
setListAdapter(adapter);
break;
}
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
userFunctions.logoutUser(getApplicationContext());
Intent login = new Intent(getApplicationContext(),
LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
});
} else {
// user is not logged in show login screen
Intent login = new Intent(getApplicationContext(),
LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
}
class MyBinder implements ViewBinder {
public boolean setViewValue(final View view, final Object data,
String textRepresentation) {
if (view.getId() == R.id.rating) {
String stringval = (String) data;
float ratingValue = Float.parseFloat(stringval);
RatingBar ratingBar = (RatingBar) view;
ratingBar.setRating(ratingValue);
return true;
}
if (view.getId() == R.id.date) {
TextView textView = (TextView) view;
String stringval = (String) data;
Typeface myTypeface = Typeface.createFromAsset(getApplicationContext().getAssets(),
"ArchivoNarrow-Regular.ttf");
textView.setTypeface(myTypeface);
textView.setText(stringval);
return true;
}
if (view.getId() == R.id.author) {
TextView textView = (TextView) view;
String stringval = (String) data;
Typeface myTypeface = Typeface.createFromAsset(getApplicationContext().getAssets(),
"ArchivoNarrow-Regular.ttf");
textView.setTypeface(myTypeface);
textView.setText(stringval);
return true;
}
if (view.getId() == R.id.urlImage) {
ImageButton image = (ImageButton) view;
String stringval = (String) data;
final int imageSource = Integer.parseInt(stringval);
image.setBackgroundResource(imageSource);
image.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if(imageSource == dealer_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if(imageSource == google_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if(imageSource == cars_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if(imageSource == edmunds_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if(imageSource == yelp_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if(imageSource == yahoo_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if(imageSource == judi_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if(imageSource == insider_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if(imageSource == city_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if(imageSource == facebook_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if(imageSource == twitter_image){
TextView theFact = (TextView) findViewById(R.id.link);
String shareFact = theFact.getText().toString();
Uri uri = Uri.parse(shareFact);
Log.d("ImageButton", shareFact);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
});
return true;
}
if (view.getId() == R.id.review) {
TextView textView = (TextView) view;
String stringval = (String) data;
Typeface myTypeface = Typeface.createFromAsset(getApplicationContext().getAssets(),
"ArchivoNarrow-Regular.ttf");
textView.setTypeface(myTypeface);
textView.setText(stringval);
return true;
}
return false;
}
}
}
第 127 行是:
for (int i = -1; i < profile.length()+1; i++)