Cannot resolve symbol view - Android Studio [duplicate] - java

This question already has answers here:
How to create a method inside onCreate() in Android
(2 answers)
Closed 4 years ago.
I have been following this tutorial to switch activites with a button - https://developer.android.com/training/basics/firstapp/starting-activity
I am getting the error "Cannot resolve symbol view" from searching this is usually because people haven't imported View, I have.
Also seems to think newPacket is a variable "Variable 'newPacket' is never used"
Can't for the life of me workout what is going wrong here
package com.alexitconsoluting.app.contactclock;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainClock extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_clock);
public void newPacket(View view) {
Intent intent = new Intent(this, NewPacket.class);
startActivity(intent);
}
}
}

You are creating a method inside method which is not allowed
Create Outside of OnCreate()
public class MainClock extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_clock);
}
public void newPacket(View view) {
Intent intent = new Intent(this, NewPacket.class);
startActivity(intent);
}
}

Related

Edittext field Not detecting. Throws Null Pointer Exception [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
java.lang.NullPointerException- error in Android
(1 answer)
How to Handle NullPointerException in android?
(2 answers)
Closed 3 years ago.
I tried a number of things but my Application keeps Crashing.
I tried debugging it is returned this error when I run the application.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
package com.zybooks.pigdice;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
final Context context = this;
//private EditText playerName;
private String name;
private int target_score_display;
//public TextView player_name;
private EditText targetScore;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//player_name = findViewById(R.id.player_name);
targetScore = dialog.findViewById(R.id.enter_target_score);
//playerName = findViewById(R.id.enter_player_name);
Button start_game = findViewById(R.id.start_game);
start_game.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialogbox);
dialog.setTitle("Title...");
Button dialogButton = dialog.findViewById(R.id.start_game_dialog);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
String value = targetScore.getText().toString();
target_score_display = Integer.parseInt(targetScore.getText().toString());
Intent intent = new Intent(getApplicationContext(),Gameplay.class);
intent.putExtra("TARGET_SCORE",target_score_display);
Bundle bundle = new Bundle();
bundle.putString("TARGET_SCORE",value);
intent.putExtras(bundle);
startActivity(intent);
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
}
});
dialog.show();
}
});
}
}
Trying to pass input taken in the dialog box to the next Activity. Have been struggling with this for over 3 hours.
You can only call dialog.findViewById after the dialog is created (inside the onClick). It has no content when the Activity is created, so you can't find its views. For example, dialogButton is gotten correctly
So when you try to get the text from the non existing view, it will throw the exception

Invalid Method Declaration - Return type required

Having issues with the line I labelled. Tried multiple different suggestions but none seem to fix the issue, anyone has anymore suggestions?
package connect2you.com;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
findViewById(R.id.textViewSignup).setOnClickListener(this); // error on this line
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.textViewSignup:
startActivity(new Intent(this, SignUpActivity.class));
break;
}
}
}
Put your call tobfindViewById() inside a method. You can't run code outside of methods.

How to fix "error: cannot find symbol method startActivity(Intent)" in a class of Listener?

