Android App OnClick崩溃

时间:2014-07-31 21:14:24

标签: java android xml

所以我有一个简单的应用程序,只有一个带有几个按钮的菜单,当点击一个按钮时,您将被带到一个新页面。该页面有一个按钮,单击该按钮时,会不断更改其背景图像,直到图像用完(图像名称列表中存储的图像名称),然后您将返回主菜单。我可以做两次,然后在第三次尝试,如果我点击菜单上的按钮应用程序崩溃。这不会发生在模拟器上,只有当我在手机上运行它时。我不知道为什么会发生这种情况

enter image description here

package com.example.otapp;

import com.example.otapp.R.raw;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.media.MediaPlayer;

public class MainActivity extends ActionBarActivity {

    public static String DPExtension;//Holds the letters dp
    public String list;
    public static int Screen_Height;//holds screen height
    public static int Screen_Width;//holds screen width
    public Intent intent;
    public MediaPlayer audio;

    public final static String EXTRA_MESSAGE = "com.example.otapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //this code block is for getting the screen proportions
        Display getdisplay = getWindowManager().getDefaultDisplay();
        DisplayMetrics dispMetrics = new DisplayMetrics();
        getdisplay.getMetrics(dispMetrics);

        float densitydp  = getResources().getDisplayMetrics().density;

        float ScreenHeightdp = dispMetrics.heightPixels / densitydp;
        float ScreenWidthdp  = dispMetrics.widthPixels / densitydp;

        //Below dimension value holders do not use pixel density
        float ScreenHeightCheck = dispMetrics.heightPixels;
        float ScreenWidthCheck = dispMetrics.heightPixels;

        DPExtension = "dp";

        Screen_Height = (int) ScreenHeightCheck;

        Screen_Width = (int) ScreenWidthCheck;

        //The printlns are so I can discern the outputs in LogCat
        //System.out.println("Screen Height:" + Screen_Height);
        //System.out.println("Screen Width:" + Screen_Width);

        View Button1 = findViewById(R.id.Button01);
        LinearLayout.LayoutParams params = (LayoutParams) Button1.getLayoutParams();
        params.height = Screen_Height/3;
        Button1.setLayoutParams(params);

        View Button2 = findViewById(R.id.Button02);
        Button2.setLayoutParams(params);

        View Button3 = findViewById(R.id.Button03);
        Button3.setLayoutParams(params);

        View Button4 = findViewById(R.id.Button04);
        Button4.setLayoutParams(params);

        Button1.setOnClickListener(onClickListener);
        Button2.setOnClickListener(onClickListener);
        Button3.setOnClickListener(onClickListener);
        Button4.setOnClickListener(onClickListener);

        Button1.setBackgroundResource(getResources().getIdentifier("gettingup", "drawable", getPackageName()));

        intent = new Intent(this, DisplayMessageActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        audio = MediaPlayer.create(MainActivity.this, raw.buttonsound);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            // play sound
            audio.start();

            // do different things for each different button
            switch(v.getId()) {
                case R.id.Button01:

                    list = "Get Up";

                    intent.putExtra(EXTRA_MESSAGE, list);

                    startActivity(intent);

                    break;
                case R.id.Button02:

                    list = "Get Dressed";

                    intent.putExtra(EXTRA_MESSAGE, list);

                    startActivity(intent);

                    break;
                case R.id.Button03:

                    list = "Get Dressed";

                    intent.putExtra(EXTRA_MESSAGE, list);

                    startActivity(intent);

                    break;
                case R.id.Button04:

                    list = "Get Dressed";

                    intent.putExtra(EXTRA_MESSAGE, list);

                    startActivity(intent);

                    break;
            }   
        }
    };

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent" android:background="#1E90FF">

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

        <Button
            android:id="@+id/Button01"
            android:layout_width="match_parent"
            android:layout_height="125dp"
            android:text="@string/button_send"/>

        <Button
            android:id="@+id/Button03"
            android:layout_width="match_parent"
            android:layout_height="125dp"
            android:text="@string/button2_send" />

        <Button
            android:id="@+id/Button04"
            android:layout_width="match_parent"
            android:layout_height="125dp"
            android:text="@string/button3_send" />

        <Button
            android:id="@+id/Button02"
            android:layout_width="match_parent"
            android:layout_height="125dp"
            android:text="@string/button4_send" />

    </LinearLayout>

</ScrollView>

2 个答案:

答案 0 :(得分:0)

尝试将onClickListener对象的定义移动到onCreate方法中。 您可以将其声明为成员变量,但是您应该创建该对象并将其分配给onCreate()

中的字段

答案 1 :(得分:0)

如果上面的源格式正确,则抛出NullPointerException的第99行是:

 audio.start();

这意味着音频为空。它在第83行宣布:

 audio = MediaPlayer.create(MainActivity.this, raw.buttonsound);

最有可能发生的事情是MediaPlayer.create无法找到raw.buttonsound。我在此行调试并验证MediaPlayer无法创建音频。