将imageview添加到imageView时出现Android运行时错误

时间:2015-10-08 14:59:47

标签: android android-layout android-intent android-activity android-studio

**当我运行应用程序时,它适用于主要活动,但在按下按钮更改活动后,应用程序停止并显示运行时错误**

这是我的主要活动,当我在这里添加图片视图时,我在logcat中遇到运行时错误

    import android.content.Context;
    import android.content.Intent;
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.SeekBar;
    import android.widget.TextView;

    import java.io.IOException;

    public class MainActivity extends AppCompatActivity {

        private Button button_Play;
        private Button button_Stop;
        private String STRING_URL = "http://listen.radionomy.com/Dutch-Trap--HipHop";
        private MediaPlayer mPlayer;
        private SeekBar volumeSeekbar = null;
        private AudioManager audioManager = null;
        private ImageView imageView;


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


            imageView = (ImageView)findViewById(R.id.imageView3);
            imageView.setImageResource(R.mipmap.radio1);
            button_Play = (Button)findViewById(R.id.button_Play);
            button_Stop = (Button)findViewById(R.id.button_Stop);
            Button button_next = (Button)findViewById(R.id.button_Next);
            button_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this , secondActivity.class);
                    startActivity(intent);
                    mPlayer.stop();

                    imageView.setImageResource(R.mipmap.radio2);

                }
            });

            Button button_previous = (Button)findViewById(R.id.button_Previous);
            button_previous.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, fifthActivity.class);
                    startActivity(intent);
                    mPlayer.stop();

                }
            });



            mPlayer = new MediaPlayer();

            button_Play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    try {
                        mPlayer.reset();
                        mPlayer.setDataSource(STRING_URL);
                        mPlayer.prepareAsync();
                        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mp) {
                                mp.start();

                            }
                        });


                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            });

            button_Stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPlayer.stop();

                }
            });


        }

        private void initControls() {

            try
            {
                volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
                audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                volumeSeekbar.setMax(audioManager
                        .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
                volumeSeekbar.setProgress(audioManager
                        .getStreamVolume(AudioManager.STREAM_MUSIC));


                volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
                {
                    @Override
                    public void onStopTrackingTouch(SeekBar arg0)
                    {
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar arg0)
                    {
                    }

                    @Override
                    public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
                    {
                        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                                progress, 0);
                    }
                });
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        }

        @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_main, 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"  tools:context=".MainActivity">

    <ImageView
        android:background="@mipmap/bg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="false"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true" />

    <Button
        android:background="@drawable/button_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/button_Next"

        android:layout_alignTop="@+id/button_Previous"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:background="@drawable/button_previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/button_Previous"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:background="@drawable/button_play"
        android:layout_width="wrap_content"
        android:layout_height="89dp"
        android:text=""
        android:id="@+id/button_Play"
        android:layout_above="@+id/button_Next"
        android:layout_alignRight="@+id/button_Stop"
        android:layout_alignEnd="@+id/button_Stop" />

    <Button
        android:background="@drawable/button_stop"
        android:layout_width="wrap_content"
        android:layout_height="79dp"
        android:text=""
        android:id="@+id/button_Stop"
        android:layout_above="@+id/seekBar1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="41dp" />

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar1"
        android:layout_marginBottom="51dp"
        android:thumb="@mipmap/thumb"
        android:progressDrawable="@drawable/progress"

        android:layout_above="@+id/textView"
        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="-          VOLUME          +"
        android:id="@+id/textView"
        android:textColor="#000000"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="41dp" />

    <ImageView
        android:background="@mipmap/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView4"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="47dp"
        android:layout_toStartOf="@+id/button_Next"
        android:layout_toLeftOf="@+id/button_Next" />


</RelativeLayout>



  **this is my second activity **



import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.IOException;

public class secondActivity extends AppCompatActivity {

    private Button button_Play;
    private Button button_Stop;
    private String STRING_URL = "http://5.104.112.196:8000";
    private MediaPlayer mPlayer;
    private SeekBar volumeSeekbar = null;
    private AudioManager audioManager = null;
    private ImageView imageView;

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




