I want to install my app which contains regestration form whose username and password is stored im sqlite database. after copyiny .apk file my apps does not contains any database table for that work.
So my question is how to import this database in my app which is installed on android device it is working fine on emulator.
Have you initialized database class object
EditDatabase db=new EditDatabase(this);
onCreate or onResume activity?
if not then initialize it.
Update from logcat output
Based on the logcat output, it shows that this is an unhandled exception. i.e. it happens outside the try{} ... catch{} block of the onClick(View) method.
This means that it is a problem in one of these 2 lines:
unname = username1.getText().toString();
storePassword1 = db.getdata(unname);
My feeling is that username1 is null because it is not referenced in your layout XML file.
Here's how you should check - replace those 2 lines with these null checks:
if (unname != null)
{
unname = username1.getText().toString();
}
else
{
Log.d("MyTag", "unname is null");
}
if (storePassword1 != null)
{
storePassword1 = db.getdata(unname);
}
else
{
Log.d("MyTag", "storePassword1 is null");
}
if (unname == null || storePassword1 == null)
{
return;
}
Run the code again, check the logcat output again, and see if it tells you about the problem.
Also try not to use System.out(String) - rather use the Log.d(String, String) methods. These are more useful on Android.
Original
Firstly, please provide a stack trace - it will show where the null pointer error happened.
For Android, the most useful way of doing that will be to use the adb logcat terminal command. This outputs the internal log of your Android device/emulator to the screen, so you can view what went wrong.
In your catch{} block, I would put the following line:
Log.d("MyTag", "Stack Trace of exception...", e);
This will output the text, and information about the error e to this log - I think it will include the stack trace.
Copy the lines starting with "MyTag" and paste them into your question.
Secondly, without the stack trace, confirm that these 3 variables are not null typically:
username1
db
password (if the app actually crashed with the NullPointerException then this one is not the problem, because the exception would be handled by the catch{} block)
i.e. confirm that you have initialised all of them before the click event.
Related
I'm trying to add a BackdoorMethod to a Xamarin.Forms application to bypass the login (IDP - opened in chrome browser) step. I have the feeling that the method is not getting triggered, but not sure, and I don't know how could I make sure about it.
I've read the documentation here: https://learn.microsoft.com/en-us/appcenter/test-cloud/uitest/working-with-backdoors
Check this thread: https://forums.xamarin.com/discussion/85821/xamarin-uitest-backdoor-on-droid-with-splash-screen-how-do-i-access-my-mainactivity
Checked this example: https://github.com/brminnick/UITestSampleApp/tree/master/Src
In the MainActivity.cs file I've defined the BackdoorMethod:
[Preserve, Export(nameof(BypassLoginScreen))]
public string BypassLoginScreen()
{
// some additional code here. the code is working, when I called it
// directly from OnCreate it was executed without any error
return "called";
}
From the test case i'm trying to invoke it like:
public constructorMethod(Platform platform)
{
this.platform = platform;
app = AppInitializer.StartApp(platform);
var result = app.Invoke("BypassLoginScreen"); // result == "<VOID>"
}
I'm not getting any error message, the method simply not called, or not returns anything. (or i don't know what's happening there with it, because breakpoint also not working as the app started from the device)
This should be already working. I have similar code and it works for me.
you can add inside your function
Android.Util.Log.WriteLine(Android.Util.LogPriority.Info, "BypassLoginScreen", $"Some text as info");
And observe the result in Device Logs, Filter by BypassLoginScreen to see if there is any log created with the tag BypassLoginScreen
I have written some simple on button click code. When I run my application and click on the button, it gives the following Error:
Unfortunately, MyApplication has stopped.
Code under the button is following:
public void onClick(View v) {
if (v == mCapture) {
try {
CaptureFingerPrint();
RegisterFingerprint();
} catch (Throwable e) {
mTextViewResult.setText(e.toString());
}
}
}
I actually don't want to close my application accidentally and display error in label as i done in exception part of the above code which does not restrict my application to close.
Important to mention that i does not face this error in android studio. I face it when APK installed and run directly on mobile,
Please anyone guide how to restrict my application to close on button click and displaying error in label. The same i done in c# without any issue.
Without posting the stacktrace, the only reason I see that can make your app crash is mTextViewResult being null or e.toString() providing a null response. Was mTextViewResult properly identified inside the layout? If yes, try replacing e.toString() with "" + e.getMessage().
It would be best, though, if you found and checked the stack trace.
I have created an application that extensively requires user inputs and interaction and even though I have made sure that I test and catch every possible case that might throw an error I want to be able to create a mechanism that traces the error in case my application crashes on the field.
I want to be able to record the entire flow right from a button click till whatever the user might be selecting or the navigation between the pages in a log file such that in case my application crashes I'm able to study the trace file later and know exactly where the error occurred.
I'm very new to this sort of programming and therefore any pointers on the above will be very helpful! Thank you in advance :]
PS: I'm not even sure whether what im referring to will be correctly called a "log trace" or not so any edit is welcome. :)
EDIT : I also want to be able to save the error report generated and send it to a particular id (similar to 'send an error report to xyz).
UPDATE :
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try {
File myFiles = new File("/sdcard/ScanApp");
if(!myFiles.exists())
{
myFiles.mkdirs();
}
File myFile = new File("sdcard/ScanApp/log.txt");
myFile.createNewFile();
myFile.delete();
myFile.createNewFile();
String cmd = "logcat -d -v time -f "+myFile.getAbsolutePath()+ " -s ActivityManager:V";
Runtime.getRuntime().exec(cmd);
Logs.this.finish();
}
catch (Exception e)
{
flag=1;
error=e.getMessage();
}
I used this in a previous application for recording any application activity and make a textfile and save it to the SD card, but the contents weren't exactly what I was looking for. Is the solution im looking for something along these lines?
Here, check for the link for reference.
In here you create a class say ExceptionHandler that implements java.lang.Thread.UncaughtExceptionHandler..
Inside this class you will do your life saving stuff like creating stacktrace and gettin ready to upload error report etc....
Now comes the important part i.e. How to catch that exception.
Though it is very simple. Copy following line of code in your each Activity just after the call of super method in your overriden onCreate method.
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));
Your Activity may look something like this…
public class ForceClose extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));
setContentView(R.layout.main);
}
}
Hope this helps...
You need to look up on Exception Handling. That is when your application crashes or any other app level errors occur, the code in the exception block executes. So in that place, log that error in a text-file and which solves your "log trace" issue.
Refer the link for beautiful examples.
I am stuck writing some code that uses reflection that calls IConnectivityManager.startLegacyVpn
The error I get is java.lang.SecurityException: Unauthorized Caller
Looking through the android source I see this is the code hanging me up:
if (Binder.getCallingUid() != Process.SYSTEM_UID) { raise the above exception }
My question is if I root my AVD and install my app in system/app will this be sufficient to get around this error?
If so, any tips on how to do this (every time I try to move my apk to the system/app folder it says the app is not installed when I click on the app icon.
Thanks!
I have the same problem, following android 4.01 open source, i see somethings like this:
public synchronized LegacyVpnInfo getLegacyVpnInfo() {
// Only system user can call this method.
if (Binder.getCallingUid() != Process.SYSTEM_UID) {
throw new SecurityException("Unauthorized Caller");
}
return (mLegacyVpnRunner == null) ? null : mLegacyVpnRunner.getInfo();
}
Or,
// Only system user can revoke a package.
if (Binder.getCallingUid() != Process.SYSTEM_UID) {
throw new SecurityException("Unauthorized Caller");
}
Or,
public void protect(ParcelFileDescriptor socket, String interfaze) throws Exception {
PackageManager pm = mContext.getPackageManager();
ApplicationInfo app = pm.getApplicationInfo(mPackage, 0);
if (Binder.getCallingUid() != app.uid) {
throw new SecurityException("Unauthorized Caller");
}
jniProtect(socket.getFd(), interfaze);
}
However, these block of code above is belongs to com.android.server.connectivity.Vpn
(class Vpn), which is not defined in interface IConnectivityManager.
I also find in startLegacyVpnInfo() function but i can't see anything involve exception
"Unauthorized Caller", so i wonder why startLegacyVpnInfo() function throws this exception?
Any solutions for this?
I am trying to make the same calls. So far I can confirm that rooting the device and copying the apk to /system/app does not work, it does not start under the system uid.
Also, this does not work:
Field uidField = Process.class.getDeclaredField("SYSTEM_UID");
uidField.setAccessible(true);
uidField.set(null, Process.myUid());
Those calls succeed, but they don't seem to affect the SYSTEM_UID field, the field is probably optimized out at compile time.
If you include android: sharedUserId="android.uid.system" into your manifest tag (not just the manifest), this should then run the application as system. This should now let you run the code.
As for pushing to /system/app, you need to run adb root followed by adb remount. This will now let you push to /system/app.
I am new to android world.
I have made an application of a user registration. It was working fine. but when i tried to add a spinner to my activity file, it was showing an error in avd, like,
The Application Registration (Process com.students) has stopped
unexpectedly. please try again
comes.
and my log cat is showing the error
"11-12 10:42:06.816: E/dalvikvm(313): Unable to open stack trace file
'/data/anr/traces.txt': Permission denied"
What is actually that error? How can i get rid of that?
You are trying to access the external storage. Make sure you have necessary permission defined in your Manifest file. This can be done by adding
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
This error has nothing to do with your app. Its referring stack trace file that gets generated when your app crashes so the user can report it to the publisher. The reason you are seeing that error is that your app was not installed through the android market so it does not have permission to write to that file. Errors that your app is generating during debug can be seen in LogCat and a stack trace will be dumped there describing the error.
This was a problem I had when I was new to Android. Then I learned that the message about being unable to write to traces.txt was not the actual problem with the program.
The solution to this problem is thus to find and correct the actual (unrelated to this message) reason the program is crashing. Then this message (which reflects a configuration problem in the crash reporting system) will no longer occur.
The operation on /data/anr/traces.txt need root or system user chmod.
Ref ActivityManagerService#dumpStackTraces code:
public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids,
ProcessCpuTracker processCpuTracker, SparseArray<Boolean> lastPids, String[] nativeProcs) {
String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
if (tracesPath == null || tracesPath.length() == 0) {
return null;
}
File tracesFile = new File(tracesPath);
try {
if (clearTraces && tracesFile.exists()) tracesFile.delete();
tracesFile.createNewFile();
FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
} catch (IOException e) {
Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e);
return null;
}
dumpStackTraces(tracesPath, firstPids, processCpuTracker, lastPids, nativeProcs);
return tracesFile;
}
And I handle the problem in the shell as following.
adb root
adb shell touch /data/anr/traces.txt
adb shell kill -3 ${APP_PID}