2012-08-05 2 views
0

내 애플리케이션에 대화 상자를 표시해야합니다. 이 대화 상자에는 회 전자가 있습니다. 그래서 내가 대화 상자를 표시하고 회 전자 채우기 위해이 코드를 사용 : android.view.WindowManager $ BadTokenException : 제대로

public class setup4 extends Activity { 

public List<String> materie = new ArrayList<String>(); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.setup4); 

    Database d = new Database(this); 
    SQLiteDatabase db = d.getWritableDatabase(); 
    Cursor cursor = db.rawQuery("select * from materie", null); 
    if (cursor.moveToFirst()) { 
     do materie.add(cursor.getString(1)); while (cursor.moveToNext()); 
    } 
    db.close(); 
} 



//On bottone setup 4 
public void onSetup4bottone(View v) 
{ 
    AlertDialog.Builder customDialog = new AlertDialog.Builder(this); 
    customDialog.setTitle("Aggiungi ora scolastica"); 
    LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view=layoutInflater.inflate(R.layout.aggiungi_ora,null); 
    customDialog.setView(view); 

    Spinner spinner = (Spinner) view.findViewById(R.id.aggiungi_ora_materia); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item, materie); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_item); 
    spinner.setAdapter(adapter); 
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
     public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
      String selected = (String) parentView.getItemAtPosition(position); 
      Toast.makeText(
        getApplicationContext(), 
        "hai selezionato "+selected, 
        Toast.LENGTH_LONG 
       ).show(); 
     } 

     public void onNothingSelected(AdapterView<?> parentView) { 
      // your code here 
     } 

    }); 

    customDialog.show(); 
} 
} 

스피로드 항목을하지만 난 값이 오류와 응용 프로그램 충돌을 변경하려면 그것을 클릭 할 때 : 나는 또한이 스레드 Android Spinner Error : android.view.WindowManager$BadTokenException: Unable to add window을 찾을 내가 솔루션을

를 이해할 수 창 을 추가 할 수 없음은 대신 LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

에게 U 해결 당신의 spinner.setOnItemSelectedListener()에서 자체

LayoutInflater layoutInflater = getLayoutInflater();

답변

0

, getApplicationContext() 전화 대신이 작성하지 않습니다

spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
     String selected = (String) parentView.getItemAtPosition(position); 
     Toast.makeText(
       setup4.this,  //<===THIS LINE IS CHANGED BY ME 
       "hai selezionato "+selected, 
       Toast.LENGTH_LONG 
      ).show(); 
    } 
+0

문제는 토스트 아니다 열리는. 그러나 나는 당신의 솔루션을 시도했지만 오류가 동일하게 유지됩니다. 제안 주셔서 감사합니다 :) – barbo

1

시도를 다른 컨텍스트에 창을 추가 할 수 있습니다.

당신의 라인을 다음과 같은 하나 ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item, materie);

교체 :이 대화 상자에 올바르게 표시 있기 때문에

ArrayAdapter<String> adapter = new ArrayAdapter<String>(setup4.this /**Your activity_name.this*/,android.R.layout.simple_spinner_item, materie); 

가 작동하는지 알려줘 ...

+0

작동하지 않습니다 :(나는 setup4.this, view.getContext()와 같은 많은 컨텍스트를 시도하지만 항상 충돌합니다. 또한 회 전자의 이벤트 (OnItemSelectedListener 및 onNothingSelected)를 제거하려고하지만 크래시 : (다른 제안? – barbo

+0

당신은 이미'getApplicationContext()'를 시도 했습니까? – yugidroid

+0

제가 문제를 해결하고, 편집을 읽습니다. – barbo

관련 문제