Home > Mobile >  Why is my code appear such mistakes I MainActicity oncreate method invokes the Util deleteall method
Why is my code appear such mistakes I MainActicity oncreate method invokes the Util deleteall method

Time:09-24

Public class Util extends Application {
Public void deleteall () {

243 SQLiteDBUtil db2=new SQLiteDBUtil (getApplicationContext ());
SQLiteDatabase sd2=db2
GetWritableDatabase ();
String SQL="delete from the user";
String sql2="delete from kebiao";
String sql3="delete from note";
Sd2. ExecSQL (SQL);
Sd2. ExecSQL (sql2);
Sd2. ExecSQL (sql3);
//here close
The close ();
}
}
Public class MainActivity extends FragmentActivity implements an OnClickListener {

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RequestWindowFeature (Window. FEATURE_NO_TITLE);
The setContentView (R.l ayout. Activity_main);

Util Util=new Util ();
Util. Deleteall ();
}
}
E/AndroidRuntime: FATAL EXCEPTION: the main
Process: com. Example. Easytablemore, PID: 8427
Java. Lang. RuntimeException: Unable to start the activity ComponentInfo {com. Example. Easytablemore/com. Example. Easytablemore. MainActivity} : Java. Lang. NullPointerException: Attempt to invoke virtual method 'android. The content, the Context android app. Activity. GetApplicationContext ()' on a null object reference
. An android app. ActivityThread. PerformLaunchActivity (ActivityThread. Java: 3270)
. An android app. ActivityThread. HandleLaunchActivity (ActivityThread. Java: 3409)
An android. App. Servertransaction. LaunchActivityItem. Execute (LaunchActivityItem. Java: 83)
An android. App. Servertransaction. TransactionExecutor. ExecuteCallbacks (TransactionExecutor. Java: 135)
An android. App. Servertransaction. TransactionExecutor. Execute (TransactionExecutor. Java: 95)
An android. App. ActivityThread $H.h andleMessage (ActivityThread. Java: 2016)
An android. OS. Handler. DispatchMessage (Handler. Java: 107)
. An android OS. Stars. Loop (214). Which Java:
. An android app. ActivityThread. Main (7356) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
At com. Android. Internal. OS. RuntimeInit $MethodAndArgsCaller. Run (492) RuntimeInit. Java:
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 930)
Under Caused by: Java. Lang. NullPointerException: Attempt to invoke virtual method 'android. The content. The Context. Android app. The Activity. The getApplicationContext ()' on a null object reference
The at Util. Util. Deleteall (Util. Java: 243)
At com. Example. Easytablemore. MainActivity. OnCreate (MainActivity. Java: 84)
. An android app. Activity. PerformCreate (7802) Activity. Java:
. An android app. Activity. PerformCreate (7791) Activity. Java:
. An android app. Instrumentation. CallActivityOnCreate (1299) Instrumentation. Java:
. An android app. ActivityThread. PerformLaunchActivity (ActivityThread. Java: 3245)
. An android app. ActivityThread. HandleLaunchActivity (ActivityThread. Java: 3409)
An android. App. Servertransaction. LaunchActivityItem. Execute (LaunchActivityItem. Java: 83)
An android. App. Servertransaction. TransactionExecutor. ExecuteCallbacks (TransactionExecutor. Java: 135)
An android. App. Servertransaction. TransactionExecutor. Execute (TransactionExecutor. Java: 95)
An android. App. ActivityThread $H.h andleMessage (ActivityThread. Java: 2016)
An android. OS. Handler. DispatchMessage (Handler. Java: 107)
. An android OS. Stars. Loop (214). Which Java:
. An android app. ActivityThread. Main (7356) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
At com. Android. Internal. OS. RuntimeInit $MethodAndArgsCaller. Run (492) RuntimeInit. Java:
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 930)
The Process of 8427 terminated.

CodePudding user response:

The null pointer, in MainActivity84, context is empty

CodePudding user response:

It's strange that I in oncreate directly execute deleteall this code is not submitted to the wrong, in the oncreate call deleteall this method will be an error

CodePudding user response:

You the util is an application, you directly in the util oncreate execute the deleteall method is not to go

CodePudding user response:

Which has a new application?
Which have utils to inherit the application?
Add into and preach a context to go, and extends the application

CodePudding user response:

Ok, I have time to try the method you above

CodePudding user response:

Above all, thank you

CodePudding user response:

Public class Util {
Public void deleteall Context (Context) {

SQLiteDBUtil db2=new SQLiteDBUtil (context));
SQLiteDatabase sd2=db2
GetWritableDatabase ();
String SQL="delete from the user";
String sql2="delete from kebiao";
String sql3="delete from note";
Sd2. ExecSQL (SQL);
Sd2. ExecSQL (sql2);
Sd2. ExecSQL (sql3);
//here close
The close ();
}
}
Your Activity invokes the Util. When deleteall (this);
  • Related