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

    }
}

}
4

2 回答 2

1

使用以下方法引用外部类classname.this

Intent i = new Intent(Abs.this, Practice.class);
Abs.this.startActivity(i);
于 2013-04-27T04:36:25.557 回答
0

您应该替换startActivity(i)为以下内容 -

 Abs.this.startActivity(i);

另外,我看到您正在扩展Activity课程SpinnerAdapter。有必要吗?

于 2013-04-27T04:24:59.720 回答