R.id. Cannot resolve symbol 'message' - java

This is the code and the error in "Cannot resolve symbol 'message'" on the line:
EditText editText = (EditText) findViewById(R.id.message);
The application worked well, previously. It's likely to be a stupid error.
File MainActivity.java
package com.example.antonio.newsbooklite2;
/**
* Created by antonio on 30/12/16.
*/
import android.os.Bundle;
import android.support.annotation.Nullable;
//import android.support.design.widget.*;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.*;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.ArrayList;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Called when the user clicks the Send button */
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
// Do something in response to button
}
}
File strings.xml
<resources>
<string name="app_name">newsbooklite2</string>
<string name="message">Scrivi messaggio: </string>
<string name="button_send">Send</string>
enter code here
<string name="nome_stringa">Testo_visualizzato</string>
</resources>

You might be mixing these things.
findViewById(R.id.message);
returns the View from the layout.
If you want the string, you can use:
getResources().getString(R.string.message);

You are using a message id defined in the string.xml file in line:
EditText editText = (EditText) findViewById(R.id.message);
Change the message to the id defined in the layout file.

Related

Inconvertible type cannot cast android.view.view to javafx.scene.web.webview

i want to show my post in webview but i fatch error of "Inconvertible type cannot cast android.view.view to javafx.scene.web.webview" in android studio.
how can i solve it please help me.
WPPostDetails.java
package com.ejobbox.ejobbox;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import javafx.scene.web.WebView;
public class WPPostDetails extends AppCompatActivity {
TextView title;
WebView webView;
#Override
public void onCreate(#Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_postdetails);
title=(TextView) findViewById(R.id.title);
webView= (WebView) findViewById(R.id.postwebview);
Intent i = getIntent();
int position = i.getExtras().getInt("itemPosition");
//title.setText(MainActivity.mListPost.get(position).getTitle().getRendered());
Log.e("WpPostDetails","title is"+MainActivity.mListPost.get(position).getTitle().getRendered());
webView.getSettings().setJavaScriptEnabled(true);
}
}
You import a wrong WebView class. Remove import javafx.scene.web.WebView; and add import android.webkit.WebView
Import
android.webkit.WebView instead of
javafx.scene.web.WebView;

Cannot resolve method 'getInstance()'

I was watching a youtube tutorial (Firebase storage and around 14 minutes I got this error code:
Cannot resolve method 'getInstance()'
Can someone help me resolve it? I am pretty new to Java and have been watch tutorials to get the understanding right. I know this is a small detail, but it has been really frustrating.
package bfb.ess.myapplicationbfb;
import android.app.ProgressDialog;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.UploadTask;
import java.io.File;
import java.io.IOException;
public class speakp extends AppCompatActivity {
private Button mRecordBtn;
private TextView mRecordlabel;
private MediaRecorder mRecorder;
private String mFileName = null;
private static final String LOG_TAG = "Record_log";
private StorageReference mStorage;
private ProgressDialog mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordpage);
mStorage = FirebaseStorage.getInstance().getReference(); /*RIGHT HERE*/
mRecordlabel = (TextView) findViewById(R.id.recordlabel);
mRecordBtn =(Button) findViewById(R.id.recordBtn);

android studio cannot resolve method(), ignoring import

I'm a total noob at android studio, and I have a (to me) weird problem. I have inserted a button in my XML document:
<Button
android:layout_width="match_parent"
android:layout_height="127dp"
android:text="SUM"
android:id="#+id/button"
android:layout_row="15"
android:layout_column="0" />
And in the java code I would like a to make it do something when i click on it. However in code (I know there is way too many import):
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class TestActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
R.id.button.onCliclistener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent action) {
DO THIS WHEN CLICKED ON
}
});
}
BUT it says: Cannot resolve method() and Cannot resolve symbol, to the onCliclistener and ActionListener. And it says: unused import statement, to thier imports. It's probably a stupid question, but what am I doing wrong?
Nicolaj
Try making reference to your button in your onCreate method and then create an onClick method.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class TestActivity extends AppCompatActivity implements View.OnClickListener {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
btn = (Button) findViewById(R.id.button); //Reference to the button
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
DO THIS WHEN CLICKED ON
}
}
I expect it will be helpful for you!

Background music and shared preferences

i want to toggle the background music on and off using settings , which is an activity and it can be launched by pressing the menu button through the main activity and selecting settings. the problem I am facing now is that when i tick the background music checkbox , upon clicking save and returning to the main activity , my music stops playing. how to i make sure that the music keeps playing after going back to the main activity? And when i stop the app and relaunch the activity , i want the settings to remain the way they were selected (using sharedpreferences). Can anyone take a look at my codes and see what I am doing wrong?
my MainActivity.java
package sp.com;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import android.app.TabActivity;
import android.widget.TabHost;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.media.MediaPlayer;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.setting:
Intent intent = new Intent(MainActivity.this, Settings.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
My settings.java
package sp.com;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.View.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
public class Settings extends Activity implements OnClickListener {
CheckBox Backmusic;
Button button1;
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.xml.preferences);
Backmusic = (CheckBox) findViewById(R.id.backmusic);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
loadSavedPreferences();
}
private void loadSavedPreferences(){
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
boolean checkBoxValue = sharedPreferences.getBoolean("CheckBox_Value",false);
if (checkBoxValue){
Backmusic.setChecked(true);
} else {
Backmusic.setChecked(false);
}
}
private void savePreferences(String key, boolean value){
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
#Override
public void onClick(View v){
savePreferences("CheckBox_Value",Backmusic.isChecked());
if (Backmusic.isChecked()){
mp = MediaPlayer.create(getBaseContext(), R.raw.sound);
mp.start();
} else {
mp.stop();
finish();
}
}
#Override
protected void onStop() {
super.onStop();
mp.stop();
finish();
}
}
Thanks in advance.
So right now you are just playing the music within your Settings activity. In fact you are calling mp.stop() in onStop(). That's why when it closes the music stops.
You should also put code in your MainActivity in onCreate() and onResume() to read from sharedpreferences and if the background music is true, play music. That could look something like this:
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
boolean backgroundMusic = sp.getBoolean("CheckBox_Value",false);
if (backgroundMusic)
{
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.sound);
mp.start();
}
else
{
mp.stop();
}
Note that you'll now want to write to SharedPreferences in Settings using getApplicationContext() instead of this. That way the preferences will be saved across the entire application and not just one activity.

Android crashing on activity start

Android crashes whenever this activity is started. here is the code.
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SettingsActivity extends Activity {
EditText vname, vphone, vemail, vaddress;
TextView textDeviceID;
Button settings_submit;
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.settingmenu);
vname = (EditText) findViewById(R.id.get_name);
vphone = (EditText) findViewById(R.id.get_phone);
vemail = (EditText) findViewById(R.id.get_email);
vaddress = (EditText) findViewById(R.id.get_address);
settings_submit = (Button) findViewById(R.id.settings_submit);
settings_submit.setOnClickListener(settings_submitOnClickListener);
TextView textDeviceID = (TextView) findViewById(R.id.deviceid);
super.onCreate(savedInstanceState);
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(
you should tell us more details. But I see that everything looks fine, except that
super.onCreate(savedInstanceState);
should go before setContentView() and all other stuff. By the way, check your xml for bad config controls too.

Categories