I am new in android development. I have a problem - java

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.

Related

discussing getActivityResult in class

There must be a way to get intent result in a class without using onActivityResult. By using other methods...
I dont know how, but Iam sure there is a way.
My class that should get the result of the intent filepicker from this class without using onActivityResult in the MainActivity.java that extents activity. FilePicker.java
package com.hadiawali.codeeditor;
import android.content.Intent;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
public class FilePicker {
Intent filePicker = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
public static void startPicking(Activity activity) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Intent chooseFolder = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
chooseFolder.addCategory(Intent.CATEGORY_DEFAULT);
activity.startActivityForResult(Intent.createChooser(chooseFolder, "Choose directory"), 9999);
}
}
}
My class that extents activity. MainActivity.java
package com.hadiawali.codeeditor;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = findViewById(R.id.btn);
btn.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
FilePicker.startPicking(MainActivity.this);
//I need to get the intent reslut from the class without using onActivityResult
}
});
}
}
For example you send to intent from activity A to B then
in Activity A create Activity Result like below
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// callback called
}
}
});
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//call B activity
someActivityResultLauncher.launch(intent);
}
in Activity B your task is successfully complete then set
setResult(RESULT_OK);
finish();
or if task is not complete successfully then put
setResult(RESULT_CANCELED);
finish();

'OnClickListener' is abstract; cannot be instantiated and Cannot resolve symbol 'v'

package com.example.application;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener());
#Override
public void onClick(View v)
Intent intent = new Intent(this, ClaimApplication.class);
startActivity(intent);
}
}
I have error such as 'OnClickListener' is abstract; cannot be instantiated and Cannot resolve symbol 'v'
I am a new programmer so please someone help!
You cannot instantiate abstract classes, so the new View.OnClickListener() gives the error.
As said in the Android Doc (https://developer.android.com/reference/android/widget/Button), try this
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here
}
});
and for keeping the code as simple as possible I suggest to call a method inside the onClick(),
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myMethod();
}
});
}
public void myMethod(){
//codehere
}

Two buttons, it mix between the links

Two buttons, it mix between the links
I have a page with two buttons, when I had the first button, it took me to the link, when I had the second button, the first stopped and the second took me to the second link, help!
PageOne.jave
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
setContentView(R.layout.weoff);
}
}
MainActivity.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.abus);
button = (Button) findViewById(R.id.weoff);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
}
}
Here
button = (Button) findViewById(R.id.abus);
button = (Button) findViewById(R.id.weoff);
It replaces the old reference to R.id.weoff so you will add the listener to weoff, two times which replaces the old listener and you set it with a listener which points to PageOne.class.
You should use two variables
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
and
button1.setOnClickListener(new OnClickListener() {
button2.setOnClickListener(new OnClickListener() {
Since the two listeners point to the same Intent, you could create the listener only one time and put the same?

Button one should aim to page one

I have a page that has two buttons to "two" other pages, the thing is, button 1 and button 2 lead to a second page, thought button 1 should lead to the first page and button 2 should lead to the second page, here is the java file, I don't know how to put it!
ActiveMain.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button1;
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTwo.class);
// you made a mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
}
}
PageOne.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
}
}
PageTwo
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageTwo extends Activity {
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weoff);
}
}
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTWO.class); // you made a
mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});

Interface OnClickListener cannot use my onClick Method?

here is my big problem:
http://www.bilder-space.de/show_img.php?img=7371f3-1333218887.jpg&size=original
and the code:
package org.madmax.sudoku;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class SudokuActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up Click Listener for all Buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
}
The error is:
-void is an invalid type for the variable onCLick()
Why does eclipse give me this errors?
you can use onclick inside your onCreate method like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code here
}
});
}
You need to bring onClick out from onCreate method. Nested method declarations are not allowed in programming.
package org.madmax.sudoku;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class SudokuActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up Click Listener for all Buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
You have defined onClick method inside of the onCreate method.
You can't have a method inside another method in Java. Put the onClick() after onCreate(), not inside it.

Categories