我的应用程序在API 23中无法运行

3

这个音乐应用程序使用了<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />,它可以在API 16上运行,但在API 23上无法工作。

当在API23上检查时,列表视图没有加载歌曲,显示为空白,在API16上正常工作。

在API22上也可以正常工作

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

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class PlayListActivity extends ListActivity {
    // Songs list
    public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    SearchView sv;
    ListView lv;
    SimpleAdapter adapter;
    ArrayList<HashMap<String, String>> songsListData = new ArrayList<>();
    int songIndex;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playlist);

        sv = (SearchView)findViewById(R.id.searchView);
        int id = sv.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) sv.findViewById(id);
        textView.setTextColor(Color.WHITE);
        textView.setHintTextColor(Color.LTGRAY);

        SongsManager plm = new SongsManager();
        // get all songs from sdcard
        this.songsList = plm.getPlayList(Environment.getExternalStorageDirectory());

        if(songsList!=null && songsList.size()>0) {
            // looping through playlist
            for (int i = 0; i < songsList.size(); i++) {
                // creating new HashMap
                HashMap<String, String> song = songsList.get(i);

                // adding HashList to ArrayList
                songsListData.add(song);
            }

            // Adding menuItems to ListView
             adapter = new SimpleAdapter(this, songsListData,
                    R.layout.playlist_item, new String[]{"songTitle"}, new int[]{
                    R.id.songTitle});


            setListAdapter(adapter);

            sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    adapter.getFilter().filter(newText);
                    return false;
                }
            });
            // selecting single ListView item
             lv = getListView();

            // register for context menu
            registerForContextMenu(lv);

            // listening to single listitem click
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    // getting listitem index
                    songIndex = position;

                    // Starting new intent
                    Intent in = new Intent(getApplicationContext(),
                            MainActivity.class);
                    // Sending songIndex to PlayerActivity
                    in.putExtra("songIndex", songIndex);
                    setResult(100, in);
                    // Closing PlayListView
                    finish();
                }
            });
        }

        sv.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(final View view, boolean hasFocus) {
                if (hasFocus) {
                    view.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.showSoftInput(view.findFocus(), 0);
                        }
                    }, 200);
                }
            }
        });
    }
    //ListView listView = (ListView)findViewById(android.R.id.list);
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.playlist_contextmenu,menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        switch (item.getItemId()){

            case R.id.delete_id:
                songsListData.remove(info.position);
                String path =  songsList.get(info.position).get("songPath");
                File filepath = new File(path);
                filepath.delete();
                adapter.notifyDataSetChanged();
                return true;
            default:
                return super.onContextItemSelected(item);
        }

    }
}

即使通知图标不显示,它也会显示为空白图标 我使用了NotificationCompat.Builder

列表视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/searchView"
        android:background="#242424" //black colour
        android:queryHint="Search...">
    </SearchView>
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="@drawable/gradient_horizontal_line"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" // balck colour
        android:background="@drawable/list_selector"/>

</LinearLayout>

列表项

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/list_selector"
    android:foreground="#EBEBEB"
    android:padding="5dp">
    <!--android:background="@drawable/list_selector"-->
    <TextView 
        android:id="@+id/songTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:padding="10dp"
        android:background="@drawable/list_selector"
        android:textColor="#FFFFFF"/>  // white colour
    <!--android:color="#f3f3f3"-->
</LinearLayout>

列表选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->

    <item 
     android:state_selected="false"
        android:state_pressed="false" 
        android:drawable="@drawable/gradient_bg" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/gradient_bg_hover" />
    <item android:state_selected="true"
     android:state_pressed="false" 
        android:drawable="@drawable/gradient_bg_hover" />
    </selector>

Styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="btnStyleBlackpearl" parent="@android:style/Widget.Button">
        <item name="android:textSize">15sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#C4C4C4</item>
        <item name="android:gravity">center</item>
        <item name="android:shadowColor">#000000</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">1</item>
        <item name="android:shadowRadius">0.6</item>
        <item name="android:background">@drawable/custom_btn_black_pearl</item>
        <item name="android:padding">10dip</item>
    </style>

</resources>

请指导...

API 16 - 工作正常

API 23 - 不工作 - 在模拟器中打开存储权限后 输入图像描述


如果您的目标API >= 21,则通知将仅具有白色。每个不透明像素都将是白色。制作一个使用alpha通道以与对比度配合的图标。 - Eugen Pechanec
@ Eugen Pechanec,即使使用了 alpha 通道,通知图标仍然只显示为白色。 - Selva
这正是我在第一句话中所说的。自Lollipop以来,这是预期行为。 - Eugen Pechanec
5个回答

1

快速解决方法:在设备/模拟器上转到 设置 -> 应用 -> 您的应用程序 -> 权限,确保选中了存储。长期来看,您需要使用API 23中添加的新API 在运行时请求权限


我已经在模拟器中打开了存储权限,但在显示列表视图方面没有太大的改进。之前是完全黑屏,现在在打开存储权限后,列表视图显示为白色屏幕,水平列表项为空文本,但如果我点击任何项,歌曲就会播放。而且通知图标仍然没有显示出来。请参见上面的屏幕截图。 - Selva
请检查您的文字颜色和样式。如果两张屏幕截图之间颜色相同,那么您只是在白色/浅色背景上使用白色文本。 - Alex Townsend
你能否发布一下你的列表项布局以及styles.xml文件吗? - Alex Townsend
你的@drawable/list_selector代码是什么?你是否使用了AppCompat?如果可以,请发布你的styles.xml - Alex Townsend
尝试将按钮的父样式更改为Widget.AppCompat.Button,并在您的list_selector.xml中将此选项放置在最后:<item android:drawable="@drawable/gradient_bg" />(假设这是您想要的默认背景)。 - Alex Townsend
谢谢您的时间,我找到了问题并发布了答案。 - Selva

0

在MarshMallow设备中,您应该以编程方式处理存储权限。


0
嘿,检查一下你的样式。 你的列表颜色在提供的API图像中是不同的。 我认为列表项文本颜色是白色,所以在API 23中看不见。 尝试改变那个颜色,我相信它会起作用的。 *注意 - 请在所有API中保持相似的主题。

是的,文本颜色是白色的,但我已将列表项和列表视图颜色设置为黑色,为什么只有在API 23中才会出现这种情况? - Selva

0

我认为textColor是白色的。所以你看不到行项目。你可以改变textColor,一切都会正常工作。

祝你好运!


是的,文本颜色是白色的,但我已将列表项和列表视图的颜色设置为黑色,为什么只有在API 23中才出现这种情况? - Selva
检查列表项中的前景色 - Avani Nagar

0
我已找到问题所在,它是在列表项线性布局中 android:foreground="#EBEBEB" 这个属性导致的白色颜色。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接