简体   繁体   中英

I am new in android development. I have a problem

From the main page, i click the image and it will open the question 1. Now, I trying to open a new activity which is question 2 using image view from the question 1. But it has an error: question 1 is not an enclosing class.

Here is the code for Main Activity.

package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

private ImageView b;


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

    //button video to video page
    b = (ImageView) findViewById(R.id.btnPlayVideo);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent i = new Intent(MainActivity.this, VideoPage.class);
            startActivity(i);

        }
    });


    //button start test to question1
    b = (ImageView) findViewById(R.id.btnStartTest);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent i = new Intent(MainActivity.this, Question1.class);
            startActivity(i);

        }
    });



    //button question 1 ke question 2
    b = (ImageView) findViewById(R.id.btn1);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent i = new Intent(Question1.this, Question2.class);
            startActivity(i);

        }
    });}}

This is question 1.java. it said that it is not an enclosing class.

package com.example.adhdtracker;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class Question1 extends AppCompatActivity {

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

This is question 2.java

package com.example.adhdtracker;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class Question2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question2);
}
}
Intent i = new Intent(Question1.this, Question2.class);
startActivity(i);

You are trying to start Question2 from the context of Question1 within MainActivity. The first parameter of new Intent() should be the context of the enclosing class that will start the next activity. Since you are defining this in MainActivity, the context should be MainActivity.this. There is a great introduction to this topic here .

Either change the first parameter of new Intent() to "MainActivity.this" or move the entire second OnClickListener to a view that is within Question1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM