Null Pointer Launch

0

I am using a code in my application that was working normally, but suddenly when I was testing with the emulator it gets null pointer error on line 32, and also the method definirVolume() called on onCreate() I checked and everything is as it was, that it was to run correctly.

This is the code:

package com.rs.player;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

public class Listen extends Activity{

      private AudioManager audioManager;
      private StreamMedia audioStreamer;
      private boolean isPlaying;
      private ImageButton playButton;
      private String urlStreaming;

      private void definirVolume()
      {
        this.audioManager = ((AudioManager)getSystemService("audio"));
        int i = this.audioManager.getStreamMaxVolume(3);
        int j = this.audioManager.getStreamVolume(3);
        setVolumeControlStream(3);
        SeekBar seek = (SeekBar)findViewById(R.id.seek);
        seek.setMax(i);
        seek.setProgress(j);
        seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
          public void onProgressChanged(SeekBar paramAnonymousSeekBar, int paramAnonymousInt, boolean paramAnonymousBoolean)
          {
            audioManager.setStreamVolume(3, paramAnonymousInt, 0);
          }

          public void onStartTrackingTouch(SeekBar paramAnonymousSeekBar)
          {
          }

          public void onStopTrackingTouch(SeekBar paramAnonymousSeekBar)
          {
          }
        });
      }


      @SuppressLint({"NewApi"})
      private void notification()
      {
        NotificationManager localNotificationManager = (NotificationManager)getSystemService("notification");
        PendingIntent localPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, Listen.class), 0);
        localNotificationManager.notify(1, new Notification.Builder(this).setSmallIcon(2130837515).setContentTitle(" Rádio Sant'Ana").setContentText(" Essa rádio caiu do céu!").setOngoing(true).setContentIntent(localPendingIntent).build());
      }

      private void sair()
      {
        if (this.audioStreamer != null)
          this.audioStreamer.interrupt();
        ((NotificationManager)getSystemService("notification")).cancel(1);
        onDestroy();
      }

      private void startStreamingAudio()
      {
        try
        {
          if (this.audioStreamer != null)
            this.audioStreamer.interrupt();
          this.audioStreamer = new StreamMedia(this, playButton);
          this.audioStreamer.startStreaming(this.urlStreaming, 5208L, 216L);
          return;
        }
        catch (IOException localIOException)
        {
          while (true)
            Log.e(getClass().getName(), "Error starting to stream audio.", localIOException);
        }
      }

      public void onCreate(Bundle b)
      {
            super.onCreate(b);
            isPlaying = false;
            definirVolume();
            notification();
            sair();
            this.urlStreaming = "http://sh.upx.com.br:10369";
            this.playButton = ((ImageButton)findViewById(R.id.buttonPlay));
            this.playButton.setOnClickListener(new View.OnClickListener()
            {
              public void onClick(View paramAnonymousView)
              {
                if (isPlaying)
                {
                  audioStreamer.interrupt();
                  playButton.setImageResource(R.drawable.bt_play);
                  ((NotificationManager)Listen.this.getSystemService("notification")).cancel(1);
                }
                if (!isPlaying)
                {
                  startStreamingAudio();
                  playButton.setImageResource(R.drawable.bt_pause);
                  notification();
                }
             isPlaying=!isPlaying;
              }
            });
      }

      protected void onDestroy()
      {
        super.onDestroy();
        if (this.audioStreamer != null)
          this.audioStreamer.interrupt();
      }

}

The class XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_listen"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/seek"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:progressDrawable="@drawable/progress"
        android:thumb="@drawable/scrubber_control_pressed_holo" />

    <ImageButton
        android:id="@+id/buttonPlay"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/seek"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="@null"
        android:contentDescription="@null"
        android:duplicateParentState="true"
        android:paddingBottom="15.0dip"
        android:src="@drawable/bt_play" />

</RelativeLayout>

Logcat error:

    
asked by anonymous 19.12.2014 / 01:10

1 answer

1

This is the section that fails (line 32 is seek.setMax(i); ):

SeekBar seek = (SeekBar)findViewById(R.id.seek);
seek.setMax(i);

This happens because seek is coming null from findViewById(R.id.seek) .

And the reason is because you did not assign the layout to your Activity . In other words, in your onCreate() method the following line is missing:

setContentView(R.layout.nomeDoSeuLayoutXml);

Replace nomeDoSeuLayoutXml with the name of the XML file that is in the /res/layout folder and contains the layout you posted in the question (without the .xml extension).

    
19.12.2014 / 14:50