当用户点按新按钮时,应用关闭(Android Studio)

时间:2016-05-16 17:38:16

标签: java android xml

我是计算机编程的新手。我是自学Java,所以请对我很轻松。我可能不理解您使用的所有技术术语。我更喜欢直接重写的代码解决方案,但是如果你决定解释问题是什么,以及如何解决它,请向我解释,就像我是五年级学生一样。

简而言之,我正在尝试创建一个Android应用程序,以便在其MainActivity上有一个图像按钮。当用户点击图像按钮时,应该将他带到我称为NewActivity的新活动。但是,当我在Nexus 6仿真器上运行应用程序时,单击图像按钮时会崩溃。

我插入主要活动的代码是:

package com.example.matthew.test1;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {
//Main Menu: MarioActivity1 to MarioActivity2
    ImageButton myImageButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myImageButton= (ImageButton) findViewById(R.id.myImageButton);

        myImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentLoadNewActivity = new Intent(MainActivity.this, NewActivity.class);
                startActivity(intentLoadNewActivity);

            }
        });
    }
}
//End

现在应用程序实际上可以正常使用此代码,因为我从教程中获得了它。基本上,我成功地让应用程序有一个图像按钮,导致一个新的活动。当我从另一个教程中获取代码时出现了问题。

本教程尝试创建一种“幻灯片”。我的意思是代码尝试在后台创建图像,以及两个按钮(上一个和下一个)。加载的第一个图像应该是Image1(例如),然后如果单击Next它应该显示Image2,或者如果你按Imagei的Prev它应该返回到Image1等。

现在,虽然本教程将代码应用于MainActivity(即MainActivity.java和activity_main.xml),但我正在尝试将代码应用于NewActivity。

因此,这是我插入activity_new.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.matthew.test1.NewActivity">

    <ViewFlipper
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/viewFlipper">

    </ViewFlipper>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView"
            android:scaleType="centerCrop"
            android:src="@drawable/mario_bair_1"/>

        <ImageView
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:id="@+id/ImageView2"
             android:scaleType="centerCrop"
             android:src="@drawable/mario_bair_2"/>

         <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView3"
            android:scaleType="centerCrop"
            android:src="@drawable/mario_bair_3"/>

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <Button
        android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Prev"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignTop="@+id/next"/>

</RelativeLayout>

这是我在NewActivity.java中的代码:

package com.example.matthew.test1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;


public class NewActivity extends AppCompatActivity implements View.OnClickListener {

    ViewFlipper viewFlipper;
    Button next;
    Button previous;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
        next = (Button) findViewById(R.id.next);
        previous = (Button) findViewById(R.id.previous);

        next.setOnClickListener(this);
        previous.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v == next) {
            viewFlipper.showNext();
        }
        else if (v == previous) {
            viewFlipper.showPrevious();
        }
    }
}

最后,这是Android监视器中显示的错误日志:

05-16 13:20:28.023 19225-19225/com.example.matthew.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.matthew.test1-2/lib/x86
05-16 13:20:28.640 19225-19225/com.example.matthew.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.matthew.test1-2/lib/x86
05-16 13:20:29.690 19225-19225/com.example.matthew.test1 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-16 13:20:29.702 19225-19235/com.example.matthew.test1 I/art: Background sticky concurrent mark sweep GC freed 8434(1445KB) AllocSpace objects, 1(20KB) LOS objects, 71% free, 1064KB/3MB, paused 9.462ms total 71.078ms
05-16 13:20:29.872 19225-19277/com.example.matthew.test1 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                           [ 05-16 13:20:29.876 19225:19225 D/         ]
                                                                           HostConnection::get() New Host Connection established 0xaaa153d0, tid 19225


                                                                           [ 05-16 13:20:29.965 19225:19277 D/         ]
                                                                           HostConnection::get() New Host Connection established 0xaaa15670, tid 19277
05-16 13:20:29.972 19225-19277/com.example.matthew.test1 I/OpenGLRenderer: Initialized EGL, version 1.4
05-16 13:20:30.040 19225-19277/com.example.matthew.test1 W/EGL_emulation: eglSurfaceAttrib not implemented
05-16 13:20:30.040 19225-19277/com.example.matthew.test1 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaa32600, error=EGL_SUCCESS
05-16 13:20:39.924 19225-19225/com.example.matthew.test1 D/AndroidRuntime: Shutting down VM
05-16 13:20:39.924 19225-19225/com.example.matthew.test1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.matthew.test1, PID: 19225
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matthew.test1/com.example.matthew.test1.NewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at com.example.matthew.test1.NewActivity.onCreate(NewActivity.java:24)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
05-16 13:20:40.998 19225-19231/com.example.matthew.test1 W/art: Suspending all threads took: 5.032ms
05-16 13:20:42.104 19225-19225/com.example.matthew.test1 I/Process: Sending signal. PID: 19225 SIG: 9

从幻灯片教程中获取的代码本身就是成功的,如教程中所示。当我将它与MainActivity中的代码结合使用时,问题似乎就出现了。

同样,当我单击MainActivity中导致NewActivity的按钮时,我基本上只希望App不强制关闭(它当前正在执行)。

提前感谢您的帮助!

0 个答案:

没有答案