在Android Studio中打开第二个Activity错误

时间:2018-10-02 07:49:34

标签: android android-activity

我试图对两个活动进行小规模的应用,但它给我一个代码错误..................... ................................................... ................................................... ................................................... ................................................... .........

the error

主要活动Java代码:

package com.example.amr.startnewactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity
{
    private Button op_btn;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onClickButtonListener();
    }
    public void onClickButtonListener()
    {
        op_btn= (Button)findViewById(R.id.button);
        op_btn.setOnClickListener(
                new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Intent intent = new Intent(".secondActivity");
                        startActivity(intent);

                    }
                }
        );
    }
}

第二个活动Java代码:

package com.example.amr.startnewactivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class secondActivity extends AppCompatActivity {

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

Android manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.amr.startnewactivity">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".secondActivity"></activity>
        <intent-filter>
            <action android:name=".secondActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </application>

</manifest>

主要activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="152dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="116dp"
        android:layout_marginLeft="116dp"
        android:layout_marginTop="248dp"
        android:text="open"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

第二个活动.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".secondActivity">

</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:1)

将此代码添加到您的按钮中,单击

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

OR

startActivity(new Intent(MainActivity.this,secondActivity.class))

并尝试遵循Java中的命名约定。看here

答案 1 :(得分:1)

您尝试过这个吗?

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

答案 2 :(得分:0)

您要寻找的是Intent(Context context, Class<?> class)

startActivity(new Intent(MainActivity.this, SecondActivity.class);

或者:

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

有关更多信息,请查看documentation ("Start another activity")


第二个注意事项:考虑从<intent-filter>文件中删除AndroidManifest.xml的{​​{1}}。这声明了一个过滤器,供其他应用以编程方式访问。

在这种情况下,如果您使用的是secondActivity,则实际上并不需要它。通常用于Activity