【问题标题】:I can't create a Intent even if I import即使我导入,我也无法创建 Intent
【发布时间】:2013-11-01 19:36:46
【问题描述】:

您好,我是 android 新手,我的代码有这个问题

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

        }

    });
}

}

在 switch case 2 中出现一个小盒子,上面写着:

Intent 无法解析为变量

我已经使用import android.content.Intent; 导入了 Intent,它说

从不使用导入 android.content.Intent

请问你知道我该怎么办吗?

【问题讨论】:

    标签: java android eclipse android-intent import


    【解决方案1】:

    改变这个

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

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

    int是java中的关键字

    您可能还想致电startActivity(intent)

    【讨论】:

    • 谢谢,我不知道!!
    • 是的,但我从不联想那个jajaja
    【解决方案2】:

    你不能命名一个变量int;那是一个关键字。选择一个不同的变量名,例如intent

    【讨论】:

      【解决方案3】:
       Intent int = new Intent(MainActivity.this, VentanaListView.class);
      

      int是java中的保留字(关键字),不能用作变量名

      【讨论】:

        猜你喜欢
        • 2022-07-06
        • 1970-01-01
        • 2020-02-12
        • 1970-01-01
        • 2017-05-21
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多