6

我收到了一个奇怪的错误列表,其中一些看似断开连接,但我无法判断,因为我什至无法让我的应用程序加载以进行调试。这是一个非常简单的应用程序,一个单选按钮组中有 3 个单选按钮,每个单选按钮都会触发不同的歌曲播放。

我将首先发布我的清单、我的活动文件和布局,然后发布错误日志。

显现:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cis298.lab2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity 
            android:label = "@string/app_name"
            android:name = ".LayoutActivity">
            <intent-filter >
                <action android:name = "android.intent.action.MAIN" />

                <category android:name = "android.intent.category.LAUNCHER" />

            </intent-filter>
            ></activity>
    </application>

</manifest>

布局活动:

package com.cis298.lab2;

import java.io.IOException;

import com.cis298.lab2.R;

import android.app.Activity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.widget.RadioGroup;

public class LayoutActivity extends Activity {

    int song;
    MediaPlayer mediaplayer = MediaPlayer.create(this, song);

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        try {
            mediaplayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        RadioGroup radgrp = (RadioGroup) findViewById(R.id.radiogroup);
        radgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                case R.id.dance:
                    song = R.raw.redalert;
                    mediaplayer.start();
                    break;
                case R.id.rap:
                    song = R.raw.cannedheat;
                    break;
                case R.id.rock:
                    song = R.raw.movmou8105;
                    break;}
            }
        });
    }
}

布局:

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

   <RadioGroup
       android:id="@+id/radiogroup"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:orientation="vertical" >

       <RadioButton
           android:id="@+id/rock"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Play Rock" />

       <RadioButton
           android:id="@+id/rap"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Play Rap" 
           android:checked="true"/>

       <RadioButton
           android:id="@+id/dance"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="Play Dance"
           android:textAlignment="center" />

   </RadioGroup>

</RelativeLayout>

日志:

02-21 20:34:05.044: D/AndroidRuntime(322): 关闭 VM 02-21 20:34:05.044: W/dalvikvm(322): threadid=1: 线程退出但未捕获异常 (group=0x4001d800) 02 -21 20:34:05.094:E/AndroidRuntime(322):致命异常:主要 02-21 20:34:05.094:E/AndroidRuntime(322):java.lang.RuntimeException:无法实例化活动 ComponentInfo{com.cis298 .lab2/com.cis298.lab2.LayoutActivity}:java.lang.NullPointerException 02-21 20:34:05.094:E/AndroidRuntime(322):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 02- 21 20:34:05.094: E/AndroidRuntime(322): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 02-21 20:34:05.094: E/AndroidRuntime(322): 在 android.app。 ActivityThread.access$2300(ActivityThread.java:125) 02-21 20:34:05.094: E/AndroidRuntime(322):在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 02-21 20:34:05.094: E/AndroidRuntime(322): 在 android.os.Handler.dispatchMessage(Handler.java:99) 02- 21 20:34:05.094: E/AndroidRuntime(322): 在 android.os.Looper.loop(Looper.java:123) 02-21 20:34:05.094: E/AndroidRuntime(322): 在 android.app。 ActivityThread.main(ActivityThread.java:4627) 02-21 20:34:05.094: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method) 02-21 20:34:05.094: E /AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:521) 02-21 20:34:05.094: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$ MethodAndArgsCaller.run(ZygoteInit.java:868) 02-21 20:34:05.094: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-21 20: 34:05.094:E/AndroidRuntime(322): 在 dalvik.system.NativeStart.main(Native Method) 02-21 20:34:05.094: E/AndroidRuntime(322): Caused by: java.lang.NullPointerException 02-21 20:34: 05.094: E/AndroidRuntime(322): 在 android.content.ContextWrapper.getResources(ContextWrapper.java:80) 02-21 20:34:05.094: E/AndroidRuntime(322): 在 android.media.MediaPlayer.create(MediaPlayer .java:641) 02-21 20:34:05.094: E/AndroidRuntime(322): at com.cis298.lab2.LayoutActivity.(LayoutActivity.java:15) 02-21 20:34:05.094: E/AndroidRuntime( 322): 在 java.lang.Class.newInstanceImpl(Native Method) 02-21 20:34:05.094: E/AndroidRuntime(322): 在 java.lang.Class.newInstance(Class.java:1429) 02-21 20 :34:05.094: E/AndroidRuntime(322): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 02-21 20:34:05.094: E/AndroidRuntime(322):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 02-21 20:34:05.094: E/AndroidRuntime(322): ... 11 更多

任何帮助将不胜感激。

4

1 回答 1

3

尝试移动 mediaplayer = MediaPlayer.create(this, song);到里面onCreate()

由于 Activity 直到 才完全初始化onCreate(),这可能是 NPE 的原因。

但是,下一个问题是您创建 MediaPlayer 时最初是歌曲当它是实例变量时,0int 具有默认值0因此,如果移动MediaPlayer.create解决了这个NPE,下一个错误将是无法找到资源。因此, callMediaPlayer.createoncesong会保留一些有用的东西。

也许这样的事情会奏效。

public class LayoutActivity extends Activity {

  int song;
  MediaPlayer mediaPlayer;

  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);

    RadioGroup radgrp = (RadioGroup) findViewById(R.id.radiogroup);
    radgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
          case R.id.dance:
            song = R.raw.redalert;
            break;
          case R.id.rap:
            song = R.raw.cannedheat;
            break;
          case R.id.rock:
            song = R.raw.movmou8105;
            break;
        }


        mediaplayer = MediaPlayer.create(LayoutActivity.this, song);
        try {
          mediaplayer.prepare();
          mediaplayer.start();
        } catch (IllegalStateException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    });
  }
}

如果歌曲没有在其他任何地方使用,您可以将其保留在onCheckedChanged().

于 2013-02-22T03:43:31.187 回答