我的应用中有几项活动。我想从其他两个独立的,B和C开始一个活动。 从B A开始好,从C它提醒ANR建议。考虑到:
1. Activities are all declared in the Manifest
2. There are no incoerent castings
这是必须打开的活动A:
包com.wikibuyers.aforismi;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class ElencoAforismiActivity extends Activity{
public final static String EXTRA_MESSAGE = "com.example.provacomunicazionefraactivity.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.elecoalfabeticoautori);
Button bottoneA = (Button)findViewById(R.id.buttonA);
Button bottoneB = (Button)findViewById(R.id.buttonB);
Button bottoneC = (Button)findViewById(R.id.buttonC);
[...]
Button bottoneX = (Button)findViewById(R.id.buttonX);
Button bottoneY = (Button)findViewById(R.id.buttonY);
Button bottoneZ = (Button)findViewById(R.id.buttonZ);
ImageButton Preferiti = (ImageButton)findViewById(R.id.buttonPreferiti);
bottoneA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button myButton = (Button) findViewById(R.id.buttonA );
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);
String message = "A" ;
Intent i = new Intent(getApplicationContext(), LetteraElencoSelezionataActivity.class);
i.putExtra(EXTRA_MESSAGE, message);
startActivity(i);
}
});
bottoneB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button myButton = (Button) findViewById(R.id.buttonB );
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);
String message = "B" ;
Intent i = new Intent(getApplicationContext(), LetteraElencoSelezionataActivity.class);
i.putExtra(EXTRA_MESSAGE, message);
startActivity(i);
}
});
bottoneC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button myButton = (Button) findViewById(R.id.buttonC );
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);
String message = "C" ;
Intent i = new Intent(getApplicationContext(), LetteraElencoSelezionataActivity.class);
i.putExtra(EXTRA_MESSAGE, message);
startActivity(i);
}
});
[...omissis]
Preferiti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {ImageButton myButton = (ImageButton) findViewById(R.id.buttonPreferiti );
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);
Intent i = new Intent(getApplicationContext(), PreferitiActivity.class);
// i.putExtra(EXTRA_MESSAGE, message);
startActivity(i);
}
});
final Button BT1 = (Button)findViewById(R.id.buttonA);
ViewTreeObserver vto1 = BT1.getViewTreeObserver();
vto1.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
LayerDrawable ld1 = (LayerDrawable)BT1.getBackground();
ld1.setLayerInset(1,0, 0, 0, BT1.getHeight()/2);}});
final Button BT2 = (Button)findViewById(R.id.buttonB);
ViewTreeObserver vto2 = BT2.getViewTreeObserver();
vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
LayerDrawable ld1 = (LayerDrawable)BT2.getBackground();
ld1.setLayerInset(1,0, 0, 0, BT2.getHeight()/2);}});
final Button BT3 = (Button)findViewById(R.id.buttonC);
ViewTreeObserver vto3 = BT3.getViewTreeObserver();
vto3.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
LayerDrawable ld1 = (LayerDrawable)BT3.getBackground();
ld1.setLayerInset(1,0, 0, 0, BT3.getHeight()/2);}});
[.........omissis]
final Button BT16 = (Button)findViewById(R.id.buttonP);
ViewTreeObserver vto16 = BT16.getViewTreeObserver();
vto16.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
LayerDrawable ld1 = (LayerDrawable)BT16.getBackground();
ld1.setLayerInset(1,0, 0, 0, BT16.getHeight()/2);}});
}
} LogCat是这样的:(对不起,我以前从未使用过它)
09-09 17:02:54.160: W/dalvikvm(21991): threadid=1: thread exiting with uncaught
exception (group=0x41476930)
09-09 17:02:54.223: E/AndroidRuntime(21991): FATAL EXCEPTION: main
09-09 17:02:54.223: E/AndroidRuntime(21991): java.lang.RuntimeException: Unable to
start activity ComponentInfo
{com.wikibuyers.aforismi/com.wikibuyers.
java.lang.ArrayIndexOutOfBoundsException: length=48;
index=48
09-09 17:02:54.223: E/AndroidRuntime(21991): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-09 17:02:54.223: E/AndroidRuntime(21991): at
android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2230)
09-09 17:02:54.223: E/AndroidRuntime(21991):
at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-09 17:02:54.223: E/AndroidRuntime(21991):
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-09 17:02:54.223: E/AndroidRuntime(21991): at android.os.Handler.dispatchMessage
(Handler.java:99)
09-09 17:02:54.223: E/AndroidRuntime(21991): at android.os.Looper.loop(Looper.java:137)
09-09 17:02:54.223: E/AndroidRuntime(21991): at android.app.ActivityThread.main
(ActivityThread.java:5039)
09-09 17:02:54.223: E/AndroidRuntime(21991): at java.lang.reflect.Method.invokeNative(Native
Method)
09-09 17:02:54.223: E/AndroidRuntime(21991): at java.lang.reflect.Method.invoke
(Method.java:511)
09-09 17:02:54.223: E/AndroidRuntime(21991): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-09 17:02:54.223: E/AndroidRuntime(21991): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:560)
09-09 17:02:54.223: E/AndroidRuntime(21991): at dalvik.system.NativeStart.main(Native Method)
09-09 17:02:54.223: E/AndroidRuntime(21991): Caused by:
java.lang.ArrayIndexOutOfBoundsException: length=48; index=48
09-09 17:02:54.223: E/AndroidRuntime(21991): at
com.wikibuyers.aforismi.RandomActivity.onCreate
(RandomActivity.java:1022)
09-09 17:02:54.223: E/AndroidRuntime(21991): at android.app.Activity.performCreate
(Activity.java:5104)
09-09 17:02:54.223: E/AndroidRuntime(21991): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-09 17:02:54.223: E/AndroidRuntime(21991): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-09 17:02:54.223: E/AndroidRuntime(21991): ... 11 more
09-09 17:03:32.918: D/AndroidRuntime(22223): Shutting down VM
09-09 17:03:32.918: W/dalvikvm(22223): threadid=1: thread exiting with uncaught exception
(group=0x41476930)
09-09 17:03:32.942: E/AndroidRuntime(22223): FATAL EXCEPTION: main
09-09 17:03:32.942: E/AndroidRuntime(22223): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.wikibuyers.aforismi/com.wikibuyers.aforismi.PreferitiActivity}:
java.lang.NullPointerException
09-09 17:03:32.942: E/AndroidRuntime(22223): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-09 17:03:32.942: E/AndroidRuntime(22223): at
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2230)
09-09 17:03:32.942: E/AndroidRuntime(22223): at android.app.ActivityThread.access$600
(ActivityThread.java:141)
09-09 17:03:32.942: E/AndroidRuntime(22223): at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1234)
09-09 17:03:32.942: E/AndroidRuntime(22223): at android.os.Handler.dispatchMessage
(Handler.java:99)
09-09 17:03:32.942: E/AndroidRuntime(22223): at android.os.Looper.loop(Looper.java:137)
09-09 17:03:32.942: E/AndroidRuntime(22223): at android.app.ActivityThread.main
(ActivityThread.java:5039)
09-09 17:03:32.942: E/AndroidRuntime(22223): at java.lang.reflect.Method.invokeNative(Native
Method)
09-09 17:03:32.942: E/AndroidRuntime(22223): at java.lang.reflect.Method.invoke
(Method.java:511)
09-09 17:03:32.942: E/AndroidRuntime(22223): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-09 17:03:32.942: E/AndroidRuntime(22223): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:560)
09-09 17:03:32.942: E/AndroidRuntime(22223): at dalvik.system.NativeStart.main(Native Method)
09-09 17:03:32.942: E/AndroidRuntime(22223): Caused by: java.lang.NullPointerException
09-09 17:03:32.942: E/AndroidRuntime(22223): at android.app.ContextImpl.makeFilename
(ContextImpl.java:1963)
09-09 17:03:32.942: E/AndroidRuntime(22223): at android.app.ContextImpl.openFileInput
(ContextImpl.java:692)
09-09 17:03:32.942: E/AndroidRuntime(22223): at android.content.ContextWrapper.openFileInput
(ContextWrapper.java:167)
09-09 17:03:32.942: E/AndroidRuntime(22223): at
com.wikibuyers.aforismi.PreferitiActivity.onCreate(PreferitiActivity.java:49)
09-09 17:03:32.942: E/AndroidRuntime(22223): at android.app.Activity.performCreate
(Activity.java:5104)
09-09 17:03:32.942: E/AndroidRuntime(22223): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-09 17:03:32.942: E/AndroidRuntime(22223): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-09 17:03:32.942: E/AndroidRuntime(22223): ... 11 more
09-09 17:03:57.676: I/Process(22223): Sending signal. PID: 22223 SIG: 9
09-09 17:03:57.981: D/OpenGLRenderer(22275): Enabling debug mode 0
09-09 17:04:04.801: D/AndroidRuntime(22275): Shutting down VM
09-09 17:04:04.801: W/dalvikvm(22275): threadid=1: thread exiting with uncaught exception
(group=0x41476930)
09-09 17:04:04.817: E/AndroidRuntime(22275): FATAL EXCEPTION: main
09-09 17:04:04.817: E/AndroidRuntime(22275): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.wikibuyers.aforismi/com.wikibuyers.aforismi.PreferitiActivity}:
java.lang.NullPointerException
09-09 17:04:04.817: E/AndroidRuntime(22275): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-09 17:04:04.817: E/AndroidRuntime(22275): at
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2230)
09-09 17:04:04.817: E/AndroidRuntime(22275): at android.app.ActivityThread.access$600
(ActivityThread.java:141)
09-09 17:04:04.817: E/AndroidRuntime(22275): at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1234)
09-09 17:04:04.817: E/AndroidRuntime(22275): at android.os.Handler.dispatchMessage
(Handler.java:99)
09-09 17:04:04.817: E/AndroidRuntime(22275): at android.os.Looper.loop(Looper.java:137)
09-09 17:04:04.817: E/AndroidRuntime(22275): at android.app.ActivityThread.main
(ActivityThread.java:5039)
09-09 17:04:04.817: E/AndroidRuntime(22275): at java.lang.reflect.Method.invokeNative(Native
Method)
09-09 17:04:04.817: E/AndroidRuntime(22275): at java.lang.reflect.Method.invoke
(Method.java:511)
09-09 17:04:04.817: E/AndroidRuntime(22275): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-09 17:04:04.817: E/AndroidRuntime(22275): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:560)
09-09 17:04:04.817: E/AndroidRuntime(22275): at dalvik.system.NativeStart.main(Native Method)
09-09 17:04:04.817: E/AndroidRuntime(22275): Caused by: java.lang.NullPointerException
09-09 17:04:04.817: E/AndroidRuntime(22275): at android.app.ContextImpl.makeFilename
(ContextImpl.java:1963)
09-09 17:04:04.817: E/AndroidRuntime(22275): at android.app.ContextImpl.openFileInput
(ContextImpl.java:692)
09-09 17:04:04.817: E/AndroidRuntime(22275): at android.content.ContextWrapper.openFileInput
(ContextWrapper.java:167)
09-09 17:04:04.817: E/AndroidRuntime(22275): at
com.wikibuyers.aforismi.PreferitiActivity.onCreate(PreferitiActivity.java:49)
09-09 17:04:04.817: E/AndroidRuntime(22275): at android.app.Activity.performCreate
(Activity.java:5104)
09-09 17:04:04.817: E/AndroidRuntime(22275): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-09 17:04:04.817: E/AndroidRuntime(22275): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-09 17:04:04.817: E/AndroidRuntime(22275): ... 11 more
09-09 17:04:04.832: D/dalvikvm(22275): GC_CONCURRENT freed 350K, 4% free 9283K/9664K, paused
2ms+4ms, total 23ms
活动PreferitiActivity.java是这样的:
package com.wikibuyers.aforismi;
import java.io.FileNotFoundException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.Writer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
public class PreferitiActivity extends Activity{
public final static String EXTRA_MESSAGE = "com.example.provare.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.preferiti);
Intent intent = getIntent();
String message = intent.getStringExtra(SingolaFraseSelezionataActivity.EXTRA_MESSAGE);
Writer writer = null;
try {
//FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
// SPOSTA IL SALVATAGGIO DEL FILE IN PREFERITI IN MODO DA AVERE SALVATAGGIO E LETTURA
// INSIEME E DA QUI MANDA SOLO LA FRASE AI PREFERITI
writer = new OutputStreamWriter(openFileOutput(message,
MODE_PRIVATE)); //SE NON VA APPEND TORNIAMO A PRIVATE
writer.write(message);
Toast.makeText(getApplicationContext(), "Frase aggiunta ai Preferiti", Toast.LENGTH_LONG).show();
}
catch (IOException e) {
Log.e("Aforismi", "Impossibile salvare il file", e);
Toast.makeText(getApplicationContext(), "Errore", Toast.LENGTH_LONG).show();
} finally {
if (writer != null) {
try {
writer.close();
} catch (Throwable t) {
}
}
}
Reader reader = null;
try {
reader = new InputStreamReader(openFileInput(message));
StringBuffer aux = new StringBuffer();
char[] buf = new char[1024];
int len;
while ((len = reader.read(buf)) != -1) {
aux.append(buf, 0, len);
}
} catch (FileNotFoundException e) {
message = "";
} catch (IOException e) {
Log.e("FileDemo", "Impossibile aprire il file", e);
message = "";
} finally {
if (reader != null) {
try {
reader.close();
} catch (Throwable t) {
}
}
}
ArrayList<String> FrasiAggiuntePreferiti = new ArrayList<String>();
FrasiAggiuntePreferiti.add(message);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, FrasiAggiuntePreferiti);
ListView listView = (ListView) findViewById(R.id.listView3);
listView.setAdapter(adapter);
}
}
答案 0 :(得分:1)
您的问题与数组越界有关:
java.lang.ArrayIndexOutOfBoundsException: length=48;
据我所知,这发生在您的ActivityThread类的第2180行。
最重要的是,您的代码中存在逻辑错误,这会阻止您的Activity成功启动。修复你的Arrary“off by 1”错误,你应该设置。
答案 1 :(得分:0)
点击按钮后会出现异常吗?如果是这样,请检查您的活动开始代码
Intent i = new Intent(getApplicationContext(), PreferitiActivity.class);
startActivity(i);
否则我想通过阅读您的错误消息,您必须在清单中定义活动c
<activity android:name="com.wikibuyers.aforismi.C" >
修改强>
你有一个ArrayIndexOutOfBoundsException,你有列表,你没有发布的数组,它们导致了问题...欢呼:)
答案 2 :(得分:0)
由于我的态度发生了变化,我必须结束讨论。我已经明白我应该更好地使用数据库来实现我的目标,所以我会改写我的应用程序。 感谢所有人的宝贵答案。