如何将方法从 java 类(不是活动,没有布局)调用到活动



>我不能使用意图,原因如下所述。另外,我尝试调用该方法,但它抛出NullPointerException.

我正在尝试做的是将字符串songNameListActivity发送到具有方法getSongIndex()的类,该方法将传入的字符串(songName)与ArrayList中的歌曲进行比较,然后将索引(整数)返回给调用活动。

我不能使用意图的原因:如果我将意图从ListActivity's onClickListener发送到 java 类,接收端的getIntent.getExtras()会导致错误。另外,我需要 java 类中的另一个意图将songIndex发送回ListActivity

以下是必需的代码:

这是java类中的函数:SongsManager.java这是 我如何在方法中获取歌曲名称并将其与歌曲标题进行比较 电话:

  public int songIndex;
ArrayList<String> songs = new ArrayList<String>();
public int getSongIndex(String s, Context c) {
    String songName = s;
    String songTitle;
    String song;
    final Cursor mCursor = c.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Audio.Media.TITLE}, null, null,
            "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
    /* run through all the columns we got back and save the data we need into the arraylist for our listview*/
    if (mCursor.moveToFirst()) {
        do {
            song = MediaStore.Audio.Media.TITLE;//mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
            songs.add(song);
        } while (mCursor.moveToNext());
    }
    String tempSong;
    for(int i=0; i <songs.size();i++) {
        tempSong = songs.get(i);
        if(songName.equalsIgnoreCase(tempSong))
        {
            songIndex = i;
        }
    }
    mCursor.close(); //cursor has been consumed so close it
    return songIndex;
}

这就是我在ListActivity中实例化SongsManager对象的方式: public SongsManager manager = new SongsManager();

这是ListActivity的onClickListener中的代码,用于调用函数getSongIndex。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                TextView textView = (TextView) view.findViewById(android.R.id.text1);
                String songName = textView.getText().toString();
                songIndex = manager.getSongIndex(songName);
                // Starting new intent
                Intent i = new Intent(getApplicationContext(),MusicPlayerActivity.class);
                Log.d("TAG", "onItemClick");
                //// Sending songIndex to PlayerActivity
                i.putExtra("songIndex", songIndex);
                startActivity(i);
            }
        });

我正在寻找一种解决或破解此问题的方法。有没有办法使用意图解决这个问题(请注意,我的 ListActivity 已经从它 onCreate() 中的另一个活动接收了意图)。我读过关于BroadcastReciever的文章,但它看起来真的很麻烦。任何其他简单的方法将不胜感激。谢谢

问题是:

final Cursor mCursor = getApplicationContext().getContentResolver().query(...

线。

因为SongsManager是普通的 java 类,那么getApplicationContext()方法如何在其中访问呢?

表示活动或任何其他组件在类中扩展SongsManager但未在AndroidManifest.xml中注册

因此,要解决此问题,请删除您在SongsManager中扩展的类,并在getSongIndex方法中传递上下文以访问 .like getContentResolver()

public int getSongIndex(String s,Context mContext) {
     String songName = s;
     String songTitle;
     final Cursor mCursor = mContext.getContentResolver().query(...);
     ...
  return songIndex;
}

并将getSongIndex方法调用为:

songIndex = manager.getSongIndex(songName,getApplicationContext());

最新更新