        button_Play = (Button)findViewById(R.id.button_Play);
        button_Stop = (Button)findViewById(R.id.button_Stop);
        Button button_next = (Button)findViewById(R.id.button_Next);
        button_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(secondActivity.this , thirdActivity.class);
                startActivity(intent);
                mPlayer.stop();





            }
        });


        Button button_previous = (Button)findViewById(R.id.button_Previous);
        button_previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(secondActivity.this, MainActivity.class);
                startActivity(intent);
                mPlayer.stop();

            }
        });

        mPlayer = new MediaPlayer();

        button_Play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    mPlayer.reset();
                    mPlayer.setDataSource(STRING_URL);
                    mPlayer.prepareAsync();
                    mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mp.start();
                        }
                    });


                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

        button_Stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.stop();

            }
        });

        imageView = (ImageView)findViewById(R.id.imageView4);
        imageView.setImageResource(R.mipmap.radio2);


    }

    private void initControls() {

        try
        {
            volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            volumeSeekbar.setMax(audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            volumeSeekbar.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC));


            volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
            {
                @Override
                public void onStopTrackingTouch(SeekBar arg0)
                {
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0)
                {
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
                {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                            progress, 0);
                }
            });
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    @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_main, 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);
    }
}`

这是secondactivity.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"  tools:context=".MainActivity">

    <ImageView
        android:background="@mipmap/bg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="false"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true" />

    <Button
        android:background="@drawable/button_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/button_Next"

        android:layout_alignTop="@+id/button_Previous"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:background="@drawable/button_previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/button_Previous"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:background="@drawable/button_play"
        android:layout_width="wrap_content"
        android:layout_height="89dp"
        android:text=""
        android:id="@+id/button_Play"
        android:layout_above="@+id/button_Next"
        android:layout_alignRight="@+id/button_Stop"
        android:layout_alignEnd="@+id/button_Stop" />

    <Button
        android:background="@drawable/button_stop"
        android:layout_width="wrap_content"
        android:layout_height="79dp"
        android:text=""
        android:id="@+id/button_Stop"
        android:layout_above="@+id/seekBar1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="41dp" />

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar1"
        android:layout_marginBottom="51dp"
        android:thumb="@mipmap/thumb"
        android:progressDrawable="@drawable/progress"

        android:layout_above="@+id/textView"
        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="-          VOLUME          +"
        android:id="@+id/textView"
        android:textColor="#000000"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="41dp" />

    <ImageView
        android:background="@mipmap/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView4"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="47dp"
        android:layout_toStartOf="@+id/button_Next"
        android:layout_toLeftOf="@+id/button_Next" />


</RelativeLayout>

这是logcat错误

10-08 19:54:56.095 4100-4127/com.speranzabd.dutchradio I/OpenGLRenderer: Initialized EGL, version 1.4
        10-08 19:54:56.122 4100-4127/com.speranzabd.dutchradio D/OpenGLRenderer: Enabling debug mode 0
        10-08 19:54:56.148 4100-4127/com.speranzabd.dutchradio W/EGL_emulation: eglSurfaceAttrib not implemented
        10-08 19:54:56.148 4100-4127/com.speranzabd.dutchradio W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb43c34c0, error=EGL_SUCCESS
        10-08 19:55:00.261 4100-4127/com.speranzabd.dutchradio W/EGL_emulation: eglSurfaceAttrib not implemented
        10-08 19:55:00.261 4100-4127/com.speranzabd.dutchradio W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb43c34e0, error=EGL_SUCCESS
        10-08 19:55:03.960 4100-4100/com.speranzabd.dutchradio E/MediaPlayer: stop called in state 1
        10-08 19:55:03.960 4100-4100/com.speranzabd.dutchradio E/MediaPlayer: error (-38, 0)
        10-08 19:55:03.970 4100-4100/com.speranzabd.dutchradio E/MediaPlayer: Error (-38,0)
        10-08 19:55:03.991 4100-4100/com.speranzabd.dutchradio D/AndroidRuntime: Shutting down VM
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime: FATAL EXCEPTION: main
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime: Process: com.speranzabd.dutchradio, PID: 4100
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.speranzabd.dutchradio/com.speranzabd.dutchradio.secondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference 10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151) 10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5257)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at com.speranzabd.dutchradio.secondActivity.onCreate(secondActivity.java:102)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5990)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)

        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5257) 
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

        10-08 19:55:03.992 4100-4100/com.speranzabd.dutchradio E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
        10-08 19:55:07.662 4100-4100/? I/Process: Sending signal. PID: 4100 SIG: 9

2 个答案:

答案 0 :(得分:0)

你有

setContentView(R.layout.activity_main);

在你们的两项活动中。我想你想要

setContentView(R.layout.secondactivity);

在你的第二次活动中。

看起来您将一项活动复制粘贴到另一项活动中,并且没有更改足够的代码以使其正常工作。

答案 1 :(得分:0)

可能的原因是, mipmap仅用于图标,因此我建议您使用drawable并将图像放入其中 绘制,MDPI 绘制,华电国际 抽拉-xhdpi

并使用  imageView.setImageResource(R.drawable.radio2);

有关确切的详细信息,您需要发布异常!!