2016-08-08 15 views
0

Wenn ich versuche, meine app diese besondere Aktivität auszuführen: -Wie Sie einen Rückruf Fehler

package com.justforyou.bestnarutosongs; 

import android.content.Context; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.Handler; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.AdapterView; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.SeekBar; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class SongsListActivity extends AppCompatActivity //implements View.OnClickListener { 

public ImageButton P_and_P = (ImageButton) findViewById(R.id.P_and_P); 
public ImageButton Stop = (ImageButton) findViewById(R.id.imageButton5); 
public TextView NameD = (TextView) findViewById(R.id.textView3); 
public TextView RateD = (TextView) findViewById(R.id.textView4); 
public SeekBar seek_bar = (SeekBar) findViewById(R.id.seekBar); 
public boolean IsPaused = false; 
public int Old_Song = -326523; 
private MediaPlayer mediaplayer; 
private AudioManager mAudioManager; 
public Handler seekHandler = new Handler(); 


private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() 
{ 
    @Override 
    public void onAudioFocusChange(int focusChange){ 
     if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){ 
      mediaplayer.pause(); 
      mediaplayer.seekTo(0); 
     } 
     else if(focusChange == AudioManager.AUDIOFOCUS_GAIN){ 
      mediaplayer.start(); 
     } 
     else if(focusChange == AudioManager.AUDIOFOCUS_LOSS){ 
      if(mediaplayer != null) 
      { 
       mediaplayer.release(); 
       mediaplayer = null; 
      } 
     } 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_songs_list); 
    seekUpdation(); 

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

    final ArrayList<Songs> Song = new ArrayList<>(); 
    Song.add(new Songs("Anger", "Rate: 4.5", R.raw.anger, R.drawable.anger)); 
    Song.add(new Songs("Daylight Of Konoha", "Rate: 4.7", R.raw.d_o_k, R.drawable.d_o_k)); 
    Song.add(new Songs("Experienced Many Battles", "Rate: 4.0", R.raw.experienced_many_battles, R.drawable.e_m_b)); 
    Song.add(new Songs("Gekiha", "Rate: 4.0", R.raw.gekiha, R.drawable.gekiha)); 
    Song.add(new Songs("Genshi", "Rate: 3.0", R.raw.genshi, R.drawable.genshi)); 
    Song.add(new Songs("Girei", "Rate: 4.5", R.raw.girei, R.drawable.girei)); 
    Song.add(new Songs("Hidan", "Rate: 3.0", R.raw.hidan, R.drawable.hidan)); 
    Song.add(new Songs("HomeComing", "Rate: 3.0", R.raw.homecoming, R.drawable.homecoming)); 
    Song.add(new Songs("Naruto Main Theme", "Rate: 5.0", R.raw.naruto_main_theme, R.drawable.narutomt)); 
    Song.add(new Songs("Saika", "Rate: 4.5", R.raw.saika, R.drawable.saika)); 
    Song.add(new Songs("Senya(Itachi's Theme)", "Rate: 5.0", R.raw.senya, R.drawable.senya)); 
    Song.add(new Songs("Shirotsumekusa", "Rate: 3.0", R.raw.shirotsumekusa, R.drawable.shirot)); 
    Song.add(new Songs("Turn Over", "Rate: 4.5", R.raw.turn_over, R.drawable.turn_over)); 

    P_and_P.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) 
     { 
      if(mediaplayer != null) 
      { 
       if(!mediaplayer.isPlaying() || IsPaused) 
       { 
        P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp); 
        IsPaused = false; 
        int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); 

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { 
         mediaplayer.start(); 
         mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
          @Override 
          public void onCompletion(MediaPlayer mediaPlayer) { 
           if (mediaplayer != null) { 
            mediaplayer.release(); 
            mediaplayer = null; 
            mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener); 
           } 
          } 
         }); 
        } 
       } 
       else 
       { 
        P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);//////////////////////////////////////////////////////////////////// 
        IsPaused = true; 
        mediaplayer.pause(); 
       } 
      } 

     } 
    }); 

    Stop.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if(mediaplayer != null) 
      { 
       mediaplayer.pause(); 
       mediaplayer.seekTo(0); 
       IsPaused = true; 
      } 
     } 
    }); 



    SongsAdapter SongAdapter = new SongsAdapter(this, Song, R.color.Main_Color); 
    ListView listView = (ListView) findViewById(R.id.list_Col); 
    listView.setAdapter(SongAdapter); 




    listView.setOnItemClickListener 
      (new AdapterView.OnItemClickListener() 
      { 
       @Override 
       public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
       { 
        int Current_Song; 
        Songs song = Song.get(i); 

        //If mediaPlayer is not used before, this will make oldsong as present song. 
        if (Old_Song == -326523) 
        { 
         Old_Song = song.getSong(); 
        } 

        Current_Song = song.getSong(); 

        ImageView IVP_P = (ImageView) findViewById(R.id.P_PImage); 


        //If mediaPlayer is paused. 
        if (IsPaused) 
        { 
         //If the song paused is same as the new song. 
         if(Current_Song == Old_Song) 
         { 
          mediaplayer.start(); 
         } 

         //If the song Paused is not the new song. 
         else 
         { 
          if (mediaplayer != null) 
          { 
           mediaplayer.release(); 
           mediaplayer = null; 
          } 

          int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); 

          if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { 
           mediaplayer = mediaplayer.create(SongsListActivity.this, song.getSong()); 
           Old_Song = song.getSong(); 
           mediaplayer.start(); 
           mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
            @Override 
            public void onCompletion(MediaPlayer mediaPlayer) { 
             if (mediaplayer != null) { 
              mediaplayer.release(); 
              mediaplayer = null; 
              mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener); 
             } 
            } 
           }); 
          } 
         } 
         IsPaused = false; 

        } 

        else if (mediaplayer != null) 
        { 
         //If mediaPlayer is already Playing a song. 
         if (mediaplayer.isPlaying()) { 
          mediaplayer.pause(); 
          IVP_P.setImageResource(R.drawable.ic_play_arrow_black_24dp); 
          IsPaused = true; 
         } 
        } 

        //If mediaPlayer is used for first time and if mediaPlayer is neither paused 
        else 
        { 

         if (mediaplayer != null) 
         { 
          mediaplayer.release(); 
          mediaplayer = null; 
         } 

         int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); 

         if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { 
          mediaplayer = mediaplayer.create(SongsListActivity.this, song.getSong()); 
          Old_Song = song.getSong(); 
          mediaplayer.start(); 

          mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
           @Override 
           public void onCompletion(MediaPlayer mediaPlayer) { 
            if (mediaplayer != null) { 
             mediaplayer.release(); 
             mediaplayer = null; 
             mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener); 
            } 
           } 
          }); 
         } 
        } 

       } 
      } 
      ); 
} 

Runnable run = new Runnable(){ 
    @Override 
    public void run() { 
     seekUpdation(); 
    } 
}; 

public void seekUpdation() 
{ 
    if(mediaplayer != null) { 
     seek_bar.setProgress(mediaplayer.getCurrentPosition()); 
     seekHandler.postDelayed(run, 1000); 
    } 
    } 

@Override 
protected void onStop() { 
    super.onStop(); 
    if(mediaplayer != null) 
    { 
     mediaplayer.release(); 
     mediaplayer = null; 
    } 
} 
} 

gibt diesen Fehler: -

08-08 14:39:41.281 1730-1730/com.justforyou.bestnarutosongs E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.justforyou.bestnarutosongs, PID: 1730 
                      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.justforyou.bestnarutosongs/com.justforyou.bestnarutosongs.SongsListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2613) 
                       at android.app.ActivityThread.access$800(ActivityThread.java:181) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
                       at android.os.Handler.dispatchMessage(Handler.java:111) 
                       at android.os.Looper.loop(Looper.java:194) 
                       at android.app.ActivityThread.main(ActivityThread.java:5649) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:372) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 
                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference 
                       at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:72) 
                       at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146) 
                       at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28) 
                       at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41) 
                       at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193) 
                       at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173) 
                       at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511) 
                       at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:183) 
                       at com.justforyou.bestnarutosongs.SongsListActivity.<init>(SongsListActivity.java:23) 
                       at java.lang.reflect.Constructor.newInstance(Native Method) 
                       at java.lang.Class.newInstance(Class.java:1606) 
                       at android.app.Instrumentation.newActivity(Instrumentation.java:1071) 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2613)  
                       at android.app.ActivityThread.access$800(ActivityThread.java:181)  
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)  
                       at android.os.Handler.dispatchMessage(Handler.java:111)  
                       at android.os.Looper.loop(Looper.java:194)  
                       at android.app.ActivityThread.main(ActivityThread.java:5649)  
                       at java.lang.reflect.Method.invoke(Native Method)  
                       at java.lang.reflect.Method.invoke(Method.java:372)  
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)  
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)  

