【问题标题】:Please solve this error: [closed]请解决此错误:[关闭]
【发布时间】:2014-01-29 09:15:19
【问题描述】:

我无法使用 Intent 启动 Main Activity。我在此代码末尾提到的错误。我该如何解决这个错误?

Activity类型中的方法startActivity(Intent)不是 适用于参数(意图)

package com.example.rishabhsintent;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Intent extends Activity {
Button b;
EditText e;
TextView t;
SharedPreferences.Editor spe;
SharedPreferences sp;

public Intent(Intent intent, Class<MainActivity> class1) {
    // TODO Auto-generated constructor stub
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intent);
    spe = sp.edit();
    b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            spe.putString("Message", e.getText().toString());
            spe.commit();
            startActivity(new Intent(Intent.this, MainActivity.class));

        }//Error on startActivity;it says"The method startActivity(Intent) in the type Activity is not applicable for the arguments (Intent)"

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
           }
          }

【问题讨论】:

  • 请更改您的活动名称。现在真的很混乱。
  • 这个问题似乎是题外话,因为它是关于提问者直接提问的。

标签: android sharedpreferences


【解决方案1】:

你选择的Activity 的名字很奇怪。尝试像这样创建Intent

new android.content.Intent(Intent.this, MainActivity.class);

【讨论】:

    【解决方案2】:

    除了@FD_ 写的内容之外,您还向 startActivity() 方法传递了对您自己类的构造函数的调用,这没有任何意义:首先,因为构造函数不打算以这种方式使用,其次是因为构造函数什么都不返回。只需更改您的班级名称并声明一个“真实”的 Intent。

    【讨论】:

      【解决方案3】:

      它应该是活动的名称。 Intent 是 Android 的保留类,不应用于您的类。在startActivity(new Intent(Intent.this, MainActivity.class)); 行中,名称 Intent 不是唯一的,您必须知道使用了哪个类。

      【讨论】:

        【解决方案4】:
        Intent intenet = new Intent(Intent.this, MainActivity.class);
        startActivity(intenet);
        

        【讨论】:

        • OP 的代码没有区别。这不会解决问题。
        【解决方案5】:

        首先将您的活动定义到您的 AndroidManifest.xml 文件中

        例如:

         <activity android:name="YourPackage.MainActivity">
         </activity>
        

        第二次开始你的活动:

        Intent intent = new Intent(Intent.this, MainActivity.class);
        startActivity(intent);
        

        【讨论】:

          【解决方案6】:

          不要使用 Intent 作为类名。它是 android 中的一个保留类,否则 android 会感到困惑,并认为您引用的是意图而不是您的类。最好选择一个唯一的名称。

          【讨论】:

            猜你喜欢
            • 2021-09-14
            • 2011-08-27
            • 1970-01-01
            • 2018-01-13
            • 2011-09-24
            • 1970-01-01
            • 2021-04-21
            • 1970-01-01
            • 2023-02-11
            相关资源
            最近更新 更多