how to fix my intent when i put the parameters - java

i get some problem when i use the intent, he said "cannot resolve constructor Intent('....')" , i dont know what is wrong in my code but you can see what i m importing here
package com.example.noen.myintentapp;
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 implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn_move);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.btn_move:
Intent intn = new Intent(MainActivity.this,target1.class);
break;
}
}
}

Related

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.

Error on OnClickListener Method

I am not sure why I am getting this error: imgur.com/a/Hxz5O
Everything seems to be in the right methods and everything so it is a mystery to me why I am getting the error.
Here is my code:
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
Button SuSe;
#Override
protected void onCreate(Bundle savedInstanceState) {
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
});
}
}
You are declaring the View.OnClickListener inside another View.OnClickListener for no reason. Remove the second one e.g.
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
}
});
You need setContentView() and your clickListener not correct.
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
Button SuSe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myView);
SuSe = (Button)findViewById(R.id.MyButtonId);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}

Error: This class should provide a default constructor

I tried to generate a signatured APK but then this error appears:
Error:Error: This class should provide a default constructor (a public
constructor with no arguments)
(com.penta.games.mrpolitik.SpendenDialog) [Instantiatable]
This is my Dialog file
package com.penta.games.mrpolitik;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class SpendenDialog extends Dialog implements
android.view.View.OnClickListener {
private Activity c;
private Button yes, no;
public SpendenDialog(Activity a) {
super(a);
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.spenden_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
Uri uri = Uri.parse("https://patreon.com/user?u=5716519&utm_medium=social&utm_source=twitter&utm_campaign=creatorshare2");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
c.startActivity(intent);
break;
}
switch (v.getId()) {
case R.id.btn_no:
break;
}
dismiss();
}
}
How can I solve this error?
How can I solve this error?
Step #1: Delete your constructor
Step #2: Delete your c field
Step #3: Replace all former references to c with getContext()

Class or interface expected at the end of the first set of closing brackets

Keep getting a class or interface expected error at the end of the first set of closing brackets, I have a feeling it has to do with the onCreate method, but I'm not sure how to go about fixing it.
package shake.shake;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainPage extends AppCompatActivity {
private static Button ShakeButton1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
OnClickButtonListener();
}
}
public void OnClickButtonListener()
{
ShakeButton1 = (Button)findViewById(R.id.ShakeButton);
ShakeButton1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentSecondActivity = new
Intent(MainPage.this, ShakePage.class);
startActivity(intentSecondActivity);
}
}
);
}}
you have extra braces here
public class MainPage extends AppCompatActivity {
private static Button ShakeButton1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
OnClickButtonListener();
}
} <-------- extra brace.
It's better having a look at codes before posting.
Your method public void OnClickButtonListener() is outside the class MainPage. Move that method inside your class.

Using simple text view and button widgets in android

Im trying to make a basic program where you press a button and it adds to an integer. Im having trouble updating the text view.
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num;
TextView numview;
private void updateNumber(){
TextView numview = (TextView)findViewById(R.id.Number);
numview.setText(num);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupButton();
}
private void setupButton() {
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num = num + 1;
numview.setText(num);
}
});
}
Help would be great im trying to get into android and books havent been very helpful
There are a few mistakes here. First, you are never calling updateNumber() so numviewis never set. Moreover, you have two TextView numView once global in your MainActivity and again in updateNumber(). There is a better way to do this. But first go to activity_main.xml file and within the button tag add the the line android:onClick="onClick" something like this :
<Button
android:id="#+id/button1"
...
android:onClick="onClick"/>
Next in your MainActivity.java, add the following lines of code :
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num = 0;
TextView numview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numview = (TextView)findViewById(R.id.Number);
}
public void onClick(View view){
num += 1;
numview.setText(String.valueOf(num));
}
}
onClick() will be automatically called when your button is clicked because of the android:onClick property.
you have two different objects with same name and it would cause error.
one of them is defined in your class (TextView numview;) and the other one is defined in your function update number.
in your onClick method you want to change numView defined in your class and it's not initialized. you have to add change your code :
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num;
TextView numview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numview = (TextView)findViewById(R.id.Number);
setupButton();
}
private void setupButton() {
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num = num + 1;
numview.setText(num);
}
});
}
Put this code in your onClick function
num = num + 1;
updateNumber();
You are never calling updateNumber()
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num=0;
TextView numview;
private void updateNumber(int number){
numview = (TextView)findViewById(R.id.Number);
numview.setText(number);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupButton();
}
private void setupButton() {
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num = num + 1;
updateNumber(num);
}
});
}

Categories