SqliteException error on Android [closed]

-2

I'm having problems starting my application, it shows me a SqliteException error, I add my Logcat message as well as my MainActivity class, thanks for your help

This is my MainActivity class

package com.example.arielra.reproductordeaudio;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private int count,i,TRACK_Column,ID_Column,DATA_Column,YEAR_Column;
    private int DURATION_Column,ALBUM_ID_Column,ALBUM_Column,ARTIST_Column;
    private int[] idMusic;
    TextView title,artist, time;
    private String[] audioLista,artistLista,arrPath,musicTime,artistaAlbumLista;
    ListView lista;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        lista = (ListView) findViewById(R.id.listView_Lista);

        audioCursor();

        AudioAdapter audioAdapter = new AudioAdapter();

        lista.setAdapter(audioAdapter);
    }
    private void audioCursor(){
        String[] information ={
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.TRACK,
                MediaStore.Audio.Media.YEAR,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.ALBUM_ID,
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.ALBUM_KEY,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.TITLE_KEY,
                MediaStore.Audio.Media.ARTIST_ID,
                MediaStore.Audio.Media.ARTIST
        };
        final String orderBy = MediaStore.Audio.Media._ID;
        Cursor audioCursor = getContentResolver().query(
                //EXTERNAL_CONTENT_URI para el volumen de almacenamiento
                // "primaria" externo.
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, information, null,
                null, orderBy);
        count = audioCursor.getCount();
        audioLista = new String[count];
        artistLista = new String[count];
        arrPath = new String[count];
        musicTime = new String[count];
        artistaAlbumLista = new String[count];

        ID_Column = audioCursor.getColumnIndex(MediaStore.Audio.Media._ID);
        DATA_Column = audioCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
        YEAR_Column = audioCursor.getColumnIndex(MediaStore.Audio.Media.YEAR);
        DURATION_Column = audioCursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
        ALBUM_ID_Column = audioCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
        ALBUM_Column = audioCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
        TRACK_Column = audioCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
        ARTIST_Column = audioCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);

        while (audioCursor.moveToNext()){
            audioLista[i] = audioCursor.getString(TRACK_Column);
            artistLista[i] = audioCursor.getString(ARTIST_Column);
            arrPath[i]=audioCursor.getString(DATA_Column);
            musicTime[i] = audioCursor.getString(DURATION_Column);
            artistaAlbumLista[i] = audioCursor.getString(ALBUM_ID_Column);

            i++;
        }
        audioCursor.close();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public class AudioAdapter extends BaseAdapter{
        private LayoutInflater inflater;
        public AudioAdapter(){
            inflater = (LayoutInflater) getSystemService(
                    //Utilizar con getSystemService (String) para recuperar
                    // un LayoutInflater para inflar los recursos de diseño en este contexto.
                    Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return count;
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = inflater.inflate(R.layout.rows, null);
            title = (TextView) view.findViewById(R.id.textView_Title);
            artist = (TextView) view.findViewById(R.id.textView_Artist);
            time = (TextView) view.findViewById(R.id.textView_Time);
            title.setId(i);
            artist.setId(i);

            title.setText(audioLista[i]);
            artist.setText(artistLista[i]);
            long tmp = Integer.parseInt(musicTime[i]);
            time.setText(convertDuration(tmp));

            lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    String url = arrPath[i];
                    String artistAlbum = artistaAlbumLista[i];
                    String urlAlbum = urlAlbumArt(artistAlbum);

                    Intent intent = new Intent(MainActivity.this, Reproductive.class);
                    intent.putExtra("Url", url);
                    intent.putExtra("urlAlbum", urlAlbum);
                    startActivity(intent);
                }
            });
            return view;
        }
        public String convertDuration(long duration){
            String out = null;
            long hours=0;

            try{
                hours = (duration/3600000);
            }catch (Exception e){
                e.printStackTrace();
                return out;
            }
            long remaining_minutes = (duration -(hours*3600000))/60000;
            String minutes= String.valueOf(remaining_minutes);
            if (minutes.equals(0)){
                minutes = "00";
            }
            long remaining_seconds = (duration -(hours*3600000)-(remaining_minutes*60000));
            String seconds= String.valueOf(remaining_seconds);
            if (seconds.length() < 2){
                seconds = "00";
            }else{
                seconds = seconds.substring(0, 2);
            }
            if(hours > 0){
                out = hours + ":" + minutes + ":" + seconds;
            }else{
                out = minutes + ":" + seconds;
            }
            return out;
        }
        private String urlAlbumArt(String artistAlbum){
            String[] projection=new String[]{MediaStore.Audio.Albums.ALBUM_ART};
            String selection= MediaStore.Audio.Albums._ID + "?";
            String[] selectionArgs=new String[]{artistAlbum};
            Cursor cursor= getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);
            String urlAlbum = "";
            if (cursor != null){
                if(cursor.moveToFirst()){
                    urlAlbum = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART));
                }
                cursor.close();
            }
            return urlAlbum;
        }
    }
}

