Null pointer launch

Asked

Viewed 117 times

0

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

That 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 XML of the class:

<?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:

  • 2

    logcat error: ... Where?

  • Line 32 from where? From code or XML ? And what is this line 32 ?

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 is because seek is coming null from the findViewById(R.id.seek).

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

setContentView(R.layout.nomeDoSeuLayoutXml);

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.