簡體   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