在Android Studio上创建意图时出错

时间:2014-10-11 09:25:08

标签: java android android-intent

使用Lompa中的import android.content.Intent;解决主.java文件和清单上的问题,但不在第二个.java文件中解决

以下是代码:

package com.tutorial.helloworld;

import android.os.Bundle;
import android.util.Log;
import android.content.Intent;

public class SecondActivity extends Activity {

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

        String varString = getIntent().getStringExtra("Test");
        Log.d("HelloWorld - Second Activity", varString);
    }
}

这里有错误:

G:\Crear_Apps\Projects\HelloWorld\app\src\main\java\com\tutorial\helloworld\SecondActivity.java

Error:(6, 37) error: cannot find symbol class Activity

Error:(8, 5) error: method does not override or implement a method from a supertype

Error:(10, 9) error: cannot find symbol variable super

Error:(11, 9) error: cannot find symbol method setContentView(int)

Error:(13, 28) error: cannot find symbol method getIntent()

我正在按照教程的步骤给我一个错误,创建一个" intent",我搜索了解决方案,但没有任何作用,如果是因为Android Studio被窃听或者是我的错误,我也重新启动了Android Studio,但没有任何反应。

MyActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Log.d("HelloWorld","onCreate");

    Intent i = new Intent(this, SecondActivity.class);
    i.putExtra("Test","true");

    startActivity(i);
}

SecondActivity.java

public class SecondActivity extends Activity{

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

        String varString = getIntent().getStringExtra("Test");
        Log.d("HelloWorld - Second Activity",varString);
    }
}

的AndroidManifest.xml

        </activity>
            <activity android:name="com.tutorial.helloworld.SecondActivity">
            </activity>
    </application>

我得到的错误

G:\Crear_Apps\Projects\HelloWorld\app\src\main\java\com\tutorial\helloworld\MyActivity.java
Error:(20, 9) error: cannot find symbol class Intent
Error:(20, 24) error: cannot find symbol class Intent
G:\Crear_Apps\Projects\HelloWorld\app\src\main\java\com\tutorial\helloworld\SecondActivity.java
Error:(6, 37) error: cannot find symbol class Activity
Error:(8, 5) error: method does not override or implement a method from a supertype
Error:(10, 9) error: cannot find symbol variable super
Error:(11, 9) error: cannot find symbol method setContentView(int)
Error:(13, 28) error: cannot find symbol method getIntent()

2 个答案:

答案 0 :(得分:1)

更改此

    </activity>
    <activity android:name="com.tutorial.helloworld.SecondActivity">
    </activity>
    </application>

对此

    </activity>
    <activity android:name=".SecondActivity">
    </activity>
    </application>

也尝试更改此内容

    public class SecondActivity extends Activity

对此

    public class SecondActivity extends AppCompatActivity

导入

    import android.support.v7.app.AppCompatActivity;
    import android.content.Intent;

答案 1 :(得分:0)

试试此代码..您应该使用MyActivity.this而不只是this 同步android studio一次并清理你的项目并重建它..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Log.d("HelloWorld","onCreate");

    Intent i = new Intent(MyActivity.this, SecondActivity.class);
    i.putExtra("Test","true");

   startActivity(i);
}