Eu atualizei meu Android Studio para a versão 1.4 e estou tendo problemas para implementar o ActionBar numa activity
.
O aplicativo executa a validação dos dados de um usuário através do MainActivity
e depois direciona o usuário para a segunda activity
(WelcomeActivity
). Estou tentando criar um "up navigation
"através do códigogetActionBar().setDisplayHomeAsUpEnabled(true);
mas quando eu executo o aplicativo, ele fecha no momento que vai passar de uma activity para outra. Se eu remover a informação da actionbar
mencionado acima, o aplicativo funciona normalmente.
Também tentei retirar as informações referentes a toolbar
que existe nas activities mas não funcionou.
// 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)