I know it is simple but I wasnt able to fix it.Tried many solution from SOF.com but none worked. I am trying to display a title for activity and throws NPE. Using API19 for testing.Below are the codes:
In the activity:
public class DisplayImageActivity extends RootActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().setTitle("Image Preview");
In the Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".DisplayImageActivity"
android:label="< app name >"
android:screenOrientation="portrait" >
</activity>
In the style.xml:
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
Finally, the logcat says:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kittu.<> /com.example.kittu.<>.DisplayImageActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.kittu.< >.DisplayImageActivity.onCreate(DisplayImageActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
If you are using the support library, your activity needs to extend it
import android.support.v7.app.AppCompatActivity
public class MainActivity extends AppCompatActivity{
and use getSupportActionBar() instead of getActionBar()
I think you are using AppCompat, then you should use getSupportActionBar() instead of getActionBar().
Change this:
getActionBar().setTitle("Image Preview");
to this:
getSupportActionBar().setTitle("Image Preview");
Hope this helps :)
Related
App is crashing on restart of app after tablet/mobile orientation change,
I m going to develop an application in landscape mode only, and I have made a layout in layout-land with name acitivty_main and there is no any portrait layout in my application and no any layout folder as well, I have also added screenOrientation="landscape" in manifest activity.
Activity Code Following:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
Manifest.xml code following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yasir.sample">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Project Structure:
FATAL EXCEPTION: main
Process: com.example.yasir.sample, PID: 12611
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.yasir.sample/com.example.yasir.sample.MainActivity}:
android.content.res.Resources$NotFoundException: Resource ID #0x7f04001b
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3151)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5205)
at android.app.ActivityThread.access$1100(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1741)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6939)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f04001b
at android.content.res.Resources.getValue(Resources.java:2495)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:4236)
at android.content.res.Resources.getLayout(Resources.java:2311)
at android.view.LayoutInflater.inflate(LayoutInflater.java:413)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.yasir.sample.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3104)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5205)
at android.app.ActivityThread.access$1100(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1741)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6939)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Resources$NotFoundException
This exception is thrown by the resource APIs when a requested
resource can not be found
FYI
Remove
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Add android:configChanges="orientation|screenLayout" in Manifest section .
Finally
<activity android:name=".MainActivity"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout">
Then Clean and Rebuild your project .
https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/
This link will explain how to prevent crashes from AsyncTask and Screen Orentation
I know that there are problems like this before. I have tried some of the solution given from the thread but it did not seem to solve the error for my application
This is my android.manifest file for the second activity
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RevGPAActivity"
android:label="#string/title_activity_rev_gpa">
<intent-filter>
<action android:name="com.example.user.testapp.RevGPAActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
And here is my MainActivity
private GoogleApiClient client;
Button start_rev_gpa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
//Start Rev GPA
startRevGPA();
}
public void startRevGPA(){
start_rev_gpa = (Button)findViewById(R.id.startRevGPA);
start_rev_gpa.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.user.testapp.RevGPAActivity");
startActivity(intent);
}
}
);
}
I did not do anything on the xml file like android:onClick or anything. The app hangs for about 3 seconds then it stopped working
Logcat
FATAL EXCEPTION: main
Process: com.example.user.testapp, PID: 29051
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.testapp/com.example.user.testapp.RevGPAActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.example.user.testapp.RevGPAActivity.onCreate(RevGPAActivity.java:10)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Solved my own problem. If someone stumbled on the same error, the target activity should be extending Activity not ActionBarActivity
You need to use a Theme.AppCompat theme (or descendant) with this activity
As you see in log file: You need to use a Theme.AppCompat theme (or descendant) with this activity could you check `#style/AppTheme" in style.xml of your project, this style need have parent="#style/Theme.AppCompat" or descendant ò
Hello I' making this project in Android when i start the dialog Activity from Main Activity.
These are the related files:
Main Activity:http://pastebin.com/xyDbBSj4
Dialogs Activity:http://pastebin.com/f6jGC1CH
I have this error on logcat:
FATAL EXCEPTION: main
Process: com.android4dev.navigationview, PID: 30736
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android4dev.navigationview/com.android4dev.navigationview.ui.activities.DialogsActivity}:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at com.android4dev.navigationview.ui.activities.BaseActivity.onCreate(BaseActivity.java:36)
at com.android4dev.navigationview.ui.activities.DialogsActivity.onCreate(DialogsActivity.java:43)
1- You should declare that your application will be stylized using Theme.AppCompat in your Android Manifest.
Something like this:
<activity
android:name=".my.packageactivity.MyOwnActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat"/>
2- Or maybe declare that the whole application will be stylized using AppCompat.
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat">
3- If you want to set your own style later, you'll have to create a style.xml.
3.1- Using the whole application:
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyOwnStyle">
3.2- Using the activity>
<activity
android:name=".my.packageactivity.MyOwnActivity"
android:label="#string/app_name"
android:theme="#style/MyOwnStyle"/>
And then set the parent to Theme.AppCompat inside your new style.xml file:
<resources>
<style name="MyOwnStyle" parent="Theme.AppCompat">
[...]
</style>
</resources>
I know that this question has been asked before, but the provided answers did not solve my case, so I'll ask for an individual solution.
My code
My AndroidManifest.xml looks like this:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".KategorieAuswahl"
android:label="#string/title_activity_kategorie_auswahl" >
</activity>
<activity
android:name=".ZeigeFragen"
android:label="#string/title_activity_zeige_fragen" >
</activity>
</application>
And my styles.xml looks like this:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
I want my App to open in fullscreen and to hide the TitleBar. The first thing I did was to add a android:theme="#android:style/Theme.NoTitleBar.Fullscreen" attribute to the <application>-Tag of my AndroidManifest.xml.
This leads to the error You need to use a Theme.AppCompat theme (or descendant) with this activity. Here is the full stacktrace:
05-27 21:24:26.889 2121-2121/de.test.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.test.myapp/de.test.myapp.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
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)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:113)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at de.test.myapp.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
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)
There are multiple questions on stackoverflow referencing this error.
Approach #1
One solution suggests to change the Java inheritance from ActionBarActivity to Activity and leave the manifest as it is.
So I updated my the <style> element in my styles.xml and removed the DarkActionBar part:
<style name="AppTheme" parent="Theme.AppCompat.Light">
This still gives me the same error.
Approach #2
The next solution suggests to change my styles.xml's parent attribute from
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to <style name="AppTheme" parent="Theme.AppCompat">
This makes it possible to use android:theme="#style/Theme.AppCompat.NoActionBar" for my <activity>, but I don't see an option to make it fullscreen then.
Approach #3
Another solution soggests to add <item name="android:windowNoTitle">true</item> to my <style> element but this doesn't change anything at all.
Conclusion
I really tried a lot, but nothing seems to work. Anyway I have problems understanding the theme inheritance chain and when I may use .NoTitleBar and .Fullscreen.
After all I don't care which theme to use, as long I can make the app fullscreen without a title bar. Can you help me to pick the right attributes?
extend ActionBarActivity or the new one, and use Theme.AppCompat.NoActionBar, now in your Activity
if (Build.VERSION.SDK_INT < 16) {
Activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}else{
Activity.getWindow().getDecorView().
setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
}
Hope it helps..
I got a very strange issue. Im using ActionbarSherlock in my project. And on very few Devices i get a:
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.felitec.dow/de.felitec.dow.ui.LoginActivity}: java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
at android.app.ActivityThread.access$500(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.
at com.actionbarsherlock.internal.ActionBarSherlockCompat.generateLayout(ActionBarSherlockCompat.java:976)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.installDecor(ActionBarSherlockCompat.java:902)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:836)
at com.actionbarsherlock.app.SherlockActivity.setContentView(SherlockActivity.java:229)
at de.felitec.dow.ui.LoginActivity.onCreate(LoginActivity.java:36)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
... 11 more
My LoginActivity looks like this:
public class LoginActivity extends SherlockActivity implements AuthCallback<OAuthConsumer> {
private ProgressDialog progDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
...
And my Manifest:
<application
android:label="#string/app_name"
android:icon="#drawable/ic_app_launcher"
android:name=".MainApplication"
android:theme="#style/MainAppTheme">
<activity
android:name=".ui.SplashActivity"
android:screenOrientation="portrait"
android:theme="#style/Theme.Sherlock.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".ui.LoginActivity"
android:windowSoftInputMode="adjustPan|stateHidden"
android:screenOrientation="portrait"/>
...
And finally my style.xml:
<style name="MainAppTheme" parent="#style/Theme.Sherlock.Light.DarkActionBar">
<item name="homeAsUpIndicator">#drawable/back_indicator</item>
...
Notice that the SplashActivity works just fine. When it starts the intent for LoginActivity it crashes, and i dont really see why.
thanks in advance
Update your Manifiest file with this.
android:theme="#style/Theme.Sherlock"