繁体   English   中英

Android App OnClick崩溃

[英]Android App OnClick Crashing

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

在此处输入图片说明

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>

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

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

 audio.start();

这意味着音频为空。 在第83行声明:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM