我的应用程序每次都被关闭的原因是什么?

时间:2015-09-24 04:04:50

标签: android xml

我正在尝试创建一个音乐应用程序,并且只是制作了我的歌曲列表列表及其关闭力量。我已经提到了阅读外部存储的用户权限,然后......这里是代码

void pop()

我使用的xml文件也在下面给出

activity_file_list.xml

package com.smp.ravirathore.music;
import android.graphics.Typeface;
import android.os.Environment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;


public class FileList extends AppCompatActivity {



TextView text_list;
ImageView backimg;
ListView listsongs;
String[] genuinesongs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_file_list);
    backimg = (ImageView) findViewById(R.id.imageView);
    text_list = (TextView) findViewById(R.id.text_list);
    listsongs = (ListView) findViewById(R.id.list_songs);
    Typeface font = Typeface.createFromAsset(getAssets(), "journal.ttf");
    text_list.setTypeface(font);
    ArrayList<File> songs = find(Environment.getExternalStorageDirectory());
    genuinesongs = new String[songs.size()];
    for (int i = 0; i <= songs.size(); i++) {

        genuinesongs[i] = songs.get(i).getName().toString().replace("mp3","");
    }
   ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(),R.layout.activity_file_list,
   R.id.text_list, genuinesongs);
   listsongs.setAdapter(adp);
}

public ArrayList<File> find(File root){
    ArrayList<File> arrayList = new ArrayList<File>();
    File[] files = root.listFiles();
    for(File single : files){
        if(single.isDirectory() && !single.isHidden()){
            arrayList.addAll(find(single));
        }
        else{
            if(single.getName().endsWith(".mp3") || single.getName().endsWith(".wav")){
            arrayList.add(single);
        }
    }


}
    return arrayList;
}


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

word_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FileList">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/list_songs"
         />

</RelativeLayout>

最后是我的AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearlayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#bdbdbd"
        android:text="Hello"
        android:id="@+id/text_list"
        android:padding="10dp" />
</LinearLayout>

这是logcat:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.smp.ravirathore.music" >
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".FileList">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在您的活动中删除文字查看参考以及与textView相关的所有内容并更新您的text_list

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_list);
        backimg = (ImageView) findViewById(R.id.imageView);
        listsongs = (ListView) findViewById(R.id.list_songs);
        ArrayList<File> songs = find(Environment.getExternalStorageDirectory());
        genuinesongs = new String[songs.size()];
        for (int i = 0; i <= songs.size(); i++) {

            genuinesongs[i] = songs.get(i).getName().toString().replace("mp3","");
        }
      **UPDATED Adapter**
       ListAdapter  adp = new ListAdapter(getApplicationContext(), genuinesongs);
       listsongs.setAdapter(adp);
    }

<强>已更新 textlistrow.xml

  <?xml version="1.0" encoding="utf-8"?>
     <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#bdbdbd"
            android:text="Hello"
            android:id="@+id/text_item"
            android:padding="10dp" />

添加自定义字体 如果需要向textview添加字体,则需要创建custom_item_layout并将其与自定义适配器一起使用并在getView()方法中设置字体。 像

public class ListAdapter extends ArrayAdapter<String> {
 String[] items;
    public ListAdapter(Context context,String[] items) {
        super(context,R.layout.itemlistrow,items);
        this.items=items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.itemlistrow, null); //inflate the list_row
         }


            TextView tv= (TextView) v.findViewById(R.id.textitem);
             Typeface font = Typeface.createFromAsset(getAssets(), "journal.ttf");
tv.setTypeface(font);
             tv.setText(items[position]);

        }

        return v;
    }

}