I can make an Intent to open other Activity with writing the code in MainActivity.java.
Then I try to make an Intent using a class and called it in MainActivity.java. But it becomes error.
How to solve this problem?
When I write startActivity(numberIntent); in MainActivity.java there is no error but when I move this line of code to NumbersClickListener.java
Errors come:
error: cannot find symbol method startActivity(Intent)
error: not an enclosing class: MainActivity
This my code
In MainActivity.java
package com.example.android.*****;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListener clickListener = new NumbersClickListener();
TextView numbers = (TextView)findViewById(R.id.numbers);
numbers.setOnClickListener(clickListener);
}
in NumbersClickListener.java
package com.example.android.*****;
import android.content.Intent;
import android.view.View;
android.widget.Toast first
import android.widget.Toast;
OnClickListener should be written in capital letter
public class NumbersClickListener implements View.OnClickListener {
#Override
public void onClick(View view) {//.makeText(view.getContext(),
"open the list of numbers", Toast.LENGTH_SHORT).show();
Intent numberIntent = new Intent(MainActivity.this,
NumbersActivity.class);
startActivity(numberIntent);
}
}
error: cannot find symbol method startActivity(Intent)” in a class of Listener?
Because if startActivity(Intent) is a method of activity and its required call from context
If you want call startActivity(Intent) outside activity you need to use
Context.startActivity(numberIntent);
Use this
view.getContext().startActivity(numberIntent);
instead of this
startActivity(numberIntent);
SAMPLE CODE
public class NumbersClickListener implements View.OnClickListener {
#Override
public void onClick(View view) {
Intent numberIntent = new Intent(view.getContext(),
NumbersActivity.class);
view.getContext().startActivity(numberIntent);
}
}
You are defining NumbersClickListener in a separate java file. It has no way compiler will know that when u call startActivity you are referring to the Activity.startActivity
Unless you have a deeper purpose for NumbersClickListener.java, simply do inline declaration of View.Listener will do
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListener clickListener = new NumbersClickListener();
TextView numbers = (TextView)findViewById(R.id.numbers);
numbers.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent numberIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(numberIntent);
}
});
}
In place MainActivity.this, use its context.
Intent numberIntent = new Intent(context, NumbersActivity.class);
startActivity(numberIntent);
Notice the changes I have made
MainActivity.java
package com.example.android.*****;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListener clickListener = new NumbersClickListener(MainActivity.this); // Context while creating ClickListener Object
TextView numbers = (TextView)findViewById(R.id.numbers);
numbers.setOnClickListener(clickListener);
}
NumbersClickListener.java
package com.example.android.*****;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;
public class NumbersClickListener implements View.OnClickListener {
Context context;
NumbersClickListener(Context c){
this.context = c;
}
#Override
public void onClick(View view) {
Intent numberIntent = new Intent(context, NumbersActivity.class);
startActivity(numberIntent);
}
}
To startActivity you need Context.
It will be like this context.startActivity()
In MainActivity it is not giving error because Activity internally extends Context.
NumbersClickListener is not extended Context.
So, you can start activity using View context
Replace startActivity(numberIntent) with
view.getContext().startActivity(numberIntent);
inside your NumberClickListener class you can do the following
Context context = view.getContext();
Intent numberIntent = new Intent (context, NumberActivity.class);
context.startActivity(numberIntent);
By using this code you can use your NumberClickListener with any other activity.
happy codding :)

Android Studio: playing with Intents and sending data between activities

I'm new to Android Studio and Java, currently I'm learning about intents and sending data/text from one activity to another. On the second activity I get "Cannot resolve symbol 'intent'" even though it's in onCreate field. What I'm trying to do is to have two text fields for first and last name, sending the text from first activity to the second one which will read it, and all done with onButtonClick method. I'm only trying to do the first name and the code looks like this MainAcitivty
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClicked(View view) {
EditText nameText = (EditText)findViewById(R.id.firstName);
TextView messageText = (TextView) findViewById(R.id.messageTextView);
if(nameText.getText().toString().trim().length() == 0){
messageText.setText("text here");
} else {
String textForSecondAcitivity = "your name is: " + nameText.getText();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, textForSecondAcitivity);
startActivity(intent);
`
The problem is the second Activity that gives me the error "Cannot resolve symbol 'intent'". And here is the code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent scIntent = getIntent();
String messageText = intent.getStringExtra("EXTRA_TEXT");
TextView messageView = (TextView)findViewById(R.id.textView);
messageView.setText(messageText);
}
It wont show the error here but it's int this line
String messageText = intent.getStringExtra("EXTRA_TEXT");
I apologize in advance because surely I messed up something bad, I'm a beginner and if you have some advice on what's the best way to do what I'm actually trying to accomplish feel free to tell me.
The app should look like this in the end 1.The first activity
1.The second one showing first name and last name
You have no local variable named intent
Replace it with scIntent
Here you are making it wrong
intent.putExtra(Intent.EXTRA_TEXT, textForSecondAcitivity);
instead you use this
intent.putExtra("EXTRA_TEXT", textForSecondAcitivity);

Runtime error while switching between activities in Android

I got a runtime error when I press the button that should change the activity:
package com.example.LocationTracker;
import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;
public class LocationTracker extends Activity{ /** Called when the activity is first created. */
Button btn_Tracker;
Button btn_Display_Map;
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
btn_Tracker = (Button)findViewById(R.id.btn_Tracker);
btn_Tracker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//setContentView(R.layout.trackeractivity);
Intent myIntent1 = new Intent(view.getContext(), TrackerActivity.class);
context.startActivity(myIntent1);
}});
}
class TrackerActivity extends Activity {
//Your member variable declaration here
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trackeractivity);
}
}
I added everything right in the maniefest file
<activity android:name=".TrackerActivity" android:label="#string/app_name"/>
<activity android:name=".DisplayMapActivity" android:label="#string/app_name"/>
</application>
Any idea?
I think TrackerActivity needs to be public, which means it will need to be in its own file as well.
You shouldn't be using getApplicationContext() to start activities. Every activity is a context, so having a member instance of Context should not be necessary. Try re-writing the onClick method of your OnClickListener like this
public void onClick(View view) {
Intent myIntent1 = new Intent(LocationTracker.this, TrackerActivity.class);
LocationTracker.this.startActivity(myIntent1);
}});
Also, refer to this documentation for when to use the application context.

Categories