问题描述
我一直在努力寻找两天的解决方案,它拥有我发疯。我目前按照 http://developer.android.com/教程导/主题/ UI / actionbar.html#ActionItems
不幸的是,应用程序崩溃时SEND键是pressed(这是工作的罚款操作栏中实施前)
这里所涉及的文件:
1. AndroidManifest.xml中
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.mycompany.myfirstapps>
<使用-SDK安卓的minSdkVersion =7
机器人:targetSdkVersion =18/>
<应用
机器人:allowBackup =真
机器人:图标=@纹理映射/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ Theme.AppCompat.Light.DarkActionBar>
&所述;! - 父活动 - >
<活动
机器人:MyActivityNAME =
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<! - 第二个活动 - >
<活动
机器人:名称=。DisplayMessageActivity
机器人:标签=@字符串/ title_activity_display_message
机器人:parentActivityName =MyActivity。>
&所述;元数据
机器人:名称=android.support.PARENT_ACTIVITY
MyActivity:机器人值= />
< /活性GT;
< /用途>
< /舱单>
包com.mycompany.myfirstapps;
进口android.os.Bundle;
进口android.support.v7.app.ActionBarActivity;
进口android.support.v7.app.AppCompatActivity;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.content.Intent;
进口android.widget.EditText;
公共类MyActivity扩展AppCompatActivity {
公共最后静态字符串EXTRA_MESSAGE =com.mycompany.myfirstapps.MESSAGE;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
//如果你的minSdkVersion是11或更高,而使用:
//getActionBar().setDisplayHomeAsUpEnabled(true);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气操作栏中使用的菜单项
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.main_activity_actions,菜单);
返回super.onCreateOptionsMenu(菜单);
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//操作栏上的项目手柄presses
开关(item.getItemId()){
案例R.id.action_search:
OpenSearch的();
返回true;
案例R.id.action_settings:
openSettings();
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}
//调用时,用户点击该按钮
公共无效的sendMessage(查看视图){
意向意图=新的意图(这一点,DisplayMessageActivity.class);
EditText上EDITTEXT =(EditText上)findViewById(R.id.edit_message);
字符串消息= editText.getText()的toString()。
intent.putExtra(EXTRA_MESSAGE,消息);
startActivity(意向);
}
私人无效的OpenSearch(){
//做一点事
}
私人无效openSettings(){
//做一点事
}
}
包com.mycompany.myfirstapps;
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v7.app.AppCompatActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.TextView;
公共类DisplayMessageActivity扩展AppCompatActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
意向意图= getIntent();
//创建文本视图
字符串消息= intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
TextView中的TextView =新的TextView(本);
textview.setTextSize(40);
textview.setText(消息);
//设置文本视图作为活动布局
的setContentView(TextView的);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.menu_display_message,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。将操作栏
//自动在主/向上按钮操作的点击,只要
//你在AndroidManifest.xml中指定一个父活动。
INT的id = item.getItemId();
// noinspection SimplifiableIfStatement
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向
机器人:主题=@风格/ CustomActionBarTheme>
<的EditText
机器人:ID =@ + ID / edit_message
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:提示=@字符串/ edit_message/>
<按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/ button_send
机器人:的onClick =的sendMessage/>
< / LinearLayout中>
应用插件:com.android.application
安卓{
compileSdkVersion 22
buildToolsVersion22.0.1
defaultConfig {
的applicationIDcom.mycompany.myfirstapps
的minSdkVersion 8
targetSdkVersion 22
版本code 1
VERSIONNAME1.0
}
buildTypes {
推出 {
minifyEnabled假
proguardFiles getDefaultProguardFile('ProGuard的-android.txt'),'proguard-rules.pro
}
}
}
依赖{
编译文件树(导演:库,包括:['的* .jar'])
编译com.android.support:appcompat-v7:22.1.1
}
logcat的
05-14 22:57:03.139 2514年至2562年/ android.process.acore I / ContactLocale:通讯录标签[EN-US]:[A,B,C,D ,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,Α,Β,Γ ,Δ,Ε,Ζ,Η,Θ,Ι,Κ,Λ,Μ,Ν,Ξ,Ο,Π,Ρ,Σ,Τ,Υ,Φ,Χ,Ψ,Ω,А,Б,В, Г,Д,Ђ,Е,Є,Ж,З,И,І,Ї,Й,Ј,К,Л,Љ,М,Н,Њ,О,П,Р,С,Т,Ћ,У, Ф,Х,Ц,Ч,Џ,Ш,Щ,Ю,Я,א,ב,ג,ד,ה,ו,ז,ח,ט,י,כ,ל,מ,נ,ס,ע ,פ,צ,ק,ר,ש,ת,ا,ب,ت,ث,ج,ح,خ,د,ذ,ر,ز,س,ش,ص,ض,ط,ظ,ع, غ,ف,ق,ك,ل,م,ن,ه,و,ي,ก,ข,ฃ,ค,ฅ,ฆ,ง,จ,ฉ,ช,ซ,ฌ,ญ,ฎ,ฏ ,ฐ,ฑ,ฒ,ณ,ด,ต,ถ,ท,ธ,น,บ,ป,ผ,ฝ,พ,ฟ,ภ,ม,ย,ร,ฤ,ล,ฦ,ว,ศ ,ษ,ส,ห,ฬ,อ,ฮ,ㄱ,ㄴ,ㄷ,ㄹ,ㅁ,ㅂ,ㅅ,ㅇ,ㅈ,ㅊ,ㅋ,ㅌ,ㅍ,ㅎ,あ,か,さ,た,な,は,ま,や,ら,わ,#]
05-14 22:57:05.181 2514年至2532年/ android.process.acore V / BackupServiceBinder:DOBACKUP()调用
05-14 22:57:05.508 2514年至2532年/ android.process.acore I /艺术:WaitForGcToComplete封锁267.334ms的原因DisableMovingGc
05-14 22:57:05.619 2514年至2532年/ android.process.acore E / DictionaryBackupAgent:无法从游标中读取
05-14 22:57:05.722 2514年至2562年/ android.process.acore I /艺术:WaitForGcToComplete封锁357.236ms的原因DisableMovingGc
1月五日至15日:35:32.770 2514年至2525年/ android.process.acore W /艺术:暂停所有线程了:57.246ms
1月五日至15日:35:35.813 2514年至2527年/ android.process.acore E / StrictMode:资源被收购,在连接的堆栈跟踪,但从来没有公布过。见java.io.Closeable有关避免资源泄漏的信息。
java.lang.Throwable中:显式的终止方法关闭不叫
在dalvik.system.CloseGuard.open(CloseGuard.java:184)
在android.os.ParcelFileDescriptor< INIT>(ParcelFileDescriptor.java:180)
在android.os.ParcelFileDescriptor $ 1.createFromParcel(ParcelFileDescriptor.java:916)
在android.os.ParcelFileDescriptor $ 1.createFromParcel(ParcelFileDescriptor.java:906)
在android.app.IBackupAgent $ Stub.onTransact(IBackupAgent.java:57)
在android.os.Binder.execTransact(Binder.java:446)
1月五日至15日:35:35.925 2514年至2527年/ android.process.acore E / StrictMode:资源被收购,在连接的堆栈跟踪,但从来没有公布过。见java.io.Closeable有关避免资源泄漏的信息。
java.lang.Throwable中:显式的终止方法关闭不叫
在dalvik.system.CloseGuard.open(CloseGuard.java:184)
在android.os.ParcelFileDescriptor< INIT>(ParcelFileDescriptor.java:180)
在android.os.ParcelFileDescriptor $ 1.createFromParcel(ParcelFileDescriptor.java:916)
在android.os.ParcelFileDescriptor $ 1.createFromParcel(ParcelFileDescriptor.java:906)
在android.app.IBackupAgent $ Stub.onTransact(IBackupAgent.java:64)
在android.os.Binder.execTransact(Binder.java:446)
1月五日至15日:35:36.578 2514年至2527年/ android.process.acore E / StrictMode:资源被收购,在连接的堆栈跟踪,但从来没有公布过。见java.io.Closeable有关避免资源泄漏的信息。
java.lang.Throwable中:显式的终止方法关闭不叫
在dalvik.system.CloseGuard.open(CloseGuard.java:184)
在android.os.ParcelFileDescriptor< INIT>(ParcelFileDescriptor.java:180)
在android.os.ParcelFileDescriptor $ 1.createFromParcel(ParcelFileDescriptor.java:916)
在android.os.ParcelFileDescriptor $ 1.createFromParcel(ParcelFileDescriptor.java:906)
在android.app.IBackupAgent $ Stub.onTransact(IBackupAgent.java:71)
在android.os.Binder.execTransact(Binder.java:446)
可能是什么问题?
将您的EditText您的onCreate()方法里面MyActivity.java
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
EditText上EDITTEXT =(EditText上)findViewById(R.id.edit_message);
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
//如果你的minSdkVersion是11或更高,而使用:
//getActionBar().setDisplayHomeAsUpEnabled(true);
}
和你的sendMessage()方法里面,就删除了这一行。
编辑:好像你在新的AppCompatActivity延伸。尝试将其更改为ActionBarActivity在这两个活动。例如:
公共类MyActivity扩展ActionBarActivity {...}
公共类DisplayMessageActivity扩展ActionBarActivity {...}
编辑:如果你计划在不使用ActionBarActivity,那么你需要定义一个自定义工具栏和initialze这样说。
工具栏=(栏)findViewById(R.id.toolbar);
setSupportActionBar(工具栏);
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
I have struggled to find the solution for couple of days and it has drive me nuts. I am currently following the tutorial in http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
Unfortunately the application crashes when SEND button was pressed (It was working fine before the action bar was implemented)
Here are the involved files:
1. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myfirstapps" >
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
<!-- parent activity-->
<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>
<!--second activity -->
<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName=".MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MyActivity" />
</activity>
</application>
</manifest>
- MyActivity.java
package com.mycompany.myfirstapps;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;
import android.widget.EditText;
public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE="com.mycompany.myfirstapps.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
//getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//called when user click the button
public void sendMessage(View view){
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText editText=(EditText)findViewById(R.id.edit_message);
String message=editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
private void openSearch() {
//do something
}
private void openSettings() {
//do something
}
}
- DisplayMessageActivity.java
package com.mycompany.myfirstapps;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
//create the text view
String message=intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
TextView textview= new TextView(this);
textview.setTextSize(40);
textview.setText(message);
//set the text view as the activity layout
setContentView(textview);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_display_message, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
- activity_main.xml
<LinearLayout 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:orientation="horizontal"
android:theme="@style/CustomActionBarTheme">
<EditText
android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
- build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.mycompany.myfirstapps"
minSdkVersion 8
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
}
Logcat
05-14 22:57:03.139 2514-2562/android.process.acore I/ContactLocale﹕ AddressBook Labels [en-US]: [, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, Α, Β, Γ, Δ, Ε, Ζ, Η, Θ, Ι, Κ, Λ, Μ, Ν, Ξ, Ο, Π, Ρ, Σ, Τ, Υ, Φ, Χ, Ψ, Ω, , А, Б, В, Г, Д, Ђ, Е, Є, Ж, З, И, І, Ї, Й, Ј, К, Л, Љ, М, Н, Њ, О, П, Р, С, Т, Ћ, У, Ф, Х, Ц, Ч, Џ, Ш, Щ, Ю, Я, , א, ב, ג, ד, ה, ו, ז, ח, ט, י, כ, ל, מ, נ, ס, ע, פ, צ, ק, ר, ש, ת, , ا, ب, ت, ث, ج, ح, خ, د, ذ, ر, ز, س, ش, ص, ض, ط, ظ, ع, غ, ف, ق, ك, ل, م, ن, ه, و, ي, , ก, ข, ฃ, ค, ฅ, ฆ, ง, จ, ฉ, ช, ซ, ฌ, ญ, ฎ, ฏ, ฐ, ฑ, ฒ, ณ, ด, ต, ถ, ท, ธ, น, บ, ป, ผ, ฝ, พ, ฟ, ภ, ม, ย, ร, ฤ, ล, ฦ, ว, ศ, ษ, ส, ห, ฬ, อ, ฮ, , ㄱ, ㄴ, ㄷ, ㄹ, ㅁ, ㅂ, ㅅ, ㅇ, ㅈ, ㅊ, ㅋ, ㅌ, ㅍ, ㅎ, , あ, か, さ, た, な, は, ま, や, ら, わ, #, ]
05-14 22:57:05.181 2514-2532/android.process.acore V/BackupServiceBinder﹕ doBackup() invoked
05-14 22:57:05.508 2514-2532/android.process.acore I/art﹕ WaitForGcToComplete blocked for 267.334ms for cause DisableMovingGc
05-14 22:57:05.619 2514-2532/android.process.acore E/DictionaryBackupAgent﹕ Couldn't read from the cursor
05-14 22:57:05.722 2514-2562/android.process.acore I/art﹕ WaitForGcToComplete blocked for 357.236ms for cause DisableMovingGc
05-15 01:35:32.770 2514-2525/android.process.acore W/art﹕ Suspending all threads took: 57.246ms
05-15 01:35:35.813 2514-2527/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:57)
at android.os.Binder.execTransact(Binder.java:446)
05-15 01:35:35.925 2514-2527/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:64)
at android.os.Binder.execTransact(Binder.java:446)
05-15 01:35:36.578 2514-2527/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:71)
at android.os.Binder.execTransact(Binder.java:446)
What could be wrong?
Move your EditText inside of your onCreate() method in MyActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText=(EditText)findViewById(R.id.edit_message);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
//getActionBar().setDisplayHomeAsUpEnabled(true);
}
And inside your sendMessage() method, just remove that line.
Edit: It seems like you're extending from the new AppCompatActivity. Try changing it to ActionBarActivity in both the activities. For example:
public class MyActivity extends ActionBarActivity {...}
public class DisplayMessageActivity extends ActionBarActivity {...}
Edit: If you're planning on not using the ActionBarActivity, then you need to define a custom Toolbar and initialze it like this..
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
这篇关于应用程序崩溃时pressing按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!