我有一个MainActivity.java和SongLoad.java。 MainActivity.java
的代码是
public class MainActivity extends Activity {
public static final int LOADREQUEST = 100;
private String callerClass = "DoubleMode";
ImageButton loadPlaylist1,loadPlaylist2;
ImageButton play1,play2;
SongLoad songloader = new SongLoad(callerClass);
MusicPlayerService musicplayer = new MusicPlayerService();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
loadPlaylist1 = (ImageButton)findViewById(R.id.load_button1);
loadPlaylist2 = (ImageButton)findViewById(R.id.load_button2);
play1 = (ImageButton)findViewById(R.id.play1);
play2 = (ImageButton)findViewById(R.id.play2);
}
public void onLoad(View v) {
int loadButtonPressed = v.getId();
songloader.playListGenerator();
musicplayer.songLoader(songloader.songIndex, loadButtonPressed);.. soon
SongLoad.java
public class SongLoad extends ListActivity{
public int songIndex = 0;
public String selectedSongPath;
public String callerClass;
final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory().getPath());
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongLoad(String callerClass) {
this.callerClass = callerClass;
}
SongLoad() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(callerClass == "DoubleMode")
{
setContentView(R.layout.playlist);
}
}
public void playListGenerator() {
mediaScanner();
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
ListAdapter adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
R.id.songTitle });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// 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;
selectedSongPath = songsList.get(songIndex).get("songPath");
}
});
}
现在我的问题是,当我创建一个SongLoad.java对象并调用playListGenerator时,调用onCreate方法。但是我希望它能够执行。我该怎么做?
答案 0 :(得分:1)
如何通过在android
中创建类的实例来执行onCreate方法
你没有。
当我创建SongLoad.java的对象
时
删除此代码。 Android 创建活动实例; 你没有。也:
删除SongLoad
空构造函数
将来自playListGenerator()
的来电转移到MainActivity
onCreate()
SongLoad
通过SongLoad
startActivity()
方法致电onLoad()
,开始MainActivity
活动
每个活动都是相对隔离的,就像Web应用程序的一个Web页面不会从同一个Web应用程序中的某个其他Web页面调用JavaScript函数一样。
您最终需要进行其他更改,例如在执行I / O时正确使用后台线程,但这会让您开始。
您还应该考虑阅读the documentation on activities。