Autore Topic: Problema nel popolare la listview  (Letto 300 volte)

Offline Eskilop

  • Nuovo arrivato
  • *
  • Post: 12
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Google Nexus 5, samsung galaxy next turbo
  • Play Store ID:
    Eskilop
  • Sistema operativo:
    Windows 8, Ubuntu 14.10
Problema nel popolare la listview
« il: 20 Ottobre 2015, 16:22:38 CEST »
0
Salve a tutti, starei cercando di creare un file manager, sembra tutto corretto in termini di sintassi, ma quando avvio l'app, questa si blocca e nel logcat non sembra dire dov'è l'errore, spero di ricevere qualche risposta, in ogni caso grazie in anticipo:

MainActivity.java

Codice (Java): [Seleziona]
package me.eskilop.filemanager;

import android.os.Bundle;
import android.os.Environment;
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.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {

    EFile efile = new EFile();
    final String [] names = null;
    static File previousFolder;
    static File [] f;
    static File currentFolder;
    static ArrayList<EFile> files = null;
    ArrayAdapter<String> aad;
    static ListView lv = null;
    static int clickno = 0;
    static String nomi [] = null;
    static String paths[] = null;

    @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();
            }
        });

        //------------------------------------------------------------------------------------------
                                    // WE ARE IN MAIN DIRECTORY

        previousFolder = Environment.getExternalStorageDirectory();

        lv = (ListView)findViewById(R.id.listView);

        final ArrayList<EFile> files = efile.searchFiles();

        paths = efile.getPaths();

        nomi = new String[paths.length];

        for (int i = 0; i < files.size(); i++)
        {
            f[i] = new File(paths[i]);
            nomi[i] = f[i].getName();
        }

        aad = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, nomi);

        lv.setAdapter(aad);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {

                currentFolder = new File(efile.getPath((int)id));

                efile.searchInSubFolder(currentFolder);

                paths = efile.getPaths();

                nomi = new String[paths.length];

                for (int i = 0; i < files.size(); i++)
                {
                    f[i] = new File(paths[i]);
                    nomi[i] = f[i].getName();
                }

                aad = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, nomi);
                lv.setAdapter(aad);
                clickno++;
            }
        });

    }

    @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);
    }

    @Override
    public void onBackPressed()
    {
        if (clickno == 1)
        {
            previousFolder = Environment.getExternalStorageDirectory();
        }
        else
        {

            files = efile.searchInSubFolder(previousFolder);

            paths = efile.getPaths();

            nomi = new String[paths.length];

            for (int i = 0; i < paths.length; i++)
            {
                f[i] = new File(paths[i]);
                nomi[i] = f[i].getName();
            }

            aad = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, nomi);

            lv.setAdapter(aad);

            clickno = 0;

        }

        paths = efile.getPaths();

        nomi = new String[paths.length];

        for (int i = 0; i < files.size(); i++)
        {
            f[i] = new File(paths[i]);
            nomi[i] = f[i].getName();
        }

        aad = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, nomi);

        lv.setAdapter(aad);

    }
}

EFile.java

Codice (Java): [Seleziona]
package me.eskilop.filemanager;

import android.os.Environment;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by eskilop on 20/10/15.
 */

public class EFile
{
    private static String fileName;
    private static String extension;
    private static int TYPE;
    private static int TYPE_IMAGE = 1;
    private static int TYPE_COMPRESSED = 2;
    private static int TYPE_TEXT = 3;
    private static int TYPE_VIDEO = 4;
    private static int TYPE_DIRECTORY = 5;
    private static int TYPE_AUDIO = 6;
    private static File [] directories;
    private static String [] paths;

    public EFile()
    {

    }

    public EFile(File f)
    {

    }


    public EFile(int type, String name)
    {
        fileName = name;
        TYPE = type;
    }

    private int getFileType(File f)
    {
        String s = f.getName();

        return TYPE;
    }

    private String getExtension(File f)
    {
        String extension = "";
        int countern = 0;
        for (int i = 0; i < f.getName().length(); i++)
        {
            if (f.getName().charAt(i) == '.')
            {
                countern = i;
                extension = f.getName().substring(countern, f.getName().length());
            }
        }
        return extension;
    }

    public ArrayList<EFile> searchFiles()
    {
        ArrayList<EFile> afl = new ArrayList<EFile>();

        File f = Environment.getExternalStorageDirectory();
        File [] files = f.listFiles();
        directories = new File[files.length];
        paths = new String[files.length];
        int i = 0;
        int si = 0;

        for (File singleFile : files)
        {
            paths[si] = singleFile.getPath();
            if (singleFile.isDirectory())
            {
                directories[i] = singleFile;
                i++;
            }
            si ++;
        }

        return afl;

    }

    public ArrayList<EFile> searchInSubFolder(File folder)
    {
        ArrayList<EFile> sf = new ArrayList<EFile>();
        File [] f = folder.listFiles();
        directories = new File[f.length];
        paths = new String[f.length];
        int i = 0;
        int si = 0;

        for (File singleFile : f)
        {
            paths[si] = singleFile.getPath();
            if (singleFile.isDirectory())
            {
                directories[i] = singleFile;
                i++;
            }
            si++;
        }

        return sf;

    }

    public File [] getDirectories()
    {
        return directories;
    }

    public String [] getPaths()
    {
        return paths;
    }

    public String getPath(int i)
    {
        return paths[i];
    }
}

LOGCAT:


10-20 16:08:31.571 30901-30901/me.eskilop.filemanager I/art: Late-enabling -Xcheck:jni
10-20 16:08:31.625 30901-30901/me.eskilop.filemanager W/System: ClassLoader referenced unknown path: /data/app/me.eskilop.filemanager-1/lib/arm
10-20 16:08:31.766 30901-30947/me.eskilop.filemanager D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-20 16:08:31.794 30901-30901/me.eskilop.filemanager D/AndroidRuntime: Shutting down VM
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime: FATAL EXCEPTION: main
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime: Process: me.eskilop.filemanager, PID: 30901
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.AbsListView.obtainView(AbsListView.java:2346)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.ListView.onMeasure(ListView.java:1188)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.View.measure(View.java:18788)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.View.measure(View.java:18788)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.View.measure(View.java:18788)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.View.measure(View.java:18788)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.View.measure(View.java:18788)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.View.measure(View.java:18788)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.View.measure(View.java:18788)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.View.measure(View.java:18788)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:670)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:606)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-20 16:08:31.795 30901-30901/me.eskilop.filemanager E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-20 16:08:44.500 30901-30907/me.eskilop.filemanager I/art: Debugger is no longer active
10-20 16:13:31.870 30901-30901/me.eskilop.filemanager I/Process: Sending signal. PID: 30901 SIG: 9

P.S.:

Sono con android M AOSP, ovviamente gli ho già settato i permessi di leggere l'archiviazione e nel mio manifest ho messo solo quello

Offline NetGEL

  • Utente normale
  • ***
  • Post: 211
  • Respect: +9
    • Google+
    • Mostra profilo
  • Play Store ID:
    NetGEL
Re:Problema nel popolare la listview
« Risposta #1 il: 20 Ottobre 2015, 18:07:38 CEST »
0
Ciao,
sicuramente quello che vedo di sbagliato in prima battuta è che instanzi l'adapter 2 volte:

aad = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, nomi);
lv.setAdapter(aad);

questo devi farlo una volta sola nella onCreate e poi devi aggiornare l'adapter e richiamare il metodo aad.notifyDataSetChanged(); dove ti serve.

Questo al di là dell'errore che ti dà. Prova inizialmente a mettere a posto quello che ti ho scritto sopra.