質問

私は私の問題を解決するために、インターネットとここで読んだすべてを試してみましたが、ここではStackoverflowでやってきましたが、今まで成功しませんでした。私がやろうとしているのは、基本的にボタンをクリックして、Mediaplayerがインターネットからストリームをバッファリングしている間にProgressDialogを表示することです。私はコンテキストのものを備えた多くの種類のエロを手に入れました(NullPointerException)およびスレッドの問題も。コードの詳細を次に示します。

  1. ボタンは、イベントに基づいて座っていた背景画像を備えたトグルボタンです(オン、オフ、接続されていない別の画像ボタンを表示)。
  2. 関数 prepareStream() スレッドで実行され、ストリームをロードした後に進行状況を却下する必要があるものです。 BBCラジオのHTTPストリームを呼び出しています。
  3. 問題はコンテキストに関するものだと思います...コードにいくつかのlog.dタグを入れて、問題がどこで起こっていたのかを確認し、それが続いていることがわかりました mediaPlayer.start() 方法。

彼は小さな男の子に行きます:

package com.android.iFocus;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import com.insightoverflow.iFocus.R;

public class iFocusActivity extends Activity implements OnClickListener {


    //Declare Controls
    public int count = 0;
    public int x = 1;
    public MediaPlayer mediaPlayer = null;
    ToggleButton toggleRain = null;
    Button buttonAbout = null;
    Button buttonMethod = null;
    Button buttonLink = null;
    public ProgressDialog progressDialog;
    public static final String TAG = "getFocused";



    public boolean isOnline() {
        //Check if internet is connected
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;

    }

    public void prepareStream(final Context context){
        if(isOnline()){
            // init player


            new Thread() 
            {
                public void run() 
                {

                    try {

                        sleep(1500);
                        //progressDialog.show();
                        mediaPlayer = MediaPlayer.create(context, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3"), null);
                        x=2;

                    } catch (Exception e){
                    x=3;
                }

                //dismiss the progressdialog   
                progressDialog.dismiss();
                }
            }.start();


        } else {
            x=3;
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // load layout
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        // load controls
        toggleRain = (ToggleButton)findViewById(R.id.toggleRain);
        buttonAbout = (Button)findViewById(R.id.buttonAbout);
        buttonMethod = (Button)findViewById(R.id.buttonMethod);
        buttonLink = (Button)findViewById(R.id.buttonLink);


        //Define Listeners (click event handler)
        toggleRain.setOnClickListener(this);
        buttonAbout.setOnClickListener(this);
        buttonMethod.setOnClickListener(this);
        buttonLink.setOnClickListener(this);


        // init state for player
        count = 0;

        //Context APP
        //Context appContext = this.getApplicationContext();

        if (!isOnline()){
            toggleRain.setBackgroundDrawable(getResources().getDrawable(R.drawable.notconnectedbutton));
            x=3;
        }

    }


    public void onClick(View v) {


        if( toggleRain.getId() == ((Button)v).getId() ){

            //meanwhile device is offline, do this
            do {
                toggleRain.setBackgroundDrawable(getResources().getDrawable(R.drawable.notconnectedbutton));
                try{
                      Thread.currentThread();
                    //do what you want to do before sleeping
                      Thread.sleep(1000);//sleep for 1000 ms
                      //do what you want to do after sleeptig
                } catch(Exception ie){}

                continue;
            }while (!isOnline());

            //If device is online, go for this
            if (((CompoundButton) toggleRain).isChecked()) {
                toggleRain.setBackgroundDrawable(getResources().getDrawable(R.drawable.stopbutton));
            } else {
                toggleRain.setBackgroundDrawable(getResources().getDrawable(R.drawable.playbutton));
            }

                    //----> HERE GOES WHERE I THINK IS THE PROBLEM <-----
                    //---------------------------------------------------
            if (isOnline()){
                //If music is not playing, start music
                if(count==0){

                    Log.d(TAG, "START PROGRESS DIALOG");
                    progressDialog = ProgressDialog.show(v.getContext(), "Load", "Loading");
                    //progressDialog = ProgressDialog.show(, "Load", "Loading...", true, false);
                    Log.d(TAG, "END PROGRESS DIALOG");
                    Log.d(TAG, "START PREPARE STREAM");
                    Context context = v.getContext();
                    prepareStream(context);
                    Log.d(TAG, "END PREPARE STREAM");
                    Log.d(TAG, "START MEDIA PLAYER START");

                            //LOG CAT START AND END ALL OF THE OTHER LOG TAGS, EXCEPT THIS mediaplayer.start()
                    mediaPlayer.start();
                    Log.d(TAG, "END MEDIAPLAYER START");
                    count = 1;
                } else {
                    mediaPlayer.pause();
                    count = 0;
                }
            }               

    } else if( buttonAbout.getId() == ((Button)v).getId() ){

        Intent i = new Intent(iFocusActivity.this, AboutActivity.class);
        startActivity(i);

    }

    else if ( buttonMethod.getId() == ((Button)v).getId() ){

        Intent o = new Intent(iFocusActivity.this, MethodActivity.class);
        startActivity(o);
    }

    else if ( buttonLink.getId() == ((Button)v).getId() ){

        Uri uri = Uri.parse( "http://getFocused.in" );
        startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
    }


}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }

    }


}

ログキャットは時間に教えてください mediaPlayer.start() と呼ばれます NullPointerException

役に立ちましたか?

解決

あなたはあなたを実行しなければなりません ProgressDialog.show() の中に runOnUiThread() メソッド(メインUIスレッドではありません)。 Androidドキュメントを参照してください ここ

進行]ダイアログオブジェクトを作成し、このコードを書きます。

runOnUiThread(new Runnable(){
            @Override
            public void run() {
                dialog.show();
            }
        });

また、呼び出し prepareStream()mediaplayer.start() メインのUIスレッドではなく、別のスレッドに移動する必要があります。

スレッド関連の問題を整理すると、完了する必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top