簡體   English   中英

如何從Java類(不是活動,沒有布局)到活動調用方法?

[英]How to call method from a java class (not an activity, no layout) to an activity?

我無法使用意圖,原因如下。 另外,我嘗試調用該方法,但它拋出NullPointerException

我想做的是將String songNameListActivity發送到具有方法getSongIndex()的類,該方法將傳入的String( songName )與ArrayList歌曲進行比較,然后將索引(整數)返回給調用方活動。

我之所以不能使用意圖:如果我到從意圖ListActivity's onClickListener Java類中, getIntent.getExtras()在接收端會導致錯誤。 另外,我需要Java類中的另一個意圖將songIndex發送回ListActivity

這是必需的代碼:

這是java類中的函數: SongsManager.java這是我在方法中獲取songName並將其與手機中的歌曲標題進行比較的方法:

  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()方法?

意味着Activity或任何其他組件在SongsManager類中進行了擴展, SongsManagerAndroidManifest.xml注冊

因此,要解決此問題,請刪除您已在SongsManager擴展的類,並在getSongIndex方法中傳遞Context以訪問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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM