我更新了我的android工作室1.4版,在activity中实现actionbar时遇到了问题。
应用程序通过MainActivity
执行来自用户的数据验证,然后引导用户执行第二个活动(WelcomeActivity
)。我正试图通过代码创建一个“up navigation
”,但是当我运行应用程序时,它会从一个活动移动到另一个活动。如果我从上面提到的操作栏中删除信息,应用程序将正常工作。
main活动.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();
}
}
欢迎活动.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_欢迎.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" />
活动欢迎.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>
样式.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崩溃日志
10-15 00:25:42.457 4456-4456/br.com.luizugliano.actionbar w/system:classloader引用了未知路径/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/:host connection::get()新主机连接已建立0xAD70DDb0,tid 4456
10-15 00:25:42.735 4456-4472/br.com.luizugliano.actionbar d/:host connection::get()已建立新主机连接0xAD70DFb0,tid 4472
10-15 00:25:42.757 4456-4472/br.com.luizugliano.actionbar i/openglrenderer:初始化的egl,版本1.4
10-15 00:25:42.854 4456-4472/br.com.luizugliano.actionbar w/egl_emulation:eglsurfaceattrib未实现
10-15 00:25:42.854 4456-4472/br.com.luizugliano.actionbar w/openglrenderer:未能在表面上设置egl_swap_行为0xabfa99e0,error=egl_success
10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar d/androidruntime:关闭虚拟机
10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar e/androidruntime:致命异常:main
10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar e/androidruntime:进程:br.com.luizugliano.actionbar,PID:4456
10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar e/androidruntime:java.lang.runtimeexception:无法启动活动组件信息{br.com.luizugliano.actionbar/br.com.luizugliano.actionbar.welcomeactivity}:java.lang.nullpointereexception:尝试调用虚拟方法“void android.app.actionbar.setdisplayHomeAsupabled(boolean)”在空对象引用上
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(本机方法)
10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar e/androidruntime:com.android.internal.os.zygeteinit$methodandargscaller.run(zygeteinit.java:726)
10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar e/androidruntime:com.android.internal.os.zygenetinit.main(zygenetinit.java:616)
10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar e/androidruntime:由以下原因引起:java.lang.nullpointerexception:尝试对空对象引用调用虚拟方法“void android.app.actionbar.setdisplayHomeAsupabled(boolean)”。
10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar e/androidruntime:网址: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(本机方法)
10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar e/androidruntime:com.android.internal.os.zygeteinit$methodandargscaller.run(zygeteinit.java:726)
10-15 00:26:02.266 4456-4456/br.com.luizugliano.actionbar e/androidruntime:com.android.internal.os.zygenetinit.main(zygenetinit.java:616)
最佳答案
我看得不够深,但乍一看,你好像在做:Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(toolbar);
然后你会的//Adiciona o botão de navegação da action bargetActionBar().setDisplayHomeAsUpEnabled(true);
(与使用工具栏相反)。
但是,最重要的是,你也有两种活动android:theme="@style/AppTheme.NoActionBar"
但是您没有提供自定义工具栏的布局。(或者你没有展示)
您至少需要在工具栏中包含一个小部件:<android.support.v7.widget.Toolbar…
你叫它toolbar
所以要确保它android:id="@+id/toolbar"
否则,当您试图取消引用不存在的工具栏时,您的崩溃可能是您的活动的OnCube方法中的空指针异常。