更改意图时 Android Studio 崩溃
Posted
技术标签:
【中文标题】更改意图时 Android Studio 崩溃【英文标题】:Android studio crashed when changing intent 【发布时间】:2015-04-29 09:28:47 【问题描述】:程序应该像这样工作,显示一个计时器和一张图片,使用名为“10-11 岁”的底部应该将用户切换到另一个具有相同计时器设置但显示不同图片的屏幕。 问题是当涉及到代码的意图部分时它崩溃了。 如果第二个活动类只有其默认代码,它可以正常工作,但是当从活动 1 添加相同的代码时,它会崩溃。消息“不幸的是,程序已停止工作” 添加了日志
公共类 Age_7_to_9 扩展 Activity 实现 OnClickListener
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button StartB;
private Button NextB;
public TextView text;
private final long startTime = 30* 1000;
private final long interval = 1* 1000;
ImageView imageView1;
int len=images.length-1;
static int curr=0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_age_7_to_9);
imageView1=(ImageView)findViewById(R.id.imageView);
StartB = (Button) this.findViewById(R.id.button);
NextB = (Button) this.findViewById(R.id.tenEleven);
StartB.setOnClickListener(this);
NextB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime / 1000));
private static final int[] images=new int[] R.drawable.p,R.drawable.pp,;
@Override
public void onClick(View v)
switch (v.getId())
case R.id.button:
if(curr < 2)
imageView1.setImageResource(images[curr]);
else
curr=-1;
curr++;
if(!timerHasStarted)
countDownTimer.start();
timerHasStarted = true;
StartB.setText("STOP");
else
countDownTimer.cancel();
timerHasStarted = false;
StartB.setText("RESTART");
Log.i("MyActivity", "MyClass.getView() — get item number " + curr);
break;
case R.id.tenEleven:
Intent i = new Intent(this, Age_10_to_11.class);
startActivity(i);
break;
public class MyCountDownTimer extends CountDownTimer
public MyCountDownTimer(long startTime, long interval)
super(startTime, interval);
@Override
public void onFinish()
text.setText("Time's Up!");
@Override
public void onTick(long millisUntilFinished)
text.setText(""+ millisUntilFinished / 1000);
@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_age_7_to_9, 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);
XML 代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_
android:layout_ android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Age_7_to_9"
android:background="@drawable/backgroundimage">
<Button
android:layout_
android:layout_
android:text="Start"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="99dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_
android:layout_
android:text="Age 10-11"
android:id="@+id/tenEleven"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="onClick"
/>
<TextView
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/timer"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp" />
<ImageView
android:layout_
android:layout_
android:id="@+id/imageView"
android:layout_above="@+id/timer"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp" />
<TextView
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Highscore:"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/textView2"
android:layout_alignTop="@+id/textView4"
android:layout_alignRight="@+id/tenEleven"
android:layout_alignEnd="@+id/tenEleven" />
<TextView
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Counter:"
android:id="@+id/textView3"
android:layout_alignTop="@+id/textView2"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button" />
<TextView
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/textView4"
android:layout_alignTop="@+id/textView"
android:layout_toLeftOf="@+id/button"
android:layout_toStartOf="@+id/button" />
</RelativeLayout>
在下方登录
04-29 10:29:00.390 2198-2198/com.example.daniel.pmp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.daniel.pmp, PID: 2198
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.daniel.pmp/com.example.daniel.pmp.Age_10_to_11: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.example.daniel.pmp.Age_10_to_11.onCreate(Age_10_to_11.java:46)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
【问题讨论】:
请发布 logcat 输出.. 要发布 logcat,只需在您的 IDE(Eclipse 或 Android Studio)中激活 logcat 并标记它,然后将其复制粘贴到此处。之前查看该 logcat,然后发布您的应用程序的异常部分.. 这个问题与您的应用程序有关,还是与 Android Studio 有关。你的标题和内容不匹配。 【参考方案1】:没有看到 Logcat 就很难知道,但是如果我查看您的代码,我会发现这里有一些问题。不知道这是否只是复制/粘贴错误。
在初始化 images
之前,您正在初始化 len
:
int len=images.length-1;
之后你正在初始化图像:
private static final int[] images=new int[] R.drawable.p,R.drawable.pp,;
这不起作用,如果图像未初始化,您将无法获得长度。这导致我们犯了第二个错误:
您的图像数组在列表末尾有一个逗号:
R.drawable.p,R.drawable.pp, <-- that could not work.
另外,你在这里使用了错误的参数:
case R.id.tenEleven:
Intent i = new Intent(this, Age_10_to_11.class);
startActivity(i);
break;
必须是Age_7_to_9.this
,而不是“this”:
case R.id.tenEleven:
Intent i = new Intent(Age_7_to_9.this, Age_10_to_11.class);
startActivity(i);
break;
否则您将引用 OnClickListener。
由于这三个错误,通常您的应用程序无法编译。但我猜是什么导致了你的崩溃,你没有将Age_10_to_11
Activity 添加到你的清单中。但是要确切地知道它,你必须发布你的 logcat...
【讨论】:
以上是关于更改意图时 Android Studio 崩溃的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio - 在片段之间更改时底部导航崩溃
启动 Unity 时应用程序崩溃 - 在 Android Studio 中测试时不会崩溃