我正在开发运行良好的Android应用。但问题是当我导航到我的设备中的另一个应用程序时,我当前的应用程序进入后台。一段时间后,当我恢复该应用程序时,它崩溃了。我没有看到任何问题。请帮帮我。
我的日志如下
01-13 15:02:38.520: E/AndroidRuntime(15875): FATAL EXCEPTION: main
01-13 15:02:38.520: E/AndroidRuntime(15875):java.lang.RuntimeException:Unable to start activity ComponentInfo{shoppy.online.com/shoppy.online.com.HomeFragment}: java.lang.NullPointerException
01-13 15:02:38.520: E/AndroidRuntime(15875):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
01-13 15:02:38.520: E/AndroidRuntime(15875):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
01-13 15:02:38.520: E/AndroidRuntime(15875):at android.app.ActivityThread.access$600(ActivityThread.java:174)
01-13 15:02:38.520: E/AndroidRuntime(15875): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
01-13 15:02:38.520: E/AndroidRuntime(15875): at android.os.Handler.dispatchMessage(Handler.java:107)
01-13 15:02:38.520: E/AndroidRuntime(15875): at android.os.Looper.loop(Looper.java:194)
01-13 15:02:38.520: E/AndroidRuntime(15875): at android.app.ActivityThread.main(ActivityThread.java:5409)
01-13 15:02:38.520: E/AndroidRuntime(15875): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 15:02:38.520: E/AndroidRuntime(15875): at java.lang.reflect.Method.invoke(Method.java:525)
01-13 15:02:38.520: E/AndroidRuntime(15875): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-13 15:02:38.520: E/AndroidRuntime(15875): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
01-13 15:02:38.520: E/AndroidRuntime(15875): at dalvik.system.NativeStart.main(Native Method)
01-13 15:02:38.520: E/AndroidRuntime(15875): Caused by: java.lang.NullPointerException
01-13 15:02:38.520: E/AndroidRuntime(15875): at shoppy.online.com.cart_db.Cart_DB.getAllItems(Cart_DB.java:39)
01-13 15:02:38.520: E/AndroidRuntime(15875): at shoppy.online.com.MainActivity.getCartData(MainActivity.java:677)
01-13 15:02:38.520: E/AndroidRuntime(15875): at shoppy.online.com.MainActivity.onCreate(MainActivity.java:103)
01-13 15:02:38.520: E/AndroidRuntime(15875): at shoppy.online.com.HomeFragment.onCreate(HomeFragment.java:112)
01-13 15:02:38.520: E/AndroidRuntime(15875): at android.app.Activity.performCreate(Activity.java:5122)
01-13 15:02:38.520: E/AndroidRuntime(15875): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
01-13 15:02:38.520: E/AndroidRuntime(15875): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
01-13 15:02:38.520: E/AndroidRuntime(15875): ... 11 more
在NullPointerException
Caet_DB
上说package shoppy.online.com.cart_db;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class Cart_DB {
public static SQLiteDatabase mDatabase;
public static String CREATE_CART_TABLE;
public static String CART_TABLE = "cart";
public static Cart_model[] cartData;
public static void CreateAllToDoTable() {
createTodoTable();
}
private static void createTodoTable() {
try {
Cart_DB.CREATE_CART_TABLE = "CREATE TABLE IF NOT EXISTS "
+ Cart_DB.CART_TABLE + "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "productID TEXT," + "productName TEXT,"
+ "productPrice TEXT," + "productSellingPrice TEXT,"
+ "productDiscount TEXT," + "productQty TEXT,"
+ "shippingCharge TEXT," + "productDeliDays TEXT,"
+ "productCode TEXT," + "storeName TEXT,"
+ "productAvgRates TEXT," + "productImage TEXT,"
+ "productSlug TEXT," + "finalQty TEXT,"
+ "finalShip TEXT," + "productColorID TEXT,"
+ "productColor TEXT," + "productSizeID TEXT,"
+ "productSize TEXT," + "productStock TEXT" + ")";
Cart_DB.mDatabase.execSQL(Cart_DB.CREATE_CART_TABLE);
} catch (Exception e) {
}
}
public static Cart_model[] getAllItems() {
Cursor c = mDatabase.query(CART_TABLE, null, null, null, null, null,
null);
cartData = new Cart_model[c.getCount()];
c.moveToFirst();
int i = 0;
while (c.isAfterLast() == false) {
cartData[i] = new Cart_model();
cartData[i].setID(c.getInt(0));
cartData[i].setProductID(c.getString(1));
cartData[i].setProductName(c.getString(2));
cartData[i].setProductPrice(c.getString(3));
cartData[i].setProductSellingPrice(c.getString(4));
cartData[i].setProductDiscount(c.getString(5));
cartData[i].setProductQty(c.getString(6));
cartData[i].setShippingCharge(c.getString(7));
cartData[i].setProductDeliDays(c.getString(8));
cartData[i].setProductCode(c.getString(9));
cartData[i].setStoreName(c.getString(10));
cartData[i].setProductAvgRates(c.getString(11));
cartData[i].setProductImage(c.getString(12));
cartData[i].setProductSlug(c.getString(13));
cartData[i].setFinalQty(c.getString(14));
cartData[i].setFinalShip(c.getString(15));
cartData[i].setProductColorID(c.getString(16));
cartData[i].setProductColor(c.getString(17));
cartData[i].setProductSizeID(c.getString(18));
cartData[i].setProductSize(c.getString(19));
cartData[i].setProductStock(c.getString(20));
i++;
c.moveToNext();
}
return cartData;
}
public static void insertAllCart(String productID, String productName,
String productPrice, String productSellingPrice,
String productDiscount, String productQty, String shippingCharge,
String productDeliDays, String productCode, String storeName,
String productAvgRates, String productImage, String productSlug,
String finalQty, String finalShip, String productColorID,
String productColor, String productSizeID, String productSize,
String productStock) {
ContentValues val = new ContentValues();
val.put("productID", productID);
val.put("productName", productName);
val.put("productPrice", productPrice);
val.put("productSellingPrice", productSellingPrice);
val.put("productDiscount", productDiscount);
val.put("productQty", productQty);
val.put("shippingCharge", shippingCharge);
val.put("productDeliDays", productDeliDays);
val.put("productCode", productCode);
val.put("storeName", storeName);
val.put("productAvgRates", productAvgRates);
val.put("productImage", productImage);
val.put("productSlug", productSlug);
val.put("finalQty", finalQty);
val.put("finalShip", finalShip);
val.put("productColorID", productColorID);
val.put("productColor", productColor);
val.put("productSizeID", productSizeID);
val.put("productSize", productSize);
val.put("productStock", productStock);
@SuppressWarnings("unused")
long value = mDatabase.insert(CART_TABLE, null, val);
}
public static void updateCartData(Cart_model c) {
ContentValues value = new ContentValues();
value.put("productID", c.productID);
value.put("productName", c.productName);
value.put("productPrice", c.productPrice);
value.put("productSellingPrice", c.productSellingPrice);
value.put("productDiscount", c.productDiscount);
value.put("productQty", c.productQty);
value.put("shippingCharge", c.shippingCharge);
value.put("productDeliDays", c.productDeliDays);
value.put("productCode", c.productCode);
value.put("storeName", c.storeName);
value.put("productAvgRates", c.productAvgRates);
value.put("productImage", c.productImage);
value.put("productSlug", c.productSlug);
value.put("finalQty", c.finalQty);
value.put("finalShip", c.finalShip);
value.put("productColorID", c.productColorID);
value.put("productColor", c.productColor);
value.put("productSizeID", c.productSizeID);
value.put("productSize", c.productSize);
value.put("productStock", c.productStock);
mDatabase.update(CART_TABLE, value, "id = " + c.ID, null);
}
public static void deleteCartData(Cart_model d) {
mDatabase.delete(CART_TABLE, "id = " + d.ID, null);
}
}
这是我的班级。
public class MainActivity extends AppCompatActivity implements OnClickListener {
protected FrameLayout frameLayout;
protected DrawerLayout mDrawerLayout;
protected ListView mDrawerList;
protected ActionBarDrawerToggle mDrawerToggle;
protected static int position;
protected static boolean isLaunch = true;
Toolbar toolbar;
RelativeLayout prof_head;
protected TextView profile_name, track_order;
protected TextView login;
TextView contact_us;
TextView faqs;
TextView about_us;
TextView tvChar;
protected RoundedImageView img;
protected FrameLayout fam;
private CharSequence mTitle;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
SharedPreferences sharedPreferences;
protected String MyPREFERENCES = "MyPrefs";
public static String name, pass, uname, pImg;
public static String id;
public static ArrayList<Cart_model> listdata;
public static ArrayList<Wishlist_model> listdata1;
Cart_model[] data;
Bundle kek;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kek = savedInstanceState;
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
frameLayout = (FrameLayout) findViewById(R.id.frame_container);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
toolbar = (Toolbar) findViewById(R.id.toolbar_headers);
setSupportActionBar(toolbar);
getCartData();
getWishData();
LayoutInflater inflater = getLayoutInflater();
View listHeaderView = inflater.inflate(R.layout.profile_header, null,
false);
View listFooterView = inflater
.inflate(R.layout.nav_footer, null, false);
mDrawerList.addHeaderView(listHeaderView);
mDrawerList.addFooterView(listFooterView);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons
.getResourceId(3, -1), true, "22"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons
.getResourceId(4, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons
.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons
.getResourceId(6, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons
.getResourceId(7, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons
.getResourceId(8, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[9], navMenuIcons
.getResourceId(9, -1), true, "22"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[10], navMenuIcons
.getResourceId(10, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[11], navMenuIcons
.getResourceId(11, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[12], navMenuIcons
.getResourceId(12, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[13], navMenuIcons
.getResourceId(13, -1), true, "50+"));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
displayView(position);
}
});
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerList.setItemChecked(1, true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.app_name) {
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
// mDrawerToggle.setDrawerIndicatorEnabled(false);
if (isLaunch) {
isLaunch = false;
displayView(1);
}
sharedPreferences = getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);
id = sharedPreferences.getString("id", null);
name = sharedPreferences.getString("email", null);
uname = sharedPreferences.getString("name", null);
pass = sharedPreferences.getString("password", null);
pImg = sharedPreferences.getString("profileImg", null);
prof_head = (RelativeLayout) findViewById(R.id.prof_head);
prof_head.setOnClickListener(this);
profile_name = (TextView) findViewById(R.id.profile_name);
img = (RoundedImageView) findViewById(R.id.roundedImage);
fam = (FrameLayout) findViewById(R.id.flProfil);
tvChar = (TextView) findViewById(R.id.tvChar);
track_order = (TextView) findViewById(R.id.track_order);
track_order.setOnClickListener(this);
login = (TextView) findViewById(R.id.login);
login.setOnClickListener(this);
contact_us = (TextView) findViewById(R.id.contact_us);
contact_us.setOnClickListener(this);
faqs = (TextView) findViewById(R.id.faqs);
faqs.setOnClickListener(this);
about_us = (TextView) findViewById(R.id.about_us);
about_us.setOnClickListener(this);
int status = 0;
int resourceId = getResources().getIdentifier("status_bar_height",
"dimen", "android");
if (resourceId > 0) {
status = getResources().getDimensionPixelSize(resourceId);
}
float scale = getResources().getDisplayMetrics().density;
int paddingdp = (int) (16 * scale + 0.5f);
int heightdp = 0;
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
heightdp = (int) (200 * scale + 0.5f);
} else {
heightdp = (int) (130 * scale + 0.5f);
}
// int statusbar = (int) (25 * scale + 0.5f);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
toolbar.setPadding(0, status, 0, 0);
// listHeaderView.setPadding(32, 84, 32, 32);
prof_head.setPadding(paddingdp, paddingdp + status, paddingdp,
paddingdp);
LayoutParams params = (LayoutParams) prof_head.getLayoutParams();
params.height = heightdp + status;
prof_head.setLayoutParams(params);
} else {
}
if (name.equals("user_email")) {
login.setText("Login");
profile_name.setText("Welcome User");
img.setVisibility(View.VISIBLE);
img.setImageResource(R.drawable.images);
fam.setVisibility(View.GONE);
} else if (!name.equals("user_email")) {
login.setText("Logout");
profile_name.setText(name);
if (pImg.equals("")) {
fam.setVisibility(View.VISIBLE);
img.setVisibility(View.GONE);
String tag = (String) name.subSequence(0, 1);
tvChar.setText(tag);
} else {
Picasso.with(getApplicationContext()).load(pImg).into(img);
}
}
}
MainActivity
string DeviceName = Phone.Tag("Name").Value();
string csvFile = @"Afile.csv";
var result = File
.ReadLines(csvFile) // You have no need to read all the lines
.Skip(1) // skip title, comment out this if CSV has no caption
.Select(line => line.Split(',')) // providing that CSV doen't have quotations
.Select(items => items[0]) // providing that 1ast column contains names
.Any(name => String.Equals(name, DeviceName, StringComparison.OrdinalIgnoreCase));
答案 0 :(得分:0)
经过大量调试后终于解决了这个问题。正如@fasteque指出我在我的首发屏幕中发起了mDatabase
。
所以我在MainActivity
做了一些小改动,现在它的工作就像魅力一样。
以下是我的更改
if (Cart_DB.mDatabase == null) {
Cart_DB.mDatabase = openOrCreateDatabase("CART",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
}
if (Wishlist_DB.mDatabase == null) {
Wishlist_DB.mDatabase = openOrCreateDatabase("WISHLIST",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
}