这是调用Activity:
package com.wikibuyers.consumocarburante;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageButton;
import android.widget.TextView;
public class RisultatoActivity extends Activity{
public static final String EXTRA_MESSAGE = "Chilometri percorsi Litri consumati";
public static final String EXTRA_MESSAGE_2 = "Percorrenza e consumo";
public static final String EXTRA_MESSAGE_3 = "Frase da condividere";
private static final int DIALOG_ALERT_ID = 1;
private String[] extraMessages2 = new String[2];
TextView txt=null;
TextView txt1=null;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.risultato);
txt = (TextView) findViewById(R.id.textView47);
txt1 = (TextView) findViewById(R.id.textView48);
Typeface font = Typeface.createFromAsset(getAssets(), "LCD-BOLD.TTF");
txt.setTypeface(font);
txt1.setTypeface(font);
Intent intent = getIntent();
String[] messages = intent.getStringArrayExtra(Passo2Activity.EXTRA_MESSAGE);
double km = Double.parseDouble(messages[0]);
double litri = Double.parseDouble(messages[1]);
double consumo = 0;
double percorrenza = 0;
try{
consumo = (km/litri);
percorrenza = (litri*100)/km;
} catch(ArithmeticException e){
showDialog(DIALOG_ALERT_ID);
}
String temp1 = String.valueOf(consumo);
String temp2 = String.valueOf(percorrenza);
String temp1troncato = String.format("%.2f", consumo);
String temp2troncato = String.format("%.2f", percorrenza);
txt.setText(temp1);
txt1.setText(temp2);
extraMessages2[0] = temp1troncato;
extraMessages2[1] = temp2troncato;
ImageButton bottoneIndietro = (ImageButton)findViewById(R.id.imageButton9);
bottoneIndietro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageButton myButton = (ImageButton) findViewById(R.id.imageButton9 );
Animation anim = new AlphaAnimation(0.0f, 1.0f);// LAMPEGGIO BOTTONE 1
anim.setDuration(20); //SI PUò CONTROLLARE LA DURATA DEL LAMPEGGIO CON QUESTO PARAMETRO
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
myButton.startAnimation(anim);
anim.setRepeatCount(5);
startPasso2Activity();
}
});
ImageButton bottoneHome = (ImageButton)findViewById(R.id.imageButton11);
bottoneHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageButton myButton = (ImageButton) findViewById(R.id.imageButton11 );
Animation anim = new AlphaAnimation(0.0f, 1.0f);// LAMPEGGIO BOTTONE 1
anim.setDuration(20); //SI PUò CONTROLLARE LA DURATA DEL LAMPEGGIO CON QUESTO PARAMETRO
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
myButton.startAnimation(anim);
anim.setRepeatCount(5);
startHomeActivity();
}
});
ImageButton bottoneCondividi = (ImageButton)findViewById(R.id.imageButton6);
bottoneCondividi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageButton myButton = (ImageButton) findViewById(R.id.imageButton6);
Animation anim = new AlphaAnimation(0.0f, 1.0f);// LAMPEGGIO BOTTONE 1
anim.setDuration(20); //SI PUò CONTROLLARE LA DURATA DEL LAMPEGGIO CON QUESTO PARAMETRO
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
myButton.startAnimation(anim);
anim.setRepeatCount(5);
startInserimentoDatiActivityCondividi();
}
});
ImageButton bottonePreferiti = (ImageButton)findViewById(R.id.imageButton4);
bottonePreferiti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageButton myButton = (ImageButton) findViewById(R.id.imageButton4);
Animation anim = new AlphaAnimation(0.0f, 1.0f);// LAMPEGGIO BOTTONE 1
anim.setDuration(20); //SI PUò CONTROLLARE LA DURATA DEL LAMPEGGIO CON QUESTO PARAMETRO
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
myButton.startAnimation(anim);
anim.setRepeatCount(5);
startInserimentoDatiActivityPreferiti();
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
if (id == DIALOG_ALERT_ID){
dialog = createAlertDialog();}
return dialog;
}
private Dialog createAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Attenzione");
builder.setMessage("E' necessario inserire valori diversi da zero in tutte e due le caselle");
builder.setCancelable(false);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(DialogInterface dialog, int which) {
// Intent i = new Intent(getApplicationContext(),Passo2Activity.class);
// startActivity(i);
dismissDialog(DIALOG_ALERT_ID);
}
});
AlertDialog alert = builder.create();
return alert;
}
public void startPasso2Activity(){
Intent i = new Intent(getApplicationContext(),Passo2Activity.class);
startActivity(i);
}
public void startHomeActivity(){
Intent i = new Intent(getApplicationContext(),PrimaPaginaActivity.class);
startActivity(i);
}
public void startInserimentoDatiActivityCondividi(){
Intent i = new Intent(getApplicationContext(),InserimentoDatiActivityCondividi.class);
i.putExtra(EXTRA_MESSAGE_2, extraMessages2);
startActivity(i);
}
public void startInserimentoDatiActivityPreferiti(){
Intent i = new Intent(getApplicationContext(),InserimentoDatiActivityPreferiti.class);
i.putExtra(EXTRA_MESSAGE_2, extraMessages2);
startActivity(i);
}
}
另一个名为Activity:
package com.wikibuyers.consumocarburante;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
public class InserimentoDatiActivityCondividi extends Activity implements OnItemSelectedListener{
public static EditText edit = null;
public static TextView tv = null;
String frase;
private static final int DIALOG_ALERT_ID = 1;
Intent sendIntent = new Intent();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inserimento_dati_mezzo_condividi);
edit = (EditText)findViewById(R.id.editText4);
tv = (TextView)findViewById(R.id.textView53);
Spinner spinner = (Spinner) findViewById(R.id.spinner2);
frase = (String) tv.getText();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, frase);
sendIntent.setType("text/plain");
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.scelta_incipit, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
Intent intent = getIntent();
final String[] messages = intent.getStringArrayExtra(RisultatoActivity.EXTRA_MESSAGE_2);
ImageButton bottoneAggiorna = (ImageButton)findViewById(R.id.imageButton13);
bottoneAggiorna.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageButton myButton = (ImageButton) findViewById(R.id.imageButton13);
Animation anim = new AlphaAnimation(0.0f, 1.0f);// LAMPEGGIO BOTTONE 1
anim.setDuration(20); //SI PUò CONTROLLARE LA DURATA DEL LAMPEGGIO CON QUESTO PARAMETRO
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
myButton.startAnimation(anim);
anim.setRepeatCount(5);
tv.setText(edit.getText()+" percorre "+messages[0]+" km/l e consuma "+messages[1]+" litri per 100 Km");
}
});
ImageButton bottoneIndietro = (ImageButton)findViewById(R.id.imageButton20);
bottoneIndietro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageButton myButton = (ImageButton) findViewById(R.id.imageButton20 );
Animation anim = new AlphaAnimation(0.0f, 1.0f);// LAMPEGGIO BOTTONE 1
anim.setDuration(20); //SI PUò CONTROLLARE LA DURATA DEL LAMPEGGIO CON QUESTO PARAMETRO
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
myButton.startAnimation(anim);
anim.setRepeatCount(5);
startRisultatoActivity();
}
});
ImageButton bottoneHome = (ImageButton)findViewById(R.id.imageButton19);
bottoneHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageButton myButton = (ImageButton) findViewById(R.id.imageButton19 );
Animation anim = new AlphaAnimation(0.0f, 1.0f);// LAMPEGGIO BOTTONE 1
anim.setDuration(20); //SI PUò CONTROLLARE LA DURATA DEL LAMPEGGIO CON QUESTO PARAMETRO
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
myButton.startAnimation(anim);
anim.setRepeatCount(5);
startHomeActivity();
}
});
ImageButton buttonCondivisione = (ImageButton)findViewById(R.id.imageButton16);
buttonCondivisione.setOnClickListener(new View.OnClickListener() {
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB) //warning e varie soppressi per poter agire sulla Clipboard più sotto
@SuppressLint("NewApi")
public void onClick(View v) {
ImageButton buttonCondivisione = (ImageButton)findViewById(R.id.imageButton16);
Animation anim = new AlphaAnimation(0.0f, 1.0f);// LAMPEGGIO BOTTONE RANDOM
anim.setDuration(20); //SI PUò CONTROLLARE LA DURATA DEL LAMPEGGIO CON QUESTO PARAMETRO
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
buttonCondivisione.startAnimation(anim);
anim.setRepeatCount(5);
showDialog(DIALOG_ALERT_ID);
}});
ImageButton bottoneAnnulla = (ImageButton)findViewById(R.id.imageButton17);
bottoneAnnulla.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageButton myButton = (ImageButton) findViewById(R.id.imageButton17);
Animation anim = new AlphaAnimation(0.0f, 1.0f);// LAMPEGGIO BOTTONE 1
anim.setDuration(20); //SI PUò CONTROLLARE LA DURATA DEL LAMPEGGIO CON QUESTO PARAMETRO
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
myButton.startAnimation(anim);
anim.setRepeatCount(5);
if(tv.getText()!=""){tv.setText("");}
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch (id) {
case DIALOG_ALERT_ID:
dialog = createAlertDialog();
break;
default:
dialog = null;
break;
}
return dialog;
}
private Dialog createAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Avviso");
builder.setMessage("Solo per Facebook bisogna incollare il testo sulla casella di condivisione tenendo premuto a lungo e cliccando sul popup ''incolla'' che compare");
builder.setCancelable(false);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(Intent.createChooser(sendIntent, "Condividi la frase con:"));
dismissDialog(DIALOG_ALERT_ID);
}
});
AlertDialog alert = builder.create();
return alert;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
edit.setText((CharSequence) parent.getItemAtPosition(pos));;
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
public void startRisultatoActivity(){
Intent i = new Intent(getApplicationContext(),RisultatoActivity.class);
startActivity(i);
}
public void startHomeActivity(){
Intent i = new Intent(getApplicationContext(),PrimaPaginaActivity.class);
startActivity(i);
}
}
当我在手机上调试应用程序并尝试按下名为bottoneIndietro的后退按钮时,最后那个LogCat出错了。我想问题出现在最后两行LogCat中,它说:&#34; getTextBeforeCursor在非活动的InputConnection&#34;上。我已经搜索过这个问题,但我找不到我的解决方案。任何建议将不胜感激。感谢。
07-05 16:20:29.216: I/dalvikvm(25677): Debugger is active
07-05 16:20:29.255: I/System.out(25677): Debugger has connected
07-05 16:20:29.255: I/System.out(25677): waiting for debugger to settle...
07-05 16:20:29.450: I/System.out(25677): waiting for debugger to settle...
07-05 16:20:29.653: I/System.out(25677): waiting for debugger to settle...
07-05 16:20:29.857: I/System.out(25677): waiting for debugger to settle...
07-05 16:20:30.052: I/System.out(25677): waiting for debugger to settle...
07-05 16:20:30.255: I/System.out(25677): waiting for debugger to settle...
07-05 16:20:30.458: I/System.out(25677): waiting for debugger to settle...
07-05 16:20:30.653: I/System.out(25677): waiting for debugger to settle...
07-05 16:20:30.857: I/System.out(25677): waiting for debugger to settle...
07-05 16:20:31.060: I/System.out(25677): waiting for debugger to settle...
07-05 16:20:31.255: I/System.out(25677): debugger has settled (1474)
07-05 16:20:31.419: D/dalvikvm(25677): GC_FOR_ALLOC freed 79K, 2% free 8932K/9040K, paused 32ms, total 33ms
07-05 16:20:31.427: I/dalvikvm-heap(25677): Grow heap (frag case) to 10.487MB for 1822516-byte allocation
07-05 16:20:31.458: D/dalvikvm(25677): GC_FOR_ALLOC freed <1K, 2% free 10711K/10820K, paused 24ms, total 24ms
07-05 16:20:31.474: D/dalvikvm(25677): GC_CONCURRENT freed <1K, 2% free 10711K/10820K, paused 3ms+1ms, total 17ms
07-05 16:20:31.544: D/dalvikvm(25677): GC_FOR_ALLOC freed <1K, 1% free 10712K/10820K, paused 11ms, total 11ms
07-05 16:20:31.552: I/dalvikvm-heap(25677): Grow heap (frag case) to 12.225MB for 1822516-byte allocation
07-05 16:20:31.560: D/dalvikvm(25677): GC_FOR_ALLOC freed 0K, 1% free 12492K/12600K, paused 14ms, total 14ms
07-05 16:20:31.583: D/dalvikvm(25677): GC_CONCURRENT freed 1K, 1% free 12494K/12600K, paused 3ms+6ms, total 21ms
07-05 16:20:31.724: D/libEGL(25677): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
07-05 16:20:31.739: D/libEGL(25677): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
07-05 16:20:31.755: D/libEGL(25677): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
07-05 16:20:31.849: D/OpenGLRenderer(25677): Enabling debug mode 0
07-05 16:21:07.841: D/dalvikvm(25677): GC_CONCURRENT freed 55K, 1% free 13526K/13612K, paused 6ms+3ms, total 21ms
07-05 16:21:15.607: W/IInputConnectionWrapper(25677): getTextBeforeCursor on inactive InputConnection
07-05 16:21:15.653: W/IInputConnectionWrapper(25677): getTextAfterCursor on inactive InputConnection
07-05 16:21:47.974: D/dalvikvm(25677): GC_CONCURRENT freed 158K, 2% free 14853K/15040K, paused 12ms+4ms, total 29ms
这是按下我的后退按钮后的LogCat。问题解决了:我必须添加一个意外删除的库。
...
07-05 16:20:31.255: I/System.out(25677): debugger has settled (1474)
07-05 16:20:31.419: D/dalvikvm(25677): GC_FOR_ALLOC freed 79K, 2% free 8932K/9040K, paused 32ms, total 33ms
07-05 16:20:31.427: I/dalvikvm-heap(25677): Grow heap (frag case) to 10.487MB for 1822516-byte allocation
07-05 16:20:31.458: D/dalvikvm(25677): GC_FOR_ALLOC freed <1K, 2% free 10711K/10820K, paused 24ms, total 24ms
07-05 16:20:31.474: D/dalvikvm(25677): GC_CONCURRENT freed <1K, 2% free 10711K/10820K, paused 3ms+1ms, total 17ms
07-05 16:20:31.544: D/dalvikvm(25677): GC_FOR_ALLOC freed <1K, 1% free 10712K/10820K, paused 11ms, total 11ms
07-05 16:20:31.552: I/dalvikvm-heap(25677): Grow heap (frag case) to 12.225MB for 1822516-byte allocation
07-05 16:20:31.560: D/dalvikvm(25677): GC_FOR_ALLOC freed 0K, 1% free 12492K/12600K, paused 14ms, total 14ms
07-05 16:20:31.583: D/dalvikvm(25677): GC_CONCURRENT freed 1K, 1% free 12494K/12600K, paused 3ms+6ms, total 21ms
07-05 16:20:31.724: D/libEGL(25677): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
07-05 16:20:31.739: D/libEGL(25677): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
07-05 16:20:31.755: D/libEGL(25677): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
07-05 16:20:31.849: D/OpenGLRenderer(25677): Enabling debug mode 0
07-05 16:21:07.841: D/dalvikvm(25677): GC_CONCURRENT freed 55K, 1% free 13526K/13612K, paused 6ms+3ms, total 21ms
07-05 16:21:15.607: W/IInputConnectionWrapper(25677): getTextBeforeCursor on inactive InputConnection
07-05 16:21:15.653: W/IInputConnectionWrapper(25677): getTextAfterCursor on inactive InputConnection
07-05 16:21:47.974: D/dalvikvm(25677): GC_CONCURRENT freed 158K, 2% free 14853K/15040K, paused 12ms+4ms, total 29ms
编辑建议后的最后一次LogCat:
...
07-05 17:29:40.497: I/System.out(27349): debugger has settled (1389)
07-05 17:29:40.661: D/dalvikvm(27349): GC_FOR_ALLOC freed 57K, 1% free 8932K/9020K, paused 27ms, total 29ms
07-05 17:29:40.661: I/dalvikvm-heap(27349): Grow heap (frag case) to 10.487MB for 1822516-byte allocation
07-05 17:29:40.685: D/dalvikvm(27349): GC_FOR_ALLOC freed <1K, 1% free 10711K/10800K, paused 19ms, total 19ms
07-05 17:29:40.700: D/dalvikvm(27349): GC_CONCURRENT freed <1K, 1% free 10711K/10800K, paused 3ms+2ms, total 20ms
07-05 17:29:40.786: D/dalvikvm(27349): GC_FOR_ALLOC freed <1K, 1% free 10712K/10800K, paused 10ms, total 10ms
07-05 17:29:40.786: I/dalvikvm-heap(27349): Grow heap (frag case) to 12.225MB for 1822516-byte allocation
07-05 17:29:40.802: D/dalvikvm(27349): GC_FOR_ALLOC freed <1K, 1% free 12492K/12580K, paused 18ms, total 18ms
07-05 17:29:40.817: D/dalvikvm(27349): GC_CONCURRENT freed 1K, 1% free 12496K/12580K, paused 2ms+2ms, total 14ms
07-05 17:29:40.872: D/libEGL(27349): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
07-05 17:29:40.880: D/libEGL(27349): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
07-05 17:29:40.880: D/libEGL(27349): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
07-05 17:29:40.974: D/OpenGLRenderer(27349): Enabling debug mode 0
07-05 17:30:52.685: D/dalvikvm(27349): GC_CONCURRENT freed 58K, 1% free 13524K/13612K, paused 3ms+3ms, total 37ms
07-05 17:31:00.880: W/IInputConnectionWrapper(27349): getTextBeforeCursor on inactive InputConnection
07-05 17:31:00.935: W/IInputConnectionWrapper(27349): getTextAfterCursor on inactive InputConnection
答案 0 :(得分:0)
public void startRisultatoActivity(){
finish();
}
这与onbackpressed相同
调用finish会从backstack中删除当前活动,而调用new活动会将当前活动添加到backstack