New error

06-10 00:23:42.057 2316-2316/com.example.arielra.reproductordeaudio E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.arielra.reproductordeaudio, PID: 2316
                                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.arielra.reproductordeaudio/com.example.arielra.reproductordeaudio.Reproductive}: java.lang.NullPointerException
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:136)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                                       Caused by: java.lang.NullPointerException
                                                                                          at com.example.arielra.reproductordeaudio.Reproductive.imgUrlAlbum(Reproductive.java:49)
                                                                                          at com.example.arielra.reproductordeaudio.Reproductive.onCreate(Reproductive.java:44)
                                                                                          at android.app.Activity.performCreate(Activity.java:5231)
                                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                          at android.os.Looper.loop(Looper.java:136) 
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                          at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                          at dalvik.system.NativeStart.main(Native Method)

Reproductive.java

package com.example.arielra.reproductordeaudio;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;

import java.io.File;
import java.io.IOException;

/**
 * Created by ArielRA on 29/05/2016.
 */
public class Reproductive extends AppCompatActivity implements MediaController.MediaPlayerControl{
    MediaPlayer mediaPlayer = new MediaPlayer();
    String FilePath, urlAlbum;
    MediaController mediaController;
    Handler handler;
    ImageView imgAlbum;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reproductive);

        //Button stop = (Button) findViewById(R.id.button);
        imgAlbum = (ImageView)findViewById(R.id.imageView);
        Bundle bundle = this.getIntent().getExtras();
        FilePath = bundle.getString("Url");

        /*stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.stop();
            }
        });*/
        urlAlbum = bundle.getString("urlAlbum");
        imgUrlAlbum(urlAlbum);
        playAudio();
    }
    private void imgUrlAlbum(String imgUrlAlbum){
        if(imgUrlAlbum == null){
            imgAlbum.setImageResource(R.mipmap.cd);
        }else{
            File imgFile = new File(imgUrlAlbum);
            if(imgFile.exists()){
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

                imgAlbum.setImageBitmap(myBitmap);
            }
        }
    }
    private void playAudio(){
        mediaController = new MediaController(this);
        mediaController.setMediaPlayer(Reproductive.this);
        mediaController.setAnchorView(findViewById(R.id.audioView));
        handler = new Handler();
        try{
            mediaPlayer.setDataSource(FilePath);

            mediaPlayer.prepare();
            mediaPlayer.start();

        } catch (IOException e){
            e.printStackTrace();
        }
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer Player) {
                handler.post(new Runnable() {
                    public void run() {
                        mediaController.setEnabled(true);
                        mediaController.show();
                        mediaPlayer.start();
                    }
                });
            }
        });
    }
    @Override
    public boolean onTouchEvent(MotionEvent event){
        mediaController.show();
        return true;
    }

    @Override
    public void start() {
        mediaPlayer.start();
    }

    @Override
    public void pause() {
        mediaPlayer.pause();
    }

    @Override
    public int getDuration() {
        return mediaPlayer.getDuration();
    }

    @Override
    public int getCurrentPosition() {
        return mediaPlayer.getCurrentPosition();
    }

    @Override
    public void seekTo(int i) {
        mediaPlayer.seekTo(i);
    }

    @Override
    public boolean isPlaying() {
        return mediaPlayer.isPlaying();
    }

    @Override
    public int getBufferPercentage() {
        int porcentage = (mediaPlayer.getCurrentPosition() * 100)/ mediaPlayer.getDuration();

        return porcentage;
    }

    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }
}

layout

activity_main.xml

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

content_main.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Lista de Audio"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView_Lista" />
</LinearLayout>

reproductive.xml

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@mipmap/cd"
    />

rows.xml

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="60dp"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="61dp"
            android:id="@+id/imageView"
            android:src="@mipmap/auriculares" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="237dp"
        android:layout_height="62dp"
        android:layout_weight="1.02">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView_Artist"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Large Text"
            android:id="@+id/textView_Title"
            android:layout_below="@+id/textView_Artist"
            android:layout_alignParentStart="true" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="73dp"
        android:layout_height="63dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView_Time" />
    </LinearLayout>
</LinearLayout>

    
asked by ArielRA 10.06.2016 в 05:55
source

1 answer

0

Your query has an error that generates SQLiteException , within the method urlAlbumArt()

 String selection = MediaStore.Audio.Albums._ID + "?";

must be

String selection = MediaStore.Audio.Albums._ID + "=?";

The second problem is

  

Caused by: java.lang.NullPointerException
  at   com.example.arielra.reproductordeaudio.Reproductive.imgUrlAlbum (Reproductive.java:49)

It would be in this line

imgAlbum.setImageResource(R.mipmap.cd); 

and it is caused because the element with id @+id/imageView is not found, inside the layout reproductive.xml , you only have @+id/imageView2 , modify it.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@mipmap/cd"
    />

Do not forget to add the permissions in your AndroidManifest.xml

<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
answered by 10.06.2016 / 06:11
source