gpt4 book ai didi

java - Android studio 改变 Intent 时崩溃

转载 作者:行者123 更新时间:2023-12-01 11:36:26 25 4
gpt4 key购买 nike

程序应该像这样工作,有一个计时器和显示的图片,使用名为“Age 10-11”的底部应该将用户切换到另一个具有相同计时器设置的屏幕,但显示不同的图片。问题是当涉及到代码的 Intent 部分时它崩溃了。如果第二个 Activity 类只有默认代码,它可以正常工作,但是当从 Activity 1 添加相同的代码时,它会崩溃。消息“不幸的是,程序已停止工作”添加日志

公共(public)类 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_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" tools:context=".Age_7_to_9"
android:background="@drawable/backgroundimage">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_above="@+id/timer"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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 ComponentInfo{com.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,就很难知道,但是如果我查看你的代码,我会发现这里出了一些问题。不知道这是否只是复制/粘贴错误。

您正在初始化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 添加到您的 list 中。但要准确了解它,您必须发布您的 logcat...

关于java - Android studio 改变 Intent 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com