简体   繁体   English

更改意图时Android Studio崩溃

[英]Android studio crashed when changing intent

The program should work like this, having a timer and a picture shown, using the bottom called "Age 10-11" should swap the user to another screen with the same setup of timer, but with different pictures being shown. 该程序应这样工作,并显示一个计时器和一张图片,使用称为“年龄10-11”的底部应将用户切换到具有相同计时器设置但显示不同图片的另一个屏幕。 The problem is it crashed when it comes to the intent part of the code. 问题是当涉及到代码的意图部分时,它崩溃了。 it works fine if the second activity class has only its default code, but when adding the same code from activity 1 it crashes. 如果第二个活动类仅具有其默认代码,则该方法工作正常,但是在活动1中添加相同的代码时,它将崩溃。 Message "Unfortunately, program has stopped working" Added the log 消息“很遗憾,程序已停止工作”已添加日志

public class Age_7_to_9 extends Activity implements OnClickListener { 公共类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 code 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>

Log below 在下面登录

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)

Without seeing Logcat it will be difficult to know, but if I look into Your code, I see some things going wrong here. 如果没有看到Logcat,将很难知道,但是如果我查看您的代码,就会发现这里有些问题。 Don´t know if it is just a copy/paste mistake. 不知道这是否只是复制/粘贴错误。

You are initializing len BEFORE initializing images : 您正在初始化len然后再初始化images

int len=images.length-1;

after that You are initializing images: 之后,您正在初始化图像:

private static final int[] images=new int[]     {R.drawable.p,R.drawable.pp,};

That will not work, You can´t get a length if images is not initialized. 那将不起作用,如果图像未初始化,则无法获得长度。 That leads us to the second mistake: 这导致我们出现第二个错误:

Your images array has a comma at the end of the list: 图片数组的逗号在列表的末尾:

 {R.drawable.p,R.drawable.pp,} <-- that could not work.

Also, You are using the wrong parameter here: 另外,您在此处使用了错误的参数:

    case R.id.tenEleven:
            Intent i = new Intent(this, Age_10_to_11.class);
            startActivity(i);
            break;

instead of "this" it must be Age_7_to_9.this : 代替“ this”,它必须是Age_7_to_9.this

     case R.id.tenEleven:
            Intent i = new Intent(Age_7_to_9.this, Age_10_to_11.class);
            startActivity(i);
            break;

otherwise You will reference the OnClickListener. 否则,您将引用OnClickListener。

Because of these three mistakes, usually Your app would not compile. 由于这三个错误,通常您的应用无法编译。 But what I guess what is causing Your crash is, that You don´t have added the Age_10_to_11 Activity to Your manifest.But to know it exactly, You have to post Your logcat... 但是我想造成崩溃的原因是,您没有在清单中添加Age_10_to_11活动。但是要确切知道,您必须发布日志...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM