1
I upgraded my Android Studio to version 1.4 and am having problems implementing Actionbar in one activity
.
The application performs validation of a user’s data through the MainActivity
and then directs the user to the second activity
(WelcomeActivity
). I’m trying to create a "up navigation
"through the codegetActionBar().setDisplayHomeAsUpEnabled(true);
but when I run the application, it closes at the moment it will go from one Activity to another. If I remove the information from the actionbar
mentioned above, the application works normally.
I have also tried to remove the information concerning toolbar
that exists in the activities but did not work.
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
Mainactivity.java
package br.com.luizugliano.actionbar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void onClickBtBuscar(View view){
EditText etNome = (EditText) findViewById(R.id.etNome);
EditText etCPF = (EditText) findViewById(R.id.etCPF);
String nome = etNome.getText().toString();
String cpf = etCPF.getText().toString();
if("luiz".equals(nome) && "123456789".equals(cpf)){
Intent intent = new Intent(getContext(),WelcomeActivity.class);
Bundle params = new Bundle();
params.putString("nome", "Luiz");
intent.putExtras(params);
startActivity(intent);
}else{
alert("Usuário não encontrado, faça seu cadastro");
}
}
private Context getContext(){
return this;
}
private void alert(String s){
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
}
}
Welcomeactivity.java
package br.com.luizugliano.actionbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;
public class WelcomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle args = getIntent().getExtras();
String nome = args.getString("nome");
//Atualizando o texto do TextView com uma mensagem de boas vindas
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(nome + ", seja bem-vindo.");
//Adiciona o botão de navegação da action bar
getActionBar().setDisplayHomeAsUpEnabled(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();
//noinspection SimplifiableIfStatement
if (id == android.R.id.home) {
//O método finish encerrará essa activity
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.luizugliano.actionbar" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<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>
<activity
android:name=".WelcomeActivity"
android:label="@string/title_activity_welcome"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".MainActivity">
</activity>
</application>
content_welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_welcome"
tools:context="br.com.luizugliano.activity.WelcomeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView" />
activity_welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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" android:fitsSystemWindows="true"
tools:context="br.com.luizugliano.actionbar.WelcomeActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_welcome" />
<!--<android.support.design.widget.FloatingActionButton android:id="@+id/fab"-->
<!--android:layout_width="wrap_content" android:layout_height="wrap_content"-->
<!--android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"-->
<!--android:src="@android:drawable/ic_dialog_email" />-->
</android.support.design.widget.CoordinatorLayout>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
logcat crash log
10-15 00:25:42.457 4456-4456/br.com.luizugliano.actionbar W/System: Classloader referenced Unknown path: /data/app/br.com.luizugliano.actionbar-2/lib/x86 10-15 00:25:42.680 4456-4472/br.com.luizugliano.actionbar D/Openglrenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 10-15 00:25:42.683 4456-4456/br.com.luizugliano.actionbar D/: Hostconnection::get() New Host Connection established 0xad70ddb0, tid 4456 10-15 00:25:42.735 4456-4472/br.com.luizugliano.actionbar D/: Hostconnection::get() New Host Connection established 0xad70dfb0, tid 4472 10-15 00:25:42.757 4456-4472/br.com.luizugliano.actionbar I/Openglrenderer: Initialized EGL, version 1.4 10-15 00:25:42.854 4456-4472/br.com.luizugliano.actionbar W/Egl_emulation: eglSurfaceAttrib not implemented 10-15 00:25:42.854 4456-4472/br.com.luizugliano.actionbar W/Openglrenderer: Failed to set EGL_SWAP_BEHAVIOR on Surface 0xabfa99e0, error=EGL_SUCCESS 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar D/Androidruntime: Shutting down VM 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: FATAL EXCEPTION: main 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: Process: br.com.luizugliano.actionbar, PID: 4456 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.luizugliano.actionbar/br.com.luizugliano.actionbar.WelcomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Actionbar.setDisplayHomeAsUpEnabled(Boolean)' on a null Object Reference 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activitythread.performLaunchActivity(Activitythread.java:2416) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2476) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activitythread. -wrap11(Activitythread.java) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activitythread$H.handleMessage(Activitythread.java:1344) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.os.Handler.dispatchMessage(Handler.java:102) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.os.Looper.loop(Looper.java:148) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activitythread.main(Activitythread.java:5417) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at java.lang.reflect.Method.invoke(Native Method) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:726) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:616) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: Caused by: java.lang.Nullpointerexception: Attempt to invoke virtual method void 'android.app.Actionbar.setDisplayHomeAsUpEnabled(Boolean)' on a null Object Reference 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/AndroidRuntime: at br.com.luizugliano.actionbar.WelcomeActivity.onCreate(WelcomeActivity.java:25) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activity.performCreate(Activity.java:6237) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activitythread.performLaunchActivity(Activitythread.java:2369) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2476) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activitythread. -wrap11(Activitythread.java) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activitythread$H.handleMessage(Activitythread.java:1344) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.os.Handler.dispatchMessage(Handler.java:102) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.os.Looper.loop(Looper.java:148) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at android.app.Activitythread.main(Activitythread.java:5417) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at java.lang.reflect.Method.invoke(Native Method) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:726) 10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar E/Androidruntime: at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:616)
Thanks for the help. I was seeing the reason for the exception through logcat and is saying that my actionBar is null-
br.com.luizugliano.actionbar.WelcomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference 10-15
. I tried to remove the libraryAppCompactActivity
extending another class I created to control the app’s lifecycle, but it’s still making a mistake when switching from one Active to another. Can you help me by showing how your suggestion would look in my codig– Luiz Henrique Ugliano
Exactly, you set the
supportActionBar
and is using theActionBar
normal. I believe it’s not the same.– Wakim
Good! I tried to change the code with the suggestions you gave initially but still giving problem. You could send me your suggestion according to the code I posted?
– Luiz Henrique Ugliano
Luiz, I made the suggestion already looking at your xD code but could update the question with the changes and the new error stacktrace?
– Wakim
Actually I changed the call from the class support library. I changed from
public class WelcomeActivity extends AppCompatActivity {
forpublic class WelcomeActivity extends LifeCycleActivity {
, which is my other class that just treats the stages of the life cycle. Also, I removed the conditions below relating totoolbar
-Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
. The error returned was the same. I found strange theManifest
have updated my class with the informationandroid:theme="@style/AppTheme.NoActionBar"
. =[– Luiz Henrique Ugliano
But in case, you still need to call
getSupportActionBar
, passing theToolbar
, because when using the theme with suffixNoActionBar
, you are required to set up yourToolbar
asActionBar
if you wanted to use it. Otherwise I believe you are correct. You are calling thesetSupportActionBar
in the superclass before theget
?– Wakim
Let’s go continue this discussion in chat.
– Luiz Henrique Ugliano