2013-04-27 82 views
0

我試圖從我的微調框中選擇一個選項時開始新的活動,但每當我選擇啓動新活動的應用程序崩潰時。我開始有意向的新的活動,我使用onItemSelected的微調從微調框開始新的活動

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.Spinner; 
import android.widget.TextView; 

public class Abs extends Activity { 

private Spinner StretchType; 
TextView tv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(
      WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
    setContentView(R.layout.abs); 
    addListenerOnSpinnerItemSelection(); 
    tv = (TextView) findViewById(R.id.tvTypeofStretch); 

} 

public void addListenerOnSpinnerItemSelection() { 
    StretchType = (Spinner) findViewById(R.id.sSType); 
    StretchType 
      .setOnItemSelectedListener(new CustomOnItemSelectedListener()); 
} 

public class CustomOnItemSelectedListener extends Activity implements 
     OnItemSelectedListener { 

    public void onItemSelected(AdapterView<?> parent, View view, int pos, 
      long id) { 
// **************************** below here is where I start the new activity 
     switch (pos) { 
     case (1): 
      Intent i = new Intent(this, Practice.class); 
      startActivity(i); 
      break; 
     } 
     // **************************** above here is where I start the new activity 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 
} 

} 
+0

顯示您的logcat它會很容易解決你的問題.. – 2013-04-27 04:22:16

+0

當你說一聲,請張貼logcat的跟蹤 – Pragnani 2013-04-27 04:24:12

回答

1

使用classname.this參考外部類:

Intent i = new Intent(Abs.this, Practice.class); 
Abs.this.startActivity(i); 
+0

THANKYOU非常多,你不會碰巧知道如何創建一個自定義佈局的警報對話,其中有一個微調框,可以做到我上面所問的內容? – user2109242 2013-04-27 04:42:18

+0

@ user2109242我看到你正在嘗試使用一個Activity作爲對話框,沒什麼不好。但是您可能想要按照[本教程](http://www.mkyong.com/android/android-custom-dialog-example/)中所示的方式執行自定義警報對話框,而不是教程中顯示的按鈕,你可以放置一個微調器。 – 2013-04-27 04:49:55

0

您應該替換爲以下startActivity(i) -

Abs.this.startActivity(i); 

另外,我看到你的擴展ActivitySpinnerAdapter類。這是需要的嗎?