-1

Hello I'm new on android I have this trouble with my code

package com.example.spinner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {


Spinner lista;
String[] datos = {"opcion1", "opcion2", "Ir a listView"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lista = (Spinner) findViewById(R.id.lista1);
    ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, datos);
    lista.setAdapter(adaptador);
    lista.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view,
                int i, long l) {
            // TODO Auto-generated method stub
            switch (i){
            case 1:
                Toast to = Toast.makeText(getApplicationContext(), datos[i], Toast.LENGTH_SHORT);
                to.show();
                break;
            case 2:
                Intent int = new Intent(MainActivity.this, VentanaListView.class);
                break;
            }
        }

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

        }

    });
}

}

In the switch case 2 is where a litte box appears saying :

Intent cannot be resolved to a variable

I have already import the Intent using import android.content.Intent; and It says

The import android.content.Intent is never used

Please do you know what should I do?

4

3 回答 3

3

You can't name a variable int; that's a keyword. Choose a different variable name, such as intent.

于 2013-11-01T19:38:26.677 回答
3
 Intent int = new Intent(MainActivity.this, VentanaListView.class);

int is a reseverd word in java (a Keyoword) ,and it can not be used a variable`s name

于 2013-11-01T19:38:40.927 回答
3

Change this

   Intent int = new Intent(MainActivity.this, VentanaListView.class)

to

   Intent intent = new Intent(MainActivity.this, VentanaListView.class)

int is a keyword in java

You probably want to call startActivity(intent) also

于 2013-11-01T19:38:42.177 回答