I am attempting to write get a handle on the new Shared Elements Animations API that google L preview is giving us.
I have been trying to use the code base as a point of reference along with: https://developer.android.com/preview/material/animations.html
For the life of me I cannot seem to get my app to work. However when I run the Demo App from google the transition works just fine.
MyActivity.java:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final ImageView imageView = (ImageView)this.findViewById(R.id.img_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void showPhoto(View view) {
Intent intent = new Intent();
intent.setClass(this, MyActivity2.class);
ImageView hero = (ImageView) ((View) view.getParent()).findViewById(R.id.img_main);
((ViewGroup) hero.getParent()).setTransitionGroup(false);
ActivityOptions options =
ActivityOptions.makeSceneTransitionAnimation(this, hero, "robot");
startActivity(intent, options.toBundle());
}
}
activity_my.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<ImageView
android:id="#+id/img_main"
android:viewName="photo1"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/test"
android:onClick="showPhoto"/>
</RelativeLayout>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowExitTransitionOverlap">true</item>
<item name="android:windowActionBarOverlay">false</item>
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jms_m_000.transitionshit" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyActivity"
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=".MyActivity2"
android:label="#string/title_activity_my_activity2" >
</activity>
</application>
</manifest>
The Error I am receiving:
07-06 18:54:42.153 24145-24145/com.example.jms_m_000.transitionshit
E/AndroidRuntime﹕FATAL EXCEPTION: main
Process: com.example.jms_m_000.transitionshit, PID: 24145
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3970)
at android.view.View.performClick(View.java:4598)
at android.view.View$PerformClick.run(View.java:19268)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at rnal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:3965)
at android.view.View.performClick(View.java:4598)
at android.view.View$PerformClick.run(View.java:19268)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
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:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.app.ActivityOptions.toBundle()' on a null object reference
at com.example.jms_m_000.transitionshit.MyActivity.showPhoto(MyActivity.java:56)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:3965)
at android.view.View.performClick(View.java:4598)
at android.view.View$PerformClick.run(View.java:19268)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5070)
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:836)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
Im not sure why but options is null after calling .makeSceneTransitionAnimation()...
Any Thoughts?
Try adding
requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
in the onCreate() method of your activity.
That surprisingly worked for me in making options non null.
Define a view name by calling setViewName() to 'hero' in your first Activity, and define a view name (same as defined in makeSceneTransitionAnimation) to your ImageView in your second Activity . That's worked for me
Related
I am pretty new to coding and am currently a student. I am trying to create a simple timer app. When I run the timer app in the emulator, it opens for a split second and than force stops.
I have tried:
1. Clean Project
2. Adding android:hasCode = "true";
3. Downloading AppCompat
Here is my MainActivity code:
package com.example.timer2;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.CountDownTimer;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView countdownText;
private Button countdownButton;
private CountDownTimer countDownTimer;
private long timeLeftInMilliseconds = 600000; //10 mins
private boolean timerRunning;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countdownText = findViewById(R.id.countdown_text);
countdownButton = findViewById(R.id.countdown_button);
countdownButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startStop();
}
});
}
public void startStop () {
if (timerRunning) {
stopTimer ();
} else {
startTimer ();
}
}
public void startTimer () {
countDownTimer = new CountDownTimer(timeLeftInMilliseconds, 1000) {
#Override
public void onTick(long l) {
timeLeftInMilliseconds = l;
updateTimer ();
}
#Override
public void onFinish() {
}
}.start();
countdownButton.setText("PAUSE");
timerRunning = true;
}
public void stopTimer () {
countDownTimer.cancel();
countdownButton.setText("START");
timerRunning = false;
}
public void updateTimer () {
int minutes = (int) timeLeftInMilliseconds / 600000;
int seconds = (int) timeLeftInMilliseconds % 600000 / 1000;
String timeLeftText;
timeLeftText = "" + minutes;
timeLeftText += ":";
if (seconds < 10) timeLeftText += "0";
timeLeftText += seconds;
countdownText.setText(timeLeftText);
}
}
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timer2">
<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"
android:hasCode="true">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:id="#+id/countdown_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="10:00"
android:textSize="50sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/countdown_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_marginBottom="237dp"
android:text="#string/start" />
</RelativeLayout>
Here is the error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.timer2, PID: 2947
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timer2/com.example.timer2.MainActivity}: android.view.InflateException: Binary XML file line #23 in com.example.timer2:layout/activity_main: Binary XML file line #20 in com.example.timer2:layout/content_main: Error inflating class com.google.android.material.button.MaterialButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3268)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3407)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7343)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933)
Caused by: android.view.InflateException: Binary XML file line #23 in com.example.timer2:layout/activity_main: Binary XML file line #20 in com.example.timer2:layout/content_main: Error inflating class com.google.android.material.button.MaterialButton
Caused by: android.view.InflateException: Binary XML file line #20 in com.example.timer2:layout/content_main: Error inflating class com.google.android.material.button.MaterialButton
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:854)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1263)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1119)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:545)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.timer2.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3243)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3407)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7343)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933)
Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTextAppearance(ThemeEnforcement.java:170)
at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:75)
at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:140)
at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:133)
... 31 more
Thank you in advance for any help with this.
tl;dr
Change your app theme to inherit from Theme.MaterialComponents or Bridge theme. Also be aware that your activity sets its own theme settings in AndroidManifest.xml.
Welcome to StackOverflow! According to the error message, the MaterialButton you are using in your layout requires an application theme which inherits from Theme.MaterialComponents. Your application most likely uses the default AppCompat theme. To fix the error go to your src/main/res/values/styles.xml file and change the theme to a MaterialComponents theme. For example:
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<!-- ... -->
</style>
You can pick from the following themes:
Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar
Theme.MaterialComponents.DayNight
Theme.MaterialComponents.DayNight.NoActionBar
Theme.MaterialComponents.DayNight.DarkActionBar
If you must use the AppCompat theme (for example, for compatibility with API level below Lollipop), you can inherit from a Bridge theme.
Bridge themes inherit from AppCompat themes, but also define the new
Material Components theme attributes for you.
The following Bridge themes are available:
Theme.MaterialComponents.Bridge
Theme.MaterialComponents.Light.Bridge
Theme.MaterialComponents.NoActionBar.Bridge
Theme.MaterialComponents.Light.NoActionBar.Bridge
Theme.MaterialComponents.Light.DarkActionBar.Bridge
You can find more information regarding the Material Themes here.
Be aware that your Activity sets its own theme settings in the AndroidManifest.xml:
<activity
...
android:theme="#style/AppTheme.NoActionBar">
...
</activity>
This means that if you set the theme in styles.xml to inherit from Theme.MaterialComponents.Light.NoActionBar, your activity will now look for a theme called Theme.MaterialComponents.Light.NoActionBar.NoActionBar, which doesn't exist. You can either delete this line or make your app inherit from Theme.MaterialComponents.Light.
I have been trying using custom title bar in my activity. Following is the code i have used
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
}
final TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
if ( txtTitle != null ) {
txtTitle.setText("PikMyBox - Welcome to PikMyBox");
}
setContentView(R.layout.activity_main);
}
custom_title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTitle"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtCustomText"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorSplash</item>
<item name="android:windowNoTitle">true</item>
</style>
Manifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
-----
I m getting the following error when activity executed
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:302)
at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2975)
at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3241)
at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1821)
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:363)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
at android.support.v7.app.AppCompatDelegateImplV7.findViewById(AppCompatDelegateImplV7.java:229)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184)
at com.kommlabs.pikmybox.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5447)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
How can i solve this problem ?
Change android:windowNoTitle to false in your styles.xml.
Your setContentView(R.layout.activity_main); is below final TextView txtTitle = (TextView) findViewById(R.id.txtTitle); ie why you are getting exception.You are initilizing the view before it is been created.
Change it to
setContentView(R.layout.activity_main);
final TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
Also in case of your exception with custom title bar use this style
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
</style>
As suggested here https://stackoverflow.com/a/27410003/3111083
I'm trying to enable the back button in this really simple activity (that is instantiated by another activity), but I always get the null pointer exception and therefore my app crashes when I launch the activity (without the back button in the action bar the activity works great).
I tried many different solution posted in the internet, but none worked...
I'm following this guide : Android Developer providing up navigation
Here the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.projectcalculator">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
android:screenOrientation="portrait">
<!--attività principale-->
<activity
android:name=".MainActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--attività infixToPostfix-->
<activity
android:name=".InfixToPostfixActivity"
android:launchMode="singleTask"
android:label="#string/infix_to_postfix"/>
<!--attività figlia di infixToPostfix-->
<activity
android:name=".ShowPostfixProcedureActivity"
android:launchMode="standard"
android:label="#string/postfix_procedure"
android:parentActivityName=".InfixToPostfixActivity" />
</application>
</manifest>
Here the java file of the activity (ShowPostfixProcedureActivity):
package com.example.android.projectcalculator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.Stack;
import static java.lang.Character.isDigit;
public class ShowPostfixProcedureActivity extends AppCompatActivity {
private static String equation;
private static int openBracket;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_infix_to_postfix_jurney);
Toolbar myToolbar = (Toolbar) findViewById(R.id.infix_to_postfix_toolbar_procedure);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView txview = (TextView)findViewById(R.id.postfix_jurney_text_wall);
Stack<String> s = new Stack<String>();
s = DataProcessor.ReturnProcess();
txview.setText(s.pop());
while (!s.empty())
{
if (s.peek().charAt(0) == '<')
{
txview.append(Html.fromHtml(s.pop()));
}
else
{
txview.append(s.pop());
}
}
//Log.d("SPPA/onCreate","text = "+DataProcessor.ReturnProcess());
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_postfix_procedure, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
The XML file related to the layout (show_infix_to_postfix_jurney):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--ActionBar in alto-->
<android.support.v7.widget.Toolbar
android:id="#+id/infix_to_postfix_toolbar_procedure"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#42f480"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<TextView
android:id="#+id/postfix_jurney_text_wall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10sp"
android:textSize="20sp"
android:gravity="center"
android:background="#f2f2f2"/>
</ScrollView>
</LinearLayout>
And finally the XML file related to the Navigation bar (action_bar_postfix_procedure):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
</menu>
Here the error message:
10-29 17:14:43.841 18822-18822/com.example.android.projectcalculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.projectcalculator, PID: 18822
Theme: themes:{}
java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setEnabled(boolean)' on a null object reference
at com.example.android.projectcalculator.ShowPostfixProcedureActivity.onCreateOptionsMenu(ShowPostfixProcedureActivity.java:51)
at android.app.Activity.onCreatePanelMenu(Activity.java:2852)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I don't know what the line NavUtils.navigateUpFromSameTask(this); is supposed to do. If you want to close the activity on the press of the back button replace it with the method call onBackPressed()
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
onBackPressed()
return true;
}
return super.onOptionsItemSelected(item);
}
This question already has answers here:
android.content.ActivityNotFoundException: Unable to find explicit activity class
(9 answers)
Closed 6 years ago.
I am creating an app and added a new Resource Layout File. In Android Studio Preview, I am able to preview the file. The problem is that, While Calling that Layout, the app is getting crashed.
details_main_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
DetailsMain.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class DetailsMain extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_main);
}
}
I am calling this Activity fron Navigation bar Menu Item. The code is as follows.
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_details) {
Intent in = new Intent(getApplicationContext(),DetailsMain.class);
startActivity(in);
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
What Mistake had I made?
LogCat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: switchmode.new.switch, PID: 3156
android.content.ActivityNotFoundException: Unable to find explicit
activity class
{switchmode.new.switch/switchmode.new.switch.DetailsMain}; have you
declared this activity in your AndroidManifest.xml?
at
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
at
android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at
android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at
switchmode.new.switch.MainActivity.onNavigationItemSelected(MainActivity.java:97)
at
android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:151)
at
android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at
android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at
android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at
android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:318)
at android.view.View.performClick(View.java:4789)
at android.view.View$PerformClick.run(View.java:19881)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
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:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
You forget to Declare your Activity in manifest file.
<activity android:name="your.pakage.name.DetailsMain" />
you need to declare DetailsMain in your Manifest file
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.DetailsMain"/>
</application>
You haven't declared activity in menifest file
<activity android:name="you_package_name.DetailsMain" />
I am making an android project by targetting api 13+. I need to have a appbar/toolbar instead of actionbar at the top of each activity I have in my app. I have tried searching over many blogs and SO itself but without any success.
I have created a base activity class and all my activities inherit from this class:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v7.widget.Toolbar;
public class BaseActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar tb = (Toolbar) findViewById(R.id.appToolbar);
setSupportActionBar(tb);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("oh yeah");
}
...
}
I have done the housekeeping stuff for the toolbar in this class and the following class is the activity which shows up when the app is launched:
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// api level < 16 and > 16 have different
// ways of removing the status bar
if(Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_splash_screen);
// Create a new thread and delay for 3 sec
// to handle the start of the main screen.
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent().setClass(getApplicationContext(), MainScreen.class);
startActivity(i);
}
}, 3000);
}
And this is the activity which is launched after the splash screen:
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
public class MainScreen extends BaseActivity {
private ImageButton btnLoyaltyForm;
private ImageButton btnFeedbackForm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
btnLoyaltyForm = (ImageButton) findViewById(R.id.btnLoyaltyForm);
btnFeedbackForm = (ImageButton) findViewById(R.id.btnFeedbackForm);
btnLoyaltyForm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent().setClass(getApplicationContext(), LoyaltyFormScreen.class);
startActivity(i);
}
});
btnFeedbackForm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent().setClass(getApplicationContext(), FeedbackFormScreen.class);
startActivity(i);
}
});
}
}
Here is the XML for my appbar:
<android.support.v7.widget.Toolbar
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/actionbar_background_color"
android:minHeight="?attr/actionBarSize"
xmlns:android="http://schemas.android.com/apk/res/android" />
Here is the XML for my MainScreen activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context="com.tarz.MainScreen">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<include
android:id="#+id/appToolbar"
layout="#layout/app_bar" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="false"
android:layout_alignParentBottom="false"
android:layout_alignWithParentIfMissing="false"
android:gravity="center">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnLoyaltyForm"
android:layout_gravity="center_horizontal"
android:src="#drawable/btn_form1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnFeedbackForm"
android:layout_gravity="center"
android:src="#drawable/btn_form2" />
</LinearLayout>
</RelativeLayout>
Here is my app's manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tarz" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashScreen"
android:label="#string/title_activity_splash_screen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainScreen"
android:label="#string/title_activity_main_screen" >
</activity>
<activity
android:name=".BaseActivity"
android:label="#string/title_activity_base" >
</activity>
</application>
</manifest>
Here is my colors.xml:
n="1.0" encoding="utf-8"?>
<resources>
<color name="actionbar_background_color">#2d3135</color>
<color name="actionbar_text_color">#ffffff</color>
<color name="app_background_color">#f7f6f0</color>
<color name="button_background_color">#2d3135</color>
<color name="button_text_color">#ffffff</color>
</resources>
Here is my styles.xml:
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:background">#color/app_background_color</item>
<!--<item name="colorPrimaryDark">#color/colorPrimaryDark</item>-->
<!--<item name="colorAccent">#color/colorAccent</item>-->
</style>
<style name="ActionBarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
<item name="android:background">#color/actionbar_background_color</item>
<item name="colorPrimary">#color/actionbar_background_color</item>
</style>
</resources>
When I run the app, the splash screen shows up then it crashes with the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
And
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
This is the log:
4059-4059/com.tarz E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.tarz, PID: 4059
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tarz/com.tarz.MainScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
at com.tarz.MainScreen.onCreate(MainScreen.java:28)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
How do I use the colors I have defined in my colors.xml so that my toolbar could have a different color, my app could have a different color and my toolbar's text could have a different color.
Also, menu on each activity would be different so the toolbar should adjust according to the activity, how could I do that also?
Please help, thanks. :)
EDIT 1:
After #Rod said to initialize toolbar after setting contentView, it cleared the NPE but then no toolbar is being shown:
This method is added at the top of the BaseActivity class:
public void initToolbar() {
Toolbar tb = (Toolbar) findViewById(R.id.appToolbar);
setSupportActionBar(tb);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("oh yeah");
}
and the onCreate now just have the super call. Also, the MainScreen class now have the initToolbar(); called after setContentView(); in onCreate(); method.
A couple of things I would point out:
You are not inflating a layout in your onCreate() in BaseActivity.
I think the official Toolbar tutorials are all using AppCompatActivity instead of ActionBarActivity.
You could define different Toolbar themes and apply them in different activities. More info here.
Each activity has it's own toolbar, so you don't have to worry about supporting different menus for each activity. You simply inflate the menu XML for the activity in onCreateOptionsMenu().
EDIT 1: Inflating the appropriate layout in the base activity:
public abstract class BaseActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResId());
final Toolbar toolbar = findViewById(<your toolbar id>);
// the rest of your logic
}
// This abstract method will provide a way for extending activities to give the id of their layout, so you can inflate it, before trying to set the toolbar
#LayoutRes
protected abstract getLayoutResId();
}
EDIT 2: Customising the toolbar
The Toolbar, like every other widget, can be customised using styles. To control the toolbar background colour, spacing, etc, you need to set a custom style to it. You can also change the toolbar title and subtitle appearance using the same mechanism. Here is a great link that shows all you need to know about creating and applying custom toolbar styles.