android studio cannot resolve method(), ignoring import - java

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!

Related

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);

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.

Toggle button onClick method does not work

I have toggle button which has android:onClick=onToggleClicked. I tried to reference the onToggleClicked method from the java code but the problem is, eclipse underscore the onToggleClicked with red and does not recognizes it. How to fix this error.
Imports:
import java.util.Set;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.os.Build;
JavaCode:
if (myBluetoothAdapter == null) {
tb_OnOFF.setEnabled(false);
btnFind.setEnabled(false);
btnPaired.setEnabled(false);
tvStatusCaption.setText("Status: Not Supported.");
Toast.makeText(getApplicationContext(), "Your Device Does Not Support " +
"Bluetooth", Toast.LENGTH_LONG).show();
} else {
public void onToggleClicked (View view) {
boolean on = ((ToggleButton) view).isChecked();
if (on) {
//enable bluetooth
}
if (!on) {
//disable bluetooth
}
}
}
XML:
<ToggleButton
android:id="#+id/btnToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="#string/toggle_turn_on"
android:textOff="#string/toggle_turn_off"
android:checked="true"
android:onClick="onToggleClicked" />
onToggleClicked is a public method and needs to be attached to your Activity. You cannot use this method as an inner method inside onCreate method.
USE THIS
mToggleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mToggleButton.isChecked()) {
//your code
}
else{
//your code
}
}
});

Using Loader with SherlockActivity show error in initLoader(o, null, this);

I'm facing problem with initLoader() while using Loader. Its not taking the third argument null. Can any one help me to fix the error?
ERROR MESSAGE:: The method initLoader(int, Bundle,
LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not
applicable for the arguments (int, null, TestActivity)
And this is my code.
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import utilcontentprovider.F1DatabaseMetaData.TeacherTableMetaData;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class TestActivity extends SherlockActivity
implements LoaderCallbacks<Cursor>{
ListView listView;
ArrayList<String> listName = new ArrayList<String>();
ArrayList<String> listId = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
listView = (ListView) findViewById(R.id.list);
getLoaderManager().initLoader(0, null, this);
}
}
your imports refer to the support package so you should use getSupportLoadManager() instead of getLoaderManager().
Change
getLoaderManager().initLoader(0, null, this);
with
getSupportLoaderManager().initLoader(0, null, this);

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