mir jemand sagen kann, was ist Fehler verursachen? Hier ist die XML-Datei: -

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_height="match_parent" 
android:layout_width="match_parent"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.58" 
    android:background="@color/Player_Color"> 

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

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="2.27"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:weightSum="1"> 

       <ImageView 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:id="@+id/imageView" /> 

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

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="0dp" 
         android:text="Please Select a Song" 
         android:textAppearance="?android:textAppearanceMedium" 
         android:textStyle="bold" 
         android:id="@+id/textView3" 
         android:layout_weight="1" 
         android:gravity="bottom" /> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="0dp" 
         android:text="" 
         android:id="@+id/textView4" 
         android:layout_weight="1" /> 
       </LinearLayout> 
      </LinearLayout> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <ImageButton 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:id="@+id/Repeat" 
       android:layout_weight="1" 
       android:background="@color/Player_Color" 
       android:src="@drawable/ic_repeat_white_48dp" /> 

      <ImageButton 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:id="@+id/Previous" 
       android:layout_weight="1" 
       android:background="@color/Player_Color" 
       android:src="@drawable/ic_skip_previous_white_48dp" /> 

      <ImageButton 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:id="@+id/P_and_P" 
       android:layout_weight="1" 
       android:src="@drawable/ic_play_arrow_white_48dp" 
       android:background="@color/Player_Color" /> 

      <ImageButton 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:id="@+id/Next" 
       android:layout_weight="1" 
       android:background="@color/Player_Color" 
       android:src="@drawable/ic_skip_next_white_48dp" /> 

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

       <ImageButton 
        android:padding="11dp" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:id="@+id/imageButton5" 
        android:background="@color/Player_Color" 
        android:src="@drawable/control_stop" 
        android:scaleType="centerInside" 
        android:layout_centerVertical="true" 
        android:layout_alignParentStart="true" /> 

      </RelativeLayout> 

     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:weightSum="1" 
       android:gravity="center_vertical"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:text="00:00" 
        android:id="@+id/textView" 
        android:gravity="center_vertical" /> 

       <SeekBar 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:id="@+id/seekBar" 
        android:layout_weight="1" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:text="00:00" 
        android:id="@+id/textView2" 
        android:gravity="center_vertical" /> 
      </LinearLayout> 
     </LinearLayout> 
    </LinearLayout> 

</RelativeLayout> 


<ListView 
    android:id="@+id/list_Col" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:layout_gravity="bottom" 
    android:drawSelectorOnTop="true" 
    android:orientation="vertical" 
    tools:context="com.justforyou.bestnarutosongs.SongsListActivity" 
    style="centreCrop" /> 

Während dieser XML-Datei zu machen, es gibt mir auch diesen Fehler: -

Error Image

Sind diese zwei Fehler bezogen oder nicht. Was könnte die beste Lösung für dieses Problem sein? Jede Hilfe wird geschätzt.

Antwort

0

sollten Sie diese Schnipsel setzen

public ImageButton P_and_P = (ImageButton) findViewById(R.id.P_and_P); 
public ImageButton Stop = (ImageButton) findViewById(R.id.imageButton5); 
public TextView NameD = (TextView) findViewById(R.id.textView3); 
public TextView RateD = (TextView) findViewById(R.id.textView4); 
public SeekBar seek_bar = (SeekBar) findViewById(R.id.seekBar); 

in Ihrem onCreate() unter: setContentView(R.layout.activity_songs_list);

+0

Obwohl dies mein Problem gelöst hat, werden Sie mir sagen, warum das half? –

+0

Weil findViewById-Methode ein View-Objekt benötigt. Die SetContentView-Methode stellt nur ein View-Objekt zur Verfügung. Wenn findViewById vor dem Aufruf von setContentView aufgerufen wird, kann die findViewById-Methode das View-Objekt nicht abrufen. –

+0

Ok! Ich habe es verstanden. Danke –

0

ich nirgendwo sehen konnte, dass mOnAudioFocusChangeListener registriert hat.

mAudioManager.OnAudioFocusChangeListener(mOnAudioFocusChangeListener);