这是Home类。它还会给出致命异常和线程退出,带有未捕获的异常和空指针异常。在启动画面后,我的应用程序很快停止了。
package com.example.resproject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.getSet.Resgetset;
import com.example.getSet.Restgetset1;
import com.example.util.AlertDialogManager;
import com.example.util.ConnectionDetector;
import com.example.util.GPSTracker;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.drawable.TransitionDrawable;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Home extends Activity {
ListView list_detail;
Context context = this;
int MAINPOSITION = 0;
DrawerLayout mDrawerLayout;
EditText edit_search, edt_nameuser, edt_usermail, edt_comment,
edt_enameuser, edt_emailuser;
public static ArrayList<Resgetset> rest;
ArrayList<Restgetset1> rest1;
ArrayList<Resgetset> restmap;
static double d;
static double miles;
GPSTracker gps;
static double latitude;
static double longitude;
String rating, search;
Float rate = 0.0f;
LinearLayout lldisplay;
Button btn_fvrts;
int pos;
RelativeLayout rl_user_dialog, rl_add_dialog;
public static final String MY_PREFS_NAME = "Restaurant";
// ListView represents Navigation Drawer
ListView mDrawerList;
String username, usermail, createusername, createusermail;
String updatename, updateemail;
ActionBarDrawerToggle mDrawerToggle;
Button btn_more, btn_map, btn_more1;
EditText edt_name, edt_mail, createnameuser, createemailuser;
ProgressDialog progressDialog;
String foodtype;
String user2, uservalue;
View layout12;
RelativeLayout rl_home, rl_update, rl_create;
String input, map;
String emailpattern;
LinearLayout ll_data;
String value;
Boolean isInternetPresent = false;
ConnectionDetector cd;
private String Error = null;
InterstitialAd mInterstitialAd;
boolean interstitialCanceled;
AlertDialogManager alert = new AlertDialogManager();
int MainPosition = 0;
RelativeLayout rl_dialoguser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_home);
if(getActionBar()!=null){
getActionBar().hide();
}
cd = new ConnectionDetector(getApplicationContext());
drawer();
initialize();
buttonclick();
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
if (getString(R.string.Home_banner).equals("yes")) {
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
} else {
AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.setVisibility(View.GONE);
}
gps = new GPSTracker(Home.this);
// check if GPS enabled
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
// \n is for new line
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
emailpattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
// initialize
Intent iv = getIntent();
foodtype = iv.getStringExtra("foodname");
map = iv.getStringExtra("map");
Log.d("map", "" + map);
// check data is category page or not
if (foodtype != null) {
new getrestaudetail1().execute();
} else {
new getrestaudetail().execute();
}
try {
rating = String.valueOf(rate);
} catch (NumberFormatException e) {
}
rating = "3";
edit_search = (EditText) findViewById(R.id.edit_search);
// search on home page method
edit_search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
search = s.toString();
if (search.equals("")) {
new getrestaudetail().execute();
Log.d("search", "" + search);
} else {
new getrestaudetail1().execute();
}
}
});
} else {
RelativeLayout rl_back = (RelativeLayout) findViewById(R.id.rl_back);
if (rl_back == null) {
Log.d("second", "second");
rl_dialoguser = (RelativeLayout) findViewById(R.id.rl_infodialog);
layout12 = getLayoutInflater().inflate(
R.layout.connectiondialog, rl_dialoguser, false);
rl_dialoguser.addView(layout12);
rl_dialoguser.startAnimation(AnimationUtils.loadAnimation(
Home.this, R.anim.popup));
Button btn_yes = (Button) layout12.findViewById(R.id.btn_yes);
btn_yes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
rl_dialoguser.setVisibility(View.GONE);
}
});
}
}
}
private void drawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_launcher, R.string.drawer_open,
R.string.drawer_close) {
@Override
public void onDrawerClosed(View view) {
// getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
// getActionBar().setTitle("Select a river");
invalidateOptionsMenu();
}
};
// Setting DrawerToggle on DrawerLayout
mDrawerLayout.setDrawerListener(mDrawerToggle);
// all linear layout from slider menu
LinearLayout ll_home = (LinearLayout) findViewById(R.id.ll_home);
ll_home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mDrawerLayout.closeDrawer(GravityCompat.START);
mDrawerLayout.setVisibility(View.INVISIBLE);
// new changes
Intent iv = new Intent(Home.this, Home.class);
startActivity(iv);
}
});
// category button click
LinearLayout ll_cat = (LinearLayout) findViewById(R.id.ll_cat);
ll_cat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_more1.setVisibility(View.INVISIBLE);
btn_more.setVisibility(View.VISIBLE);
mDrawerLayout.setVisibility(View.INVISIBLE);
mDrawerLayout.closeDrawer(GravityCompat.START);
Intent iv = new Intent(getApplicationContext(), Category.class);
startActivity(iv);
}
});
// favourite button click
LinearLayout ll_fvrt = (LinearLayout) findViewById(R.id.ll_fav);
ll_fvrt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_more1.setVisibility(View.INVISIBLE);
btn_more.setVisibility(View.VISIBLE);
mDrawerLayout.setVisibility(View.INVISIBLE);
mDrawerLayout.closeDrawer(GravityCompat.START);
Intent iv = new Intent(getApplicationContext(), Favourite.class);
startActivity(iv);
}
});
LinearLayout ll_special = (LinearLayout) findViewById(R.id.ll_special);
ll_special.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_more1.setVisibility(View.INVISIBLE);
btn_more.setVisibility(View.VISIBLE);
mDrawerLayout.setVisibility(View.INVISIBLE);
mDrawerLayout.closeDrawer(GravityCompat.START);
Intent iv = new Intent(getApplicationContext(), Offer.class);
startActivity(iv);
}
});
LinearLayout ll_about = (LinearLayout) findViewById(R.id.ll_about);
ll_about.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_more1.setVisibility(View.INVISIBLE);
btn_more.setVisibility(View.VISIBLE);
mDrawerLayout.setVisibility(View.INVISIBLE);
mDrawerLayout.closeDrawer(GravityCompat.START);
Intent iv = new Intent(getApplicationContext(), About.class);
startActivity(iv);
}
});
LinearLayout ll_social = (LinearLayout) findViewById(R.id.ll_social);
ll_social.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_more1.setVisibility(View.INVISIBLE);
btn_more.setVisibility(View.VISIBLE);
mDrawerLayout.setVisibility(View.INVISIBLE);
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
share.putExtra(Intent.EXTRA_SUBJECT, "Restaurant");
share.putExtra(
Intent.EXTRA_TEXT,
"https://play.google.com/store/apps/details?id="
+ Home.this.getPackageName()
+ "\n"
+ "The great advantage of a restaurant is that it's a refuge from home life. ");
startActivity(Intent.createChooser(share, "Share Link!"));
}
});
LinearLayout ll_terms = (LinearLayout) findViewById(R.id.ll_term);
ll_terms.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_more1.setVisibility(View.INVISIBLE);
btn_more.setVisibility(View.VISIBLE);
mDrawerLayout.setVisibility(View.INVISIBLE);
mDrawerLayout.closeDrawer(GravityCompat.START);
Intent iv = new Intent(getApplicationContext(),
TermCondition.class);
startActivity(iv);
}
});
LinearLayout ll_profile = (LinearLayout) findViewById(R.id.ll_profile);
ll_profile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_more1.setVisibility(View.INVISIBLE);
btn_more.setVisibility(View.VISIBLE);
list_detail.setEnabled(false);
// create view
layout12 = v;
mDrawerLayout.closeDrawer(GravityCompat.START);
mDrawerLayout.setVisibility(View.INVISIBLE);
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME,
MODE_PRIVATE);
// check user is created or not
if (prefs.getString("score", null) != null) {
uservalue = prefs.getString("score", null);
Log.d("user3", uservalue);
if (prefs.getString("username", null) != null
&& prefs.getString("usermailid", null) != null) {
Log.d("first", "first");
RelativeLayout rl_back = (RelativeLayout) findViewById(R.id.rl_back);
if (rl_back == null) {
Log.d("second", "second");
RelativeLayout rl_dialoguser = (RelativeLayout) findViewById(R.id.rl_infodialog);
layout12 = getLayoutInflater().inflate(
R.layout.update_dialog, rl_dialoguser,
false);
rl_dialoguser.addView(layout12);
ImageView img_back = (ImageView) findViewById(R.id.img_back);
rl_dialoguser.setAlpha(1);
img_back.setAlpha(0.8f);
edt_enameuser = (EditText) layout12
.findViewById(R.id.edt_enameuser);
edt_emailuser = (EditText) layout12
.findViewById(R.id.edt_emailuser);
username = prefs.getString("username", null);
Log.d("user3", username);
edt_enameuser.setText(username);
usermail = prefs.getString("usermailid", null);
edt_emailuser.setText(usermail);
// updateuser button click
Button btn_updateuser = (Button) layout12
.findViewById(R.id.btn_updateuser);
btn_updateuser
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// rl_adddialog.setVisibility(View.GONE);
layout12 = v;
list_detail.setEnabled(true);
updatename = edt_enameuser
.getText().toString()
.replace(" ", "%20");
updateemail = edt_emailuser
.getText().toString();
if (updateemail
.matches(emailpattern)
&& updateemail.length() > 0) {
if (username.equals("")) {
edt_nameuser
.setError("Enter User Name");
} else {
SharedPreferences.Editor editor = getSharedPreferences(
MY_PREFS_NAME,
MODE_PRIVATE)
.edit();
editor.putString(
"username",
"" + updatename);
editor.putString(
"usermailid",
"" + updateemail);
editor.commit();
new getupdatedetail()
.execute();
AlertDialog.Builder builder = new AlertDialog.Builder(
Home.this);
builder.setMessage(
"Successfully updated your profile")
.setTitle("Updated");
builder.setNeutralButton(
android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = builder
.create();
alert.show();
// rl_user_dialog.setVisibility(View.GONE);
mDrawerLayout
.setVisibility(View.INVISIBLE);
View myView = findViewById(R.id.rl_back);
ViewGroup parent = (ViewGroup) myView
.getParent();
parent.removeView(myView);
ImageView img_back = (ImageView) findViewById(R.id.img_back);
}
} else {
edt_emailuser
.setError("Enter Valid Email");
}
// img_back.setAlpha(0.0f);
// new getreviewdetail().execute();
}
});
// cancel button click
Button btn_cancel = (Button) layout12
.findViewById(R.id.btn_canceluser);
btn_cancel
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list_detail.setEnabled(true);
mDrawerLayout
.setVisibility(View.INVISIBLE);
ImageView img_back = (ImageView) findViewById(R.id.img_back);
img_back.setAlpha(0.0f);
View myView = findViewById(R.id.rl_back);
ViewGroup parent = (ViewGroup) myView
.getParent();
parent.removeView(myView);
}
});
}
} else {
}
} else {
mDrawerLayout.setVisibility(View.INVISIBLE);
RelativeLayout rl_back = (RelativeLayout) findViewById(R.id.rl_back);
if (rl_back == null) {
RelativeLayout rl_dialog = (RelativeLayout) findViewById(R.id.rl_infodialog);
layout12 = getLayoutInflater().inflate(
R.layout.dialog_create, rl_dialog, false);
rl_dialog.addView(layout12);
ImageView img_back = (ImageView) findViewById(R.id.img_back);
img_back.setAlpha(0.8f);
rl_dialog.setAlpha(1);
edt_nameuser = (EditText) layout12
.findViewById(R.id.edt_name);
edt_usermail = (EditText) layout12
.findViewById(R.id.edt_email12);
Button btn_usersubmit = (Button) layout12
.findViewById(R.id.btn_submituser);
btn_usersubmit
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list_detail.setEnabled(true);
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
createusername = edt_nameuser.getText()
.toString();
createusermail = edt_usermail.getText()
.toString();
if (createusermail
.matches(emailPattern)
&& createusermail.length() > 0) {
if (createusername.equals("")) {
edt_nameuser
.setError("Enter User Name");
} else {
SharedPreferences.Editor editor = getSharedPreferences(
MY_PREFS_NAME,
MODE_PRIVATE).edit();
editor.putString("username", ""
+ createusername);
editor.putString("usermailid",
"" + createusermail);
editor.commit();
new getuserdetail().execute();
// new changes
AlertDialog.Builder builder = new AlertDialog.Builder(
Home.this);
builder.setMessage(
"Successfully created your profile")
.setTitle("Create");
builder.setNeutralButton(
android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = builder
.create();
alert.show();
// complete
View myView = findViewById(R.id.rl_back);
ViewGroup parent = (ViewGroup) myView
.getParent();
parent.removeView(myView);
ImageView img_back = (ImageView) findViewById(R.id.img_back);
}
} else {
edt_usermail
.setError("Enter valid email address");
}
// img_back.setAlpha(0.0f);
}
});
// new changes
Button btn_cancel = (Button) layout12
.findViewById(R.id.btn_canceluser1);
btn_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list_detail.setEnabled(true);
mDrawerLayout.setVisibility(View.INVISIBLE);
ImageView img_back = (ImageView) findViewById(R.id.img_back);
img_back.setAlpha(0.0f);
View myView = findViewById(R.id.rl_back);
ViewGroup parent = (ViewGroup) myView
.getParent();
parent.removeView(myView);
}
});
}
}
}
});
}
private void buttonclick() {
// TODO Auto-generated method stub
btn_map.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iv = new Intent(Home.this, Gmap.class);
iv.putExtra("map", "yes");
startActivity(iv);
overridePendingTransition(R.anim.rotate_in, R.anim.rotate_out);
// finish();
}
});
btn_more = (Button) findViewById(R.id.img_more);
btn_more1 = (Button) findViewById(R.id.img_more1);
btn_more1.setVisibility(View.INVISIBLE);
// drawer open
btn_more.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_more1.setVisibility(View.VISIBLE);
btn_more.setVisibility(View.INVISIBLE);
mDrawerLayout.setVisibility(View.VISIBLE);
mDrawerLayout.openDrawer(Gravity.LEFT);
}
});
// close drawer
btn_more1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// mDrawerLayout.setVisibility(View.VISIBLE);
btn_more1.setVisibility(View.INVISIBLE);
btn_more.setVisibility(View.VISIBLE);
mDrawerLayout.closeDrawer(Gravity.LEFT);
mDrawerLayout.setVisibility(View.INVISIBLE);
}
});
}
private void initialize() {
// TODO Auto-generated method stub
rl_home = (RelativeLayout) findViewById(R.id.rl_home);
rest = new ArrayList<Resgetset>();
rest1 = new ArrayList<Restgetset1>();
list_detail = (ListView) findViewById(R.id.list_detail);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setVisibility(View.INVISIBLE);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
// btn_fvrts = (Button) findViewById(R.id.btn_fvrts);
btn_map = (Button) findViewById(R.id.btn_map);
} }
此代码无效..
04-04 03:21:38.758: E/AndroidRuntime(2311): FATAL EXCEPTION: main
04-04 03:21:38.758: E/AndroidRuntime(2311): Process: com.example.resproject, PID: 2311
04-04 03:21:38.758: E/AndroidRuntime(2311): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.resproject/com.example.resproject.Home}: java.lang.NullPointerException
04-04 03:21:38.758: E/AndroidRuntime(2311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
04-04 03:21:38.758: E/AndroidRuntime(2311): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
04-04 03:21:38.758: E/AndroidRuntime(2311): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-04 03:21:38.758: E/AndroidRuntime(2311): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-04 03:21:38.758: E/AndroidRuntime(2311): at android.os.Handler.dispatchMessage(Handler.java:102)
04-04 03:21:38.758: E/AndroidRuntime(2311): at android.os.Looper.loop(Looper.java:136)
04-04 03:21:38.758: E/AndroidRuntime(2311): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 03:21:38.758: E/AndroidRuntime(2311): at java.lang.reflect.Method.invoke(Method.java:515)
04-04 03:21:38.758: E/AndroidRuntime(2311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
04-04 03:21:38.758: E/AndroidRuntime(2311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-04 03:21:38.758: E/AndroidRuntime(2311): at dalvik.system.NativeStart.main(Native Method)
04-04 03:21:38.758: E/AndroidRuntime(2311): Caused by: java.lang.NullPointerException
04-04 03:21:38.758: E/AndroidRuntime(2311): at com.example.resproject.Home.drawer(Home.java:285)
04-04 03:21:38.758: E/AndroidRuntime(2311): at com.example.resproject.Home.onCreate(Home.java:146)
04-04 03:21:38.758: E/AndroidRuntime(2311): at android.app.Activity.performCreate(Activity.java:5231)
04-04 03:21:38.758: E/AndroidRuntime(2311): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-04 03:21:38.758: E/AndroidRuntime(2311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
答案 0 :(得分:0)
mDrawerLayout
方法中调用mDrawerLayout.setDrawerListener(mDrawerToggle)
之前,您的变量drawer()
尚未初始化。这可能会导致NullPointerException。
您应该在initialize()
方法的drawer()
之前致电onCreate()
。