I have an application that creates an Intent when selecting item menu, but the first time the user clicks this button, the app crashes with the following stack trace.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.systemsat.monitriip, PID: 17818
java.lang.NoClassDefFoundError: br.com.systemsat.monitriip.activity.ConfigurationActivity
at br.com.systemsat.monitriip.activity.MainActivity.onOptionsItemSelected(MainActivity.java:212)
at android.app.Activity.onMenuItemSelected(Activity.java:2912)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:421)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:188)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:663)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:947)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:155)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Below is my Activity declaration inside AndroidManifest.xml:
<activity
android:name=".activity.ConfigurationActivity"
android:parentActivityName=".activity.MainActivity">
</activity>
Also, below is the line that is accused to be the cause of the problem by the call stack:
Intent hashCodeIntent = new Intent(getApplicationContext(), ConfigurationActivity.class);
Some answers found in StackOverflow pointed me that clean and rebuild would do the trick but I've tried without any luck. I have also tried adding MultiDex to gradle file as well as to my custom Application class, but that also resulted in a no go.
Could someone else have any insights as to what I might have done wrong? What is really intriguing is that this error only happens on the first time the user clicks the "Configuration" screen button. After the app crashes, the user can reopen it and click the button normally.
Thank you in advance.
UPDATE
I have forgot to mention that Android Monitor also logs the following info right before the crash:
Rejecting re-init on previously-failed class java.lang.Class<br.com.systemsat.monitriip.activity.ConfigurationActivity>
UPDATE 2
Here is my onOptionsItemSelected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_config:
Intent hashCodeIntent = new Intent(this, ConfigurationActivity.class);
hashCodeIntent.putExtra("isConfigurationCall", true);
startActivity(hashCodeIntent);
break;
}
return true;
}
UPDATE 3
I have just generated an apk for testing purposes and the error did not show. Maybe it has something to do with debuging? Or even something to do with gradle version? I currently use the last version of gradle.
Try to change getAplicationContext() for getActivity()
Sometimes getAplicationContext returns null.
Related
When the orientation of app change this error is occur kindly help me
W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.explore_kpk, PID: 24892
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.explore_kpk/com.example.explore_kpk.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5389)
at android.app.ActivityThread.access$1200(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
***Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference***
***at com.example.explore_kpk.MainActivity.onCreate(MainActivity.java:121)
at android.app.Activity.performCreate(Activity.java:6877)***
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5389)
at android.app.ActivityThread.access$1200(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
I/Process: Sending signal. PID: 24892 SIG: 9
This is code in which error pointing**
at com.example.explore_kpk.MainActivity.onCreate(MainActivity.java:121)
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recyler_view);
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this, recyclerList);
long seed = System.nanoTime();
Collections.shuffle(recyclerList, new Random(seed));
GridLayoutManager gridLayoutManager=new GridLayoutManager(this, mNoOfColumns);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(recyclerViewAdapter);
Your exception is saying that you are trying to use setLayoutManager on null object reference. This means that your recylcerView is null at the moment. I don't see your whole code so I can't give you an instant solution. What I can say for you is that each time the screen orientation is changed your current activity will be destroyed and called again. So, onCreate will be called again and everything else like you just started new Activity. Sometimes issues happen here so there is few options on how to handle these events.
Retaining an object during a configuration change
What this means is for you to keep your data while screen rotation is happening. This can be done with Bundle but if you have a large set of data then it is better to use the ViewModel object. How to do this check here:
Retaining an object during a configuration change | Android Developers
ViewModel Guide
Helpful answer on SO
Handling the configuration change yourself
This other option comes with big CAUTION. Consider this as a last resort when you really need to avoid restarts because of a configuration change. What this does is preventing your activity from the restart. If you can't save your instance then I would recommend doing this but first, try to play with ViewModel and Bundles to fix this issue.
Handling the configuration change yourself | Documentation
How to do this?
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|screenLayout" />
add android:configChanges line to your AndroidManifest.xml inside your <activity/> tag.
I tried to use 2 SwitchPreference in a PreferenceActivity. At the first start of the PreferenceActivity, everything works normal; the activity gets started without problem, the settings.xml is displayed
The problems start if one attends to close the PreferenceActivity or to change the state of the SwitchPreference: everytime the app is closed with a ClassCastException. This also occurs without a set dafaultValue. After that, the app will be closed if one attempts to open the PreferenceActivity.
Until now i do not access/influence the stored values programmatically in any way, for now i just want display the settings screen for testing purposes. The activity will be started if an menu item gets clicked in the calling activity via
final Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
I tried different suggested ways stated here such as defining boolean ressources and using them to set the defaultValue of the SwitchPreferences or using the method
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
in the main activity. None of that approaches worked. How is the SwitchPreference intended to be used, if it apparently can not save its one state wihtout causing an exception? Do one need to define specific xml attributes to make the preference work?
ClassCastException
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:242)
at android.preference.Preference.getPersistedBoolean(Preference.java:1637)
at android.preference.Preference.persistBoolean(Preference.java:1608)
at android.preference.TwoStatePreference.setChecked(TwoStatePreference.java:79)
at android.preference.SwitchPreference$Listener.onCheckedChanged(SwitchPreference.java:54)
at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
at android.widget.Switch.setChecked(Switch.java:688)
at android.widget.CompoundButton.toggle(CompoundButton.java:87)
at android.widget.CompoundButton.performClick(CompoundButton.java:99)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
PreferenceActivity
public class SettingsActivity extends PreferenceActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.addPreferencesFromResource(R.xml.settings);
}
}
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
[...]
<PreferenceCategory
android:title="#string/sett_not_header">
<SwitchPreference
android:key="#+id/sett_not_on"
android:title="#string/sett_not_act_header"
android:summary="#string/sett_not_act_summ"
android:defaultValue="true"/>
<SwitchPreference
android:key="#+id/sett_not_mute"
android:title="#string/sett_not_mute_header"
android:summary="#string/sett_not_mute_summ"
android:defaultValue="true"/>
[...]
</PreferenceCategory>
</PreferenceScreen>
I was able to track down the cause of the exception:
In the field android:key i falsely used the expression #+id/in the assumption the key declaration would work the same way the id declaration works. Removing the expression #+id/allowed me to use the SwitchPreference without errors.
I have an activity, LookingForGPS, that starts another activity, Run. After starting Run, LookingForGPS doesn't finish, but rather continually updates some TextViews in Run:
Run run = new Run();
if(runHasBeenStarted)run.getAndSetValues(someParameters, this);
To my understanding, this returns the LookingForGPS activity. However, after a while (when the parameters are a certain value) I want to start a new activity, PostRun. The following method is called from the Run instance's getAndSetValues:
private void killEverythingAndProceed(Context context){
Intent finishRun = new Intent(context, PostRun.class);
//putting some extras into the intent
startActivity(finishRun);
}
I then get a NullPointerException at the line of startActivity:
11-17 08:34:33.647 2472-2472/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: lv.rv1g.kj0112.forward, PID: 2472
java.lang.NullPointerException
at android.app.Activity.startActivityForResult(Activity.java:3424)
at android.app.Activity.startActivityForResult(Activity.java:3385)
at android.app.Activity.startActivity(Activity.java:3627)
at android.app.Activity.startActivity(Activity.java:3595)
at lv.rv1g.kj0112.forward.Run.killEverythingAndProceed(Run.java:143)
at lv.rv1g.kj0112.forward.Run.getAndSetValues(Run.java:88)
at lv.rv1g.kj0112.forward.LookingForGPS.onLocationChanged(LookingForGPS.java:100)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
What am I doing horribly wrong? I guess it's my lack of knowledge about object-oriented programming that's causing this. It's not the extras causing it, and the context should be correct as well, as it works for another method from the same instance.
Edit: detailed explanation
The LookingForGPS activity has a LocationManager (not using a service because it has some limitations that are too complicated for me to work around). Each time onLocationChanged is called, the Run activity is instantiated (I check whether it has actually been started first, it gets started by the user independetly of the LocationManager), and the getAndSetValues method inside Run is called, giving a bunch of parameters. getAndSetValues then performs some calculations and updates some TextViews in the Run activity (which all works fine). A calculation is also performed to see whether a certain condition has been met. If it has been, the postRun activity is supposed to be started, which is when I run into the exception above.
Here's the problem:
Run run = new Run();
Looks like Run is an Activity. Never instantiate activities with new - you cannot use activities instantiated such way for anything you'd want to use an activity for. For example, to be used as a Context for startActivity().
Either use an Activity instance that has been set up by the system for you, or use an Intent to launch a new activity instance.
I am doing this for exit an application using Android UIAutomator.
assertTrue("Back button can't be pressed", getUiDevice().pressBack());
assertTrue("Back button can't be pressed", getUiDevice().pressBack()); // This line will giving exception.
My application will exit when we press two times back button that's why i am calling it twice.
But
Then it will give me exception.
But when i'm doing it manually back twice it will work perfectly.
Exception is:
junit.framework.AssertionFailedError: Back button can't be pressed
at com.android.jdsu.automation.youtube.YoutubeUiTest.exitApplicaion(Yout
ubeUiTest.java:487)
at com.android.jdsu.automation.youtube.YoutubeUiTest.testYouTube(Youtube
UiTest.java:208)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAuto
matorTestRunner.java:160)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutoma
torTestRunner.java:96)
at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.ja
va:91)
at com.android.commands.uiautomator.Launcher.main(Launcher.java:83)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
at dalvik.system.NativeStart.main(Native Method)
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: test=testYouTube
INSTRUMENTATION_STATUS: class=com.android.jdsu.automation.youtube.YoutubeUiTest
INSTRUMENTATION_STATUS: stack=junit.framework.AssertionFailedError: Back button
can't be pressed
at com.android.jdsu.automation.youtube.YoutubeUiTest.exitApplicaion(Yout
ubeUiTest.java:487)
at com.android.jdsu.automation.youtube.YoutubeUiTest.testYouTube(Youtube
UiTest.java:208)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAuto
matorTestRunner.java:160)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutoma
torTestRunner.java:96)
at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.ja
va:91)
at com.android.commands.uiautomator.Launcher.main(Launcher.java:83)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
at dalvik.system.NativeStart.main(Native Method)
Ya I too experienced same problem, but didn't get why this happens.
I got it working by using as follows:
UiDevice device = UiDevice.getInstance();
device.pressMenu();
This is working fine for me.
Else you can also use keyevent as follows:
Runtime.getRuntime().exec("/system/bin/input keyevent 82");
assertTrue("Back button can't be pressed", getUiDevice().pressBack());
It will return false if UI has not changed after pressBack(), if current is in home view it will return false after getUiDevice().pressBack());
This is an interesting one:
https://github.com/appium/appium-android-driver/blob/ad4aa239aa8352553325c800829202d960030d25/bootstrap/src/io/appium/android/bootstrap/handler/PressBack.java#L43
Based on this we can't trust UIDevice.pressBack(), at least if we trust Appium.
It's curious though that it works most of the time and actually returns true. In my empirical experience, it works fine with Intent Chooser, Google Drive, Google Play Store, but not with Device Settings on API 19. So so far I have this, which is more strict that Appium's solution:
#RequiresApi(VERSION_CODES.JELLY_BEAN)
public static void pressBackExternal() {
UiDevice device = UiDevice.getInstance(getInstrumentation());
if (VERSION.SDK_INT == VERSION_CODES.KITKAT
&& "com.android.settings".equals(device.getCurrentPackageName())) {
// fails because pressBack sometimes returns false even though the Settings is closed.
device.pressBack();
} else {
assertTrue("expected to press Back button", device.pressBack());
}
}
Interesting, did you try adding wait time between two calls. Your application might need some time to reload last activity. Add at least 500ms or 1s of wait time.
i am developing an android application using a new device (Samsung galaxy core) the problem is when I try to run the application and I have an error in my code like infinity loop or something like that when the application run in my device a black screen appears and i can't do nothing only restart the device and start over again I don't see the dialog ( force to close application ... ) which I used to have in my old device any help please !
thanks for you answers and sorry if i wasn't so clear actually the problem is not in my code it's in the device (i think) for example in this code i haven't declare a new activity in Androidmanifest.xml
package com.exadle.df;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button);
final Intent intent = new Intent(MainActivity.this, newj.class);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.this.startActivity(intent);
}
});
}
}
so when the app crash a black screen appears and i can't do nothing ! even as you said "Hold the Home Button for couple of seconds, when the list of running applications shows up, flip your app out." the only thing i can do is Hold power Button to restart the phone
here is the logcat if it can help
05-25 15:04:32.281 21017-21017/com.exadle.df D/dalvikvm﹕ Late-enabling CheckJNI
05-25 15:04:32.401 21017-21017/com.exadle.df D/ActivityThread﹕ setTargetHeapUtilization:0.25
05-25 15:04:32.401 21017-21017/com.exadle.df D/ActivityThread﹕ setTargetHeapIdealFree:8388608
05-25 15:04:32.401 21017-21017/com.exadle.df D/ActivityThread﹕ setTargetHeapConcurrentStart:2097152
05-25 15:04:32.871 21017-21017/com.exadle.df D/libEGL﹕ loaded /system/lib/egl/libEGL_adreno200.so
05-25 15:04:32.881 21017-21017/com.exadle.df D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_adreno200.so
05-25 15:04:32.891 21017-21017/com.exadle.df D/libEGL﹕ loaded /system/lib/egl/libGLESv2_adreno200.so
05-25 15:04:32.891 21017-21017/com.exadle.df I/Adreno200-EGL﹕ <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107_msm8625_JB_REL_2.0.3_CL3357771_release_AU (CL3357771)
Build Date: 02/25/13 Mon
Local Branch:
Remote Branch: quic/jb_rel_2.0.3
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107 + NOTHING
05-25 15:04:32.941 21017-21017/com.exadle.df D/OpenGLRenderer﹕ Enabling debug mode 0
05-25 15:04:37.531 21017-21017/com.exadle.df D/AndroidRuntime﹕ Shutting down VM
05-25 15:04:37.531 21017-21017/com.exadle.df W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41b78438)
05-25 15:04:37.541 21017-21017/com.exadle.df E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.exadle.df/com.exadle.df.newj}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1556)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1431)
at android.app.Activity.startActivityForResult(Activity.java:3429)
at android.app.Activity.startActivityForResult(Activity.java:3390)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
at android.app.Activity.startActivity(Activity.java:3600)
at android.app.Activity.startActivity(Activity.java:3568)
at com.exadle.df.MainActivity$1.onClick(MainActivity.java:25)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
i am using Android Studio and i can't use stop execution !
Without the code for your app, I can't tell why it is hanging.
If you think it is stuck in an infinite loop, you can use tools to determine roughly where the loop is. A good pace to start is with the profiling tools in the Android Development Kit, which will tell you the method where the loop is occurring.
If you show us some of the code or logcat, we may be able to help more. But, I can offer my bit of advice.
When I come across something in my apps that is not working right, or it crashes when it comes to that bit of code, I comment out a few blocks of code at a time, and find the point at which the app runs correctly. So If I had an app that crashed when a specific method was called, I would systematically comment out parts of that method until it stopped crashing. If it only stopped when the entire method was commented out, then I would need to go into the code and try to spot the problem. However, if you comment out half of the method and the remaining code runs, then you know that the problem is in something you commented out. Then you can narrow that down to the exact problem.
Just make sure you comment out blocks of code that make sense. Don't just comment out a bracket, and then have your program give you errors. Comment out the code intelligently.
Since your entire app isn't working, make sure that your preliminary activity is set correctly in your Manifest. If it is your splash, make sure that it is called when the app starts. There is a setting/line of code in the manifest somewhere, but I forget where it is. If all of that is correct, then start commenting out the code in that activity systematically. Start with the last method called, then the second to last, and so on. When it starts running, look at the code you have commented out and try to find the problem.
This is all we can do without any code or logcat. If the problem turns out not to be in the code, but in the phone, then this would be better suited on the Android Enthusiasts forum. You could ask for a migration if this is the case. Hope this helps!
UPDATE
I am still not 100% sure what the problem is, but I can suggest some changes in your code that may or may not work.
Make sure your newj activity is in your Manifest.XML. There should be a line of code that says:
<activity android:theme="#style/AppBaseTheme" android:screenOrientation="portrait" android:name="newj"></activity>
If that doesn't help at all, you can try these also.
Instead of having your intent outside the click listener, put in inside. I find that having not in the same OnClickListener or if statement or method can cause a problem to two. So your code would be:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Intent intent = new Intent(MainActivity.this, newj.class);
startActivity(intent); //and get rid of MainActivity.this.
}
});
If that doesn't work, then this method is more in-depth, but I think it is safer.
First, you want to go into the XML for your button, and add these two lines.
android:Clickable="true"
android:onClick="methodToBeCalled"
These two lines take the place of the OnClickListener, and make the button clickable and tell the program which method to call when the button is clicked. So you need to make a new method, outside of onCreate, that looks like this.
public static void methodToBeCalled(View view){
final Intent intent = new Intent(MainActivity.this, newj.class);
startActivity(intent);
}
So basically what happens is that the code in the XML makes the button clickable, and says to call the method, "methodToBeCalled" when it is clicked. Then it looks for that method in your mainActivity class (because that XML file is linked to this activity) and it runs that activity, starting the Intent intent which starts the Activity newj. Confusing, right? Not really. The XML tells the program to run the method "methodToBeCalled", which starts newj.
Try these methods in order. If method 1 doesn't work, move onto 2. Then 3. If none of these work, update your question with the new logcat, your entire MainActivity code, your entire activity_main XML code, and your entire AndroidManifest.XML code. We can move on from there.
If this does turn out to be a problem with your phone and not the code, flag for migration to Android Enthusiasts Stack Exchange. You will get better help there if this does turn out to be a problem with the phone itself, and not the code.
Hope this helps!