在Android中遇到新的类问题

时间:2013-09-26 13:44:50

标签: java android class this

我真的很厌倦了Java,而且我是Android的新手,我把一些代码放在一起,当它在MainActivity Class中完全正常时。但现在我想清理函数并将它们放在自己的类中以使代码看起来很漂亮。这是我想要做的一个例子。我没有在IDE中收到任何错误,但是我的设备崩溃,MainActivity试图运行setArtist和setAlbum函数。

public class MainActivity extends Activity {

private ListView lv;
String path, selectedFromList;
ArrayAdapter<String> adapter;
public ViewFiller k = new ViewFiller();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv = (ListView) findViewById(R.id.listView1);

    k.setArtist();
    k.setAlbums();

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, k.getAlbums());
    lv.setAdapter(adapter);

    ImageButton albumButton = (ImageButton) findViewById(R.id.albumButton);
    ImageButton artistButton = (ImageButton) findViewById(R.id.artistButton);

    albumButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            k.album(lv, adapter);
        }
    });

    artistButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            k.artist(lv, adapter);
        }
    });

}

}

继续上课这堂课我遇到了麻烦......

public class ViewFiller extends MainActivity  {

public ViewFiller(){
    }

ArrayList<String> Albums = new ArrayList<String>();
ArrayList<String> Artist = new ArrayList<String>();
LinkedHashSet<String> hs = new LinkedHashSet<String>();

public ArrayList<String> getAlbums() {
    return Albums;
}

public void setAlbums() {

    String selection = AudioColumns.IS_MUSIC + " != 0";
    String[] projection = { BaseColumns._ID, AudioColumns.ALBUM };
    Cursor c = this.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
            selection, null, null);

    while (c.moveToNext()) {
        Albums.add(c.getString(1));
    }
    hs.clear();
    hs.addAll(Albums);
    Albums.clear();
    Albums.addAll(hs);
}


public ArrayList<String> getArtist() {
    return Artist;
}

public void setArtist() {

    String selection = AudioColumns.IS_MUSIC + " != 0";
    String[] projection = { BaseColumns._ID, AudioColumns.ARTIST, };
    Cursor c = this.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
            selection, null, null);

    while (c.moveToNext()) {
        Artist.add(c.getString(1));
    }

    hs.clear();
    hs.addAll(Artist);
    Artist.clear();
    Artist.addAll(hs);
}

public void artist(ListView lv, ArrayAdapter<String> s) {
    lv.setAdapter(null);
    s = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, Artist);
    lv.setAdapter(s);
}

public void album(ListView lv, ArrayAdapter<String> s) {
    lv.setAdapter(null);
    s = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, Albums);
    lv.setAdapter(s);

}

}

1 个答案:

答案 0 :(得分:1)

我认为你不希望ViewFiller成为一项活动......但我可能错了。如果它只是一个常规类,您正在使用它来创建主活动使用的某个对象,并且您需要访问MainActivity,只需将上下文传递给对象的构造函数。

编辑:

尝试这样的事情,我对Android也有点生气,所以我不确定它是否会起作用(我可能完全错了,对不起):

Activity activity;
public ViewFiller(Activity activity){
     this.activity = activity;
}

和...而不是this.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);

尝试:

activity.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);

在您的主要活动中:ViewFiller k = new ViewFiller(this);

但是,我不确定这是不是你想要的。如果我在某处出错,我会道歉。