如何从 java class(不是 activity,无布局)调用方法到 activity?
How to call method from a java class (not an activity, no layout) to an activity?
我不能使用意图,原因如下。另外,我尝试调用该方法,但它抛出 NullPointerException
.
我想做的是将字符串 songName
从 ListActivity
发送到 class,后者有一个方法 getSongIndex()
,它比较传入的字符串(songName
) 到 ArrayList
中的歌曲然后 returns 索引(整数)到调用 activity.
无法使用 Intent 的原因:
如果我从 ListActivity's
onClickListener
发送一个意图到 java class,接收方的 getIntent.getExtras()
会导致错误。此外,我需要 java class 中的另一个意图将 songIndex
发送回 ListActivity
。
这是所需的代码:
This is the function in java class: SongsManager.java This is
how I get songName in the method and compare it to songtitles in the
phone:
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() 中收到了另一个 activity 的意图)。我读到了 BroadcastReciever,但它看起来真的很麻烦。任何其他简单的方法将不胜感激。
谢谢
问题是:
final Cursor mCursor = getApplicationContext().getContentResolver().query(...
行。
因为SongsManager
是正常的java class那么getApplicationContext()
方法怎么访问呢?
表示 Activity 或任何其他组件在 SongsManager
class 中扩展但未在 AndroidManifest.xml
中注册
因此,要解决此问题,请删除您在 SongsManager
中扩展的 class 并在 getSongIndex
方法中传递上下文以访问 getContentResolver()
.like:
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());
我不能使用意图,原因如下。另外,我尝试调用该方法,但它抛出 NullPointerException
.
我想做的是将字符串 songName
从 ListActivity
发送到 class,后者有一个方法 getSongIndex()
,它比较传入的字符串(songName
) 到 ArrayList
中的歌曲然后 returns 索引(整数)到调用 activity.
无法使用 Intent 的原因:
如果我从 ListActivity's
onClickListener
发送一个意图到 java class,接收方的 getIntent.getExtras()
会导致错误。此外,我需要 java class 中的另一个意图将 songIndex
发送回 ListActivity
。
这是所需的代码:
This is the function in java class: SongsManager.java This is how I get songName in the method and compare it to songtitles in the phone:
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() 中收到了另一个 activity 的意图)。我读到了 BroadcastReciever,但它看起来真的很麻烦。任何其他简单的方法将不胜感激。 谢谢
问题是:
final Cursor mCursor = getApplicationContext().getContentResolver().query(...
行。
因为SongsManager
是正常的java class那么getApplicationContext()
方法怎么访问呢?
表示 Activity 或任何其他组件在 SongsManager
class 中扩展但未在 AndroidManifest.xml
因此,要解决此问题,请删除您在 SongsManager
中扩展的 class 并在 getSongIndex
方法中传递上下文以访问 getContentResolver()
.like:
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());