我的应用程序崩溃,无法解释原因

时间:2014-01-09 18:15:16

标签: java android

好的,所以我正在关注mybringback的教程,我在哪里设置带位图的背景图片我得到了一个错误,所以我看了一下这里,我使用的代码看到我没有错误,所以我运行应用程序,当我选择一个崩溃的图像 这是我的活动:

package com.odysseus.thebasics;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class TutorialThree extends Activity implements OnClickListener {

    MediaPlayer buttonSound;
    ImageView display;
    int toPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wallpaper);
        final MediaPlayer buttonSound = MediaPlayer.create(TutorialThree.this, R.raw.button_sound);

        toPhone = R.drawable.image1;

        display = (ImageView) findViewById(R.id.IVdisplay);
        ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
        ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
        ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
        ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
        Button setWall = (Button) findViewById(R.id.setBackround);

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        image4.setOnClickListener(this);
        setWall.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
        case R.id.IVimage1:
            display.setImageResource(R.drawable.image5);
            toPhone = R.drawable.image1;
            break;
        case R.id.IVimage2:
            display.setImageResource(R.drawable.image2);
            toPhone = R.drawable.image2;
            break;
        case R.id.IVimage3:
            display.setImageResource(R.drawable.image3);
            toPhone = R.drawable.image3;
            break;
        case R.id.IVimage4:
            display.setImageResource(R.drawable.image4);
            toPhone = R.drawable.image4;
            break;
        case R.id.setBackround:
            InputStream decode = getResources().openRawResource(toPhone);
            Bitmap background = BitmapFactory.decodeStream(decode); //this before was InputStream ....
        WallpaperManagermyWallpaperManager=WallpaperManager.getInstance(getApplicationContext()); //error used to be here before i had getApplicationContext().setWallpaper(background);
            try{
                myWallpaperManager.setBitmap(background);
            }catch(IOException e){
                e.printStackTrace();
                Toast error = Toast.makeText(TutorialThree.this, "Oops, couldn't set image", Toast.LENGTH_LONG);
                error.show();
            }
            break;
        }
        buttonSound.start();
    }
}

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/IVdisplay"
        android:layout_width="200dp"
        android:layout_height="159dp"
        android:layout_gravity="center"
        android:src="@drawable/image5" />

    <Button
        android:id="@+id/setBackround"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Set Image To Background" />

    <HorizontalScrollView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="25dp"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/IVimage1"
                android:layout_width="125dp"
                android:layout_height="200dp"
                android:padding="15dp"
                android:src="@drawable/image5" />

            <ImageView
                android:id="@+id/IVimage2"
                android:layout_width="125dp"
                android:layout_height="200dp"
                android:padding="15dp"
                android:src="@drawable/image2" />

            <ImageView
                android:id="@+id/IVimage3"
                android:layout_width="125dp"
                android:layout_height="200dp"
                android:padding="15dp"
                android:src="@drawable/image3" />

            <ImageView
                android:id="@+id/IVimage4"
                android:layout_width="125dp"
                android:layout_height="200dp"
                android:padding="15dp"
                android:src="@drawable/image4" />
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

我的logcat:

01-09 20:03:03.960: V/MediaPlayer(29999): getDuration
01-09 20:03:03.960: V/MediaPlayer(29999): getCurrentPosition
01-09 20:03:03.970: D/MediaPlayer(29999): start() mUri is null
01-09 20:03:03.980: I/MediaPlayer(29999): mContext.toString() = com.odysseus.thebasics.menu@422c3b50
01-09 20:03:03.980: I/MediaPlayer(29999): setLGSoundNormalizerOnOff(1)
01-09 20:03:03.980: V/MediaPlayer(29999): start
01-09 20:03:04.020: V/MediaPlayer(29999): message received msg=4, ext1=0, ext2=0
01-09 20:03:04.020: V/MediaPlayer(29999): Received seek complete
01-09 20:03:04.020: V/MediaPlayer(29999): All seeks complete - return to regularly scheduled program
01-09 20:03:04.020: V/MediaPlayer(29999): callback application
01-09 20:03:04.020: V/MediaPlayer(29999): back from callback
01-09 20:03:04.040: V/MediaPlayer(29999): getDuration
01-09 20:03:04.040: V/MediaPlayer(29999): getCurrentPosition
01-09 20:03:04.040: V/MediaPlayer(29999): isPlaying: 1
01-09 20:03:04.590: V/MediaPlayer(29999): message received msg=2, ext1=0, ext2=0
01-09 20:03:04.590: V/MediaPlayer(29999): playback complete
01-09 20:03:04.590: V/MediaPlayer(29999): callback application
01-09 20:03:04.590: V/MediaPlayer(29999): back from callback
01-09 20:03:04.590: E/MediaPlayer(29999): ##### MEDIA_PLAYBACK_COMPLETE
01-09 20:03:04.590: V/MediaPlayer(29999): getDuration
01-09 20:03:04.590: V/MediaPlayer(29999): getCurrentPosition
01-09 20:03:04.590: D/MediaPlayer(29999): start() mUri is null
01-09 20:03:06.650: D/dalvikvm(29999): GC_CONCURRENT freed 76K, 5% free 6944K/7303K, paused 20ms+4ms, total 86ms
01-09 20:03:06.650: D/dalvikvm(29999): WAIT_FOR_CONCURRENT_GC blocked 45ms
01-09 20:03:06.750: D/dalvikvm(29999): GC_FOR_ALLOC freed 679K, 14% free 6646K/7687K, paused 23ms, total 23ms
01-09 20:03:06.820: D/dalvikvm(29999): GC_CONCURRENT freed 196K, 10% free 6931K/7687K, paused 21ms+2ms, total 58ms
01-09 20:03:06.820: D/dalvikvm(29999): WAIT_FOR_CONCURRENT_GC blocked 40ms
01-09 20:03:06.820: V/MediaPlayer(29999): constructor
01-09 20:03:06.820: V/MediaPlayer(29999): setListener
01-09 20:03:06.820: V/MediaPlayer(29999): setDataSource(51, 3706, 13890)
01-09 20:03:06.840: V/MediaPlayer(29999): setVideoSurfaceTexture
01-09 20:03:06.840: V/MediaPlayer(29999): prepare
01-09 20:03:06.840: V/MediaPlayer(29999): message received msg=200, ext1=1, ext2=9120
01-09 20:03:06.840: W/MediaPlayer(29999): info/warning (1, 9120)
01-09 20:03:06.840: V/MediaPlayer(29999): callback application
01-09 20:03:06.840: V/MediaPlayer(29999): back from callback
01-09 20:03:06.890: V/MediaPlayer(29999): message received msg=5, ext1=0, ext2=0
01-09 20:03:06.890: V/MediaPlayer(29999): New video size 0 x 0
01-09 20:03:06.890: V/MediaPlayer(29999): callback application
01-09 20:03:06.890: V/MediaPlayer(29999): back from callback
01-09 20:03:06.890: V/MediaPlayer(29999): message received msg=1, ext1=0, ext2=0
01-09 20:03:06.890: V/MediaPlayer(29999): prepared
01-09 20:03:06.890: V/MediaPlayer(29999): signal application thread
01-09 20:03:06.890: V/MediaPlayer(29999): prepare complete - status=0
01-09 20:03:06.900: V/MediaPlayer(29999): callback application
01-09 20:03:06.900: V/MediaPlayer(29999): back from callback
01-09 20:03:06.910: I/MediaPlayer(29999): Info (1,9120)
01-09 20:03:10.300: W/dalvikvm(29999): threadid=1: thread exiting with uncaught exception (group=0x41c2e438)
01-09 20:03:10.330: E/AndroidRuntime(29999): FATAL EXCEPTION: main
01-09 20:03:10.330: E/AndroidRuntime(29999): java.lang.NullPointerException
01-09 20:03:10.330: E/AndroidRuntime(29999):    at com.odysseus.thebasics.TutorialThree.onClick(TutorialThree.java:79)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at android.view.View.performClick(View.java:4101)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at android.view.View$PerformClick.run(View.java:17082)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at android.os.Handler.handleCallback(Handler.java:615)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at android.os.Looper.loop(Looper.java:137)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at android.app.ActivityThread.main(ActivityThread.java:4940)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at java.lang.reflect.Method.invokeNative(Native Method)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at java.lang.reflect.Method.invoke(Method.java:511)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
01-09 20:03:10.330: E/AndroidRuntime(29999):    at dalvik.system.NativeStart.main(Native Method)

你可以自己运行看看我也是android新手所以请理解!提前致谢

1 个答案:

答案 0 :(得分:1)

MediaPlayer buttonSound; // already declared  but not initialized
ImageView display;
int toPhone;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wallpaper);
    final MediaPlayer buttonSound = MediaPlayer.create(TutorialThree.this, R.raw.button_sound);
     // again declared and initialized. local to onCreate.   

更改为

buttonSound = MediaPlayer.create(TutorialThree.this, R.raw.button_sound);

buttonSound.start(); 

可能是第79行TutorialThree.java,导致NUllPointerException