2010-10-21 146 views
0

嗨我有兩個類在android和一個類中我寫了一個數組,我想在主類中訪問它,但錯誤是給我,這裏的「強制關閉」是我的代碼訪問數組與對象

package com.semanticnotion.DAO; 


import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class DAO extends Activity { 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

WordsDAO DAO = new WordsDAO(new String[]{"Arte","Arquitectura","Familia","Moda","Cotilleos","Cine","Libros","Historia","Pintura","Musica","Tendencies","Modernimso","Pop art","Masteialismo","realities","filosofia","moda","fotografia","religion"}); 


Button next = (Button) findViewById(R.id.Button01); 
next.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
     Intent myIntent = new Intent(view.getContext(), WordsDAO.class); 

     startActivity(myIntent); 
     } 
    }); 
    } 
} 

和第二類代碼爲

package com.semanticnotion.DAO; 

public class WordsDAO { 


String[] words = new String[]{"Arte","Arquitectura","Familia","Moda","Cotilleos","Cine","Libros","Historia","Pintura","Musica","Tendencies","Modernimso","Pop art","Masteialismo","realities","filosofia","moda","fotografia","religion"}; 




public WordsDAO(String[] words) { 
    this.words=words; 
    } 

請任何一個可以告訴什麼以及在此代碼中的錯誤thaks

+0

這是一個確切的重複http://stackoverflow.com/questions/3987349/accessing-array-with-object?不要這樣做。 – 2010-10-21 13:43:01

回答

1

該錯誤可能是在這裏:

Intent myIntent = new Intent(view.getContext(), WordsDAO.class); 

(你應該張貼的錯誤跟蹤)

意圖構造函數需要一個組件(活動)類作爲第二個參數。一個任意的類是不允許的。

最簡單的做法是使用putExtra。您可以使用此方法傳遞CharSequence數組,然後使用getCharSequenceArrayExtra檢索它。