0

在 Android 模拟器中启动我的应用程序时,它崩溃并出现以下错误我附上了下面的代码文件。请检查代码并建议我寻求解决方案

错误日志

03-19 17:42:07.613: W/Trace(901): Unexpected value from nativeGetEnabledTags: 0
03-19 17:42:07.623: W/Trace(901): Unexpected value from nativeGetEnabledTags: 0
03-19 17:42:08.273: D/dalvikvm(901): GC_FOR_ALLOC freed 64K, 7% free 2514K/2696K, paused 44ms, total 47ms
03-19 17:42:08.294: I/dalvikvm-heap(901): Grow heap (frag case) to 3.569MB for 1050832-byte allocation
03-19 17:42:08.354: D/dalvikvm(901): GC_FOR_ALLOC freed 1K, 5% free 3539K/3724K, paused 57ms, total 57ms
03-19 17:42:08.454: D/dalvikvm(901): GC_CONCURRENT freed <1K, 5% free 3576K/3732K, paused 10ms+22ms, total 100ms
03-19 17:42:08.614: D/AndroidRuntime(901): Shutting down VM
03-19 17:42:08.614: W/dalvikvm(901): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-19 17:42:08.624: E/AndroidRuntime(901): FATAL EXCEPTION: main
03-19 17:42:08.624: E/AndroidRuntime(901): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mypiano/com.example.mypiano.Piano}: java.lang.NullPointerException
03-19 17:42:08.624: E/AndroidRuntime(901):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-19 17:42:08.624: E/AndroidRuntime(901):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-19 17:42:08.624: E/AndroidRuntime(901):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-19 17:42:08.624: E/AndroidRuntime(901):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-19 17:42:08.624: E/AndroidRuntime(901):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 17:42:08.624: E/AndroidRuntime(901):  at android.os.Looper.loop(Looper.java:137)
03-19 17:42:08.624: E/AndroidRuntime(901):  at android.app.ActivityThread.main(ActivityThread.java:5039)
03-19 17:42:08.624: E/AndroidRuntime(901):  at java.lang.reflect.Method.invokeNative(Native Method)
03-19 17:42:08.624: E/AndroidRuntime(901):  at java.lang.reflect.Method.invoke(Method.java:511)
03-19 17:42:08.624: E/AndroidRuntime(901):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-19 17:42:08.624: E/AndroidRuntime(901):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-19 17:42:08.624: E/AndroidRuntime(901):  at dalvik.system.NativeStart.main(Native Method)
03-19 17:42:08.624: E/AndroidRuntime(901): Caused by: java.lang.NullPointerException
03-19 17:42:08.624: E/AndroidRuntime(901):  at com.example.mypiano.Piano.onCreate(Piano.java:118)
03-19 17:42:08.624: E/AndroidRuntime(901):  at android.app.Activity.performCreate(Activity.java:5104)
03-19 17:42:08.624: E/AndroidRuntime(901):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-19 17:42:08.624: E/AndroidRuntime(901):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-19 17:42:08.624: E/AndroidRuntime(901):  ... 11 more
03-19 17:42:39.023: I/Process(901): Sending signal. PID: 901 SIG: 9

钢琴.java

package com.example.mypiano;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

public class Piano extends Activity {
MediaPlayer curSnd;
Button sa,ri,ga,ma,pa,da,ni;
RadioButton msc,ani,hrn,rb;
OnClickListener radioListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_piano);
        System.out.print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxxx");
        sa = (Button)findViewById(R.id.B1);
        ri = (Button)findViewById(R.id.B2);
        ga = (Button)findViewById(R.id.B3);
        ma = (Button)findViewById(R.id.B4);
        pa = (Button)findViewById(R.id.B5);
        da = (Button)findViewById(R.id.B6);


        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);


        radioListener = new OnClickListener(){

            @Override
            public void onClick(View v) {
                System.out.println("coming here 1");
                // TODO Auto-generated method stub
                RadioButton rb= (RadioButton)v;
                curSnd = MediaPlayer.create(Piano.this,R.raw.rb);
                playSound(curSnd);
                switch(rb.getId())
                {
                case R.id.music : System.out.print("MUSIC");break;
                case R.id.animals:System.out.print("ABIMALS");break;
                case R.id.horns: System.out.print("HORN");break;
                }
            }
        };      
        msc  = (RadioButton)findViewById(R.id.music);
        msc.setOnClickListener(radioListener);

        ani  = (RadioButton)findViewById(R.id.animals);
        ani.setOnClickListener(radioListener);

        hrn  = (RadioButton)findViewById(R.id.horns);
        hrn.setOnClickListener(radioListener);


        sa.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                System.out.print("SA playing");
                // TODO Auto-generated method stub
                curSnd = MediaPlayer.create(Piano.this,R.raw.sa);
                curSnd.start();
            }
        });
        ri.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                curSnd = MediaPlayer.create(Piano.this,R.raw.ri);
                curSnd.start();
            }
        });
        ga.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                curSnd = MediaPlayer.create(Piano.this,R.raw.ga);
                curSnd.start();
            }
        });
        ma.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                curSnd = MediaPlayer.create(Piano.this,R.raw.ma);
                curSnd.start();
            }
        });
        pa.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                curSnd = MediaPlayer.create(Piano.this,R.raw.pa);
                curSnd.start();
            }
        });
        da.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                curSnd = MediaPlayer.create(Piano.this,R.raw.da);
                curSnd.start();
            }
        });
        ni.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                curSnd = MediaPlayer.create(Piano.this,R.raw.ni);
                curSnd.start();
            }
        });

    }
    private void playSound(MediaPlayer mediaPlayer) {
        mediaPlayer.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_piano, menu);
        return true;
    }

}

清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mypiano"
    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:name="com.example.mypiano.Piano"
        android:label="@string/app_name" >
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>

</manifest>

活动.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/piano"
    tools:context=".Piano" >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="128dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
        android:id="@+id/B1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1" />

        <Button
        android:id="@+id/B2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="2" />

        <Button
        android:id="@+id/B3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="128dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
        android:id="@+id/B4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="4" />

        <Button
        android:id="@+id/B5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="5" />

        <Button
        android:id="@+id/B6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="6" />

    </LinearLayout>

    </LinearLayout>

    <RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
        android:id="@+id/music"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:gravity="center_vertical|start|fill_vertical"
        android:text="MUSIC" />

    <RadioButton
    android:id="@+id/animals"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ANIMALS" />

    <RadioButton
    android:id="@+id/horns"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="HORN" />
</RadioGroup>
    </LinearLayout>
4

1 回答 1

1

你好像忘记初始化了ni

ni = (Button) findViewById (R.id.buttonName);

所以你到null了这里

ni.setOnClickListener(new View.OnClickListener() {

onClick此外,这只是一个建议,但您可以通过在 xml 中定义Buttons类似的内容来节省大量代码

<Button
...
android:onClick="functionName"/>

然后在您的代码中使用 switch 案例或类似来定义您的功能逻辑

public void functionName(View v)
{
    switch (v.getId())
    case R.id.button1
        // do something

语法可能不完全正确,但这应该给你的想法。然后你没有所有的听众,因为他们都做同样的事情,除了 set a resource

于 2013-03-19T18:29:49.577 回答