2 alllllllll alllllllll 于 2013.04.27 15:12 提问

从spinner中开始新的activity

需要实现的功能是spinner中一个选项可以开始一个新的activity。但是应用每次执行到这都会崩溃。我用了intent和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
    }
}
}

2个回答

jnhoodlum
jnhoodlum   2013.04.27 16:23
已采纳

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
    }
}

改成

**public class CustomOnItemSelectedListener 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);
             **Abs.this.startActivity(i);** 
            break;
        }
        // **************************** above here is where I start the new activity
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
}

试试

denghaofa
denghaofa   2013.05.13 14:14

你在Manifest中注册了第二个Activity没?

suannai0314
suannai0314 这个问题已经解决了,你可以看下上边的答案。
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!