Android RadioButton CompoundButton.OnCheckedChangeListener Error - java

I have a question for my code, it should change the background color of my Start-Activity. I should choose between blue and red with radio buttons (in Radiogroup). I always get this error:
Error:(8, 8) error: com.example.clecks.reaction_game.OnCheckedChangeListener is not abstract and does not override abstract method onCheckedChanged(CompoundButton,boolean) in android.widget.CompoundButton.OnCheckedChangeListener
C:\Users\clecks\Desktop\asdfasdf\app\src\main\java\com\example\clecks\reaction_game\OnCheckedChangeListener.java
When I click on the error message there opens a new class named
OnCheckedChangeListener.java :
public class OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {}
Here is my code:
public class activity_settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_settings);
colorchange();
}
public void colorchange() {
final RelativeLayout background = (RelativeLayout) findViewById(R.id.start);
final RadioButton changeToBlue = (RadioButton) findViewById(R.id.button_blue);
final RadioButton changeToRed = (RadioButton) findViewById(R.id.button_red);
final Button button_save = (Button) findViewById(R.id.button_save);
button_save.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
changeOption(background);
}
});
changeToBlue.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeToBlue(background);
}
});
changeToRed.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeToRed(background);
}
});
}
public void changeOption(RelativeLayout background) {
if (background.isEnabled()) {
background.setEnabled(false);
} else {
background.setEnabled(true);
}
}
public void changeToBlue(RelativeLayout background) {
background.setBackgroundColor(0x0000FF00);
background.invalidate();
}
public void changeToRed(RelativeLayout background) {
background.setBackgroundColor(0x0000FF00);
background.invalidate();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_settings, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}

CompoundButton itself is an abstract class (extending `Button).
You should not have CompoundButton in your activity_activity_settings layout.
Use Button for example instead.

Related

Handle SettingsActivity onBackpressed() the same way as top bar back button

I have a SettingsActivity that has an "up" button included on the top bar. My AettingsActivity also has headers, that load proper settings fragments. By default, when I press the top bar "up" button, it closes the visible fragment and returns to headers, and closes activity if pressed on header view. Howeer, my phone "back" button works different - it just closes the activity. How ca i make "up" arrow and back button work the same way to keep my app consistent? Should I add onBackPressed to my activity, and if so, what should I put there?
My SettingsActivity:
public class SettingsActivity extends AppCompatPreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (!super.onMenuItemSelected(featureId, item)) {
NavUtils.navigateUpFromSameTask(this);
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
#Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| NewsPreferenceFragment.class.getName().equals(fragmentName)
|| OtherPreferenceFragment.class.getName().equals(fragmentName);
}
public static class NewsPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_news);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
public static class OtherPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_other);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
This is a default code generated from Android Studio when creating new SettingsActivity, just with some changes made to suit my app
You just need to override onBackPressed() method:
#Override
public void onBackPressed() {
NavUtils.navigateUpFromSameTask(this);
}

setOnTouchListener working only for one view

I have a XML layout, on which I'm applying OnTouchListener, which task is to detect finger swipe direction; anyway, it's working only when swipe is performed just on layout, but not on it's items (like ImageViews, Buttons etc.). How do I rewrite it to make swipe working on whole layout, but avoid creating dozens of Listeners for all items on it?
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
>
</RelativeLayout>
Code:
View relative = findViewById(R.id.main_layout);
relative.setOnTouchListener(...)
Because you are setting OnclickListener for only one view which is parent. so that's why there is only one click response.
You can try this.
Button button1 = (Button) findviewbyid(R.id.buttonid);
button.setOnTouchListener(...)
Similarly images or other views.
Edit -
DO this for every view that you want to listn swipe for.
ImageView im = (ImgaeView) findviewbyid(R.id.imageid)
im.setOnTouchListener(...)
Similarly with Every View for Listning Swipe.
EDIT- So you don't wanna make more ONTouchListener. Make another class which implements OnTouchListener
public class Mytouchlistener implements OnTouchlistener{
#Override
public boolean onTouch(View v, MotionEvent event) {
HERE PUT YOUR CODE.
}
Then
Youanyview.OnTouchlistener(MyTouchlistener.class);
Got the idea???
you can implement View.OnTouchListener as shown below
public class SomeClass implements View.OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()){
case R.id.view1:
//do something
break;
case R.id.view2:
//do something here too
break;
default:
break;
}
return false;
}
}
be aware that you must use setOnTouchListener .
good luck .
You can use following code to make a perfect android gesture:
Type following code at Main_activity.java:
package com.androidtutorialpoint.myapplication;
public class MainActivity extends AppCompatActivity implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener {
private TextView output_text;
private GestureDetectorCompat DetectMe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output_text = (TextView) findViewById(R.id.outputText); // Taking reference of text to be displayed on screen
DetectMe = new GestureDetectorCompat(this,this);
DetectMe.setOnDoubleTapListener(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
// Following functions are overrided to show text when a particular method called.
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
output_text.setText("onSingleTapConfirmed");
return true;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
output_text.setText("onDoubleTap");
return true;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
output_text.setText("onDoubleTapEvent");
return true;
}
#Override
public boolean onDown(MotionEvent e) {
output_text.setText("onDown");
return true;
}
#Override
public void onShowPress(MotionEvent e) {
output_text.setText("onShowPress");
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
output_text.setText("onSingleTapUp");
return true;
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
output_text.setText("onScroll");
return true;
}
#Override
public void onLongPress(MotionEvent e) {
output_text.setText("onLongPress");
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
output_text.setText("onFling");
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event){
this.DetectMe.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Multiple markers at this line help me

I am getting the message multiple markers at this line:
- Syntax error, insert ")" to complete
Expression
- Syntax error, insert ";" to complete
Statement
- Syntax error, insert "}" to complete
ClassBody
This is the code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Menemukan lokasi tombol di activity_main.xml
Button tombol = (Button) findViewById(R.id.button1);
//Menangkap Klik pada Tombol
tombol.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//TODO Auto-generated method stub
Intent Intentku = new Intent(MainActivity.this,AktivitasBaru.class);
startActivity(Intentku);
} <<<<<<<<<< eror
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You have not closed your on click listener anonymous class properly, you should have:
tombol.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Intentku = new Intent(MainActivity.this,AktivitasBaru.class);
startActivity(Intentku);
}
});
You are missing the last line.

Error On Null Pointer But Still Unsolved on Android Studio

i am a newbie for Android Programming.
Here i have some problem when i want to make an activity (call Category_Setting) that showing list view, but when i switch from main activity to Category_Setting the error report in Android Studio gimme some error like this
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ever_ncn.cashflow/com.example.ever_ncn.cashflow.CategorySetting}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
I have googling for it, asking some friend, but still i even don't know what is my error for.
Please somebody answer my question with simple understanding words.
Thank You..
NB. here is my code for MainActivity.java
public class MainActivity extends Activity implements OnItemSelectedListener {
private static Button BtnINewTrans;
private static Button BtnIViewCash;
private static Button BtnIAddCateg;
Spinner my_Spinner;
DatabaseHelper dbHelper = new DatabaseHelper(this);
//ArrayAdapter<String> adapterCategory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
my_Spinner.setOnItemSelectedListener(this);
select_spinner_Category();
onButtonClickButtonListener();
}
/*ArrayList<String> my_array = new ArrayList<String>();
my_array = getTableValues();*/
/*ArrayAdapter my_Adapter = new ArrayAdapter(this, R.layout.spinner_row, my_array);
My_spinner.setAdapter(my_Adapter);*/
public void select_spinner_Category () {
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
DatabaseHelper dbH = new DatabaseHelper(getApplicationContext());
List<String> listCategory = dbH.getAllCategory();
ArrayAdapter<String> adapterCategory = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listCategory);
adapterCategory
.setDropDownViewResource(android.R.layout.simple_spinner_item);
my_Spinner.setAdapter(adapterCategory);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id){
String label = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "You selected "+label,
Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
/*ArrayList<String> arrayCategory;
arrayCategory = dbHelper.getAllCategory();
selectCategory = (Spinner) findViewById(R.id.spnCategSelect);
ArrayAdapter adapterCategory = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayCategory);
// adapterCategory = new ArrayList<String>(this, android.R.layout.simple_spinner_item, R.id.spnCategSelect, AllCategoryList);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectCategory.setAdapter(adapterCategory);
selectCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onButtonClickButtonListener(){
BtnINewTrans = (Button)findViewById(R.id.btnNewTrans);
BtnINewTrans.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentNewTrans = new Intent ("com.example.ever_ncn.cashflow.NewTransaction");
startActivity(intentNewTrans);
}
}
);
BtnIViewCash = (Button)findViewById(R.id.btnViewCashflow);
BtnIViewCash.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentViewCash = new Intent ("com.example.ever_ncn.cashflow.ViewCashflow");
startActivity(intentViewCash);
}
}
);
BtnIAddCateg = (Button)findViewById(R.id.btnAddCateg);
BtnIAddCateg.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.CategorySetting");
startActivity(intentAddCateg);
}
}
);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is Category_Setting.java (where i get this error)
public class CategorySetting extends Activity {
private SQLiteDatabase db;
private CursorAdapter currAdapter;
private static Button BtnIAddCateg;
private static Button BtnICancelCateg;
private static final String TAG = CategorySetting.class.getSimpleName();
DatabaseHelper dBHelper = new DatabaseHelper (this);
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onButtonClickButtonListener();
//reload();
}
public void reload(){
listView = (ListView) findViewById(R.id.listViewCateg);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "clicked on item: " + position);
}
}
);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_category_setting, menu);
return true;
}
public void onButtonClickButtonListener(){
BtnIAddCateg = (Button)findViewById(R.id.btnAddNewCateg);
BtnIAddCateg.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.AddCategory");
startActivity(intentAddCateg);
}
}
);
BtnICancelCateg = (Button)findViewById(R.id.btnCancelCateg);
BtnICancelCateg.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
}
);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
In this method in Category_Setting.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onButtonClickButtonListener();
//reload();
}
you forgot to load the layout. You should add some code like you used in the MainActivity:
setContentView(R.layout.activity_main);
Right now, the layout is not loaded at all, so
BtnINewTrans = (Button)findViewById(R.id.btnNewTrans);
will return null.

Android WebView: A particular site doesn't render properly.

I am working with a WebView which is supposed to open any website.
I have 2 activities-CallingActivity and UrlBrowser. The webview is in UrlBrowser.
The CallingActivity has an EditText which takes the URL from a user and supplies it to the UrlBrowser activity via a bundle object passed as an extra with an Intent object. The UrlBrowser activity uses this URL to load into a webview.
I have to update a progress bar with the loading progress also for which I've used the onProgressChanged method to update values .
The problem I'm facing is that a particular website: www.flipkart.com is not rendering properly. A blank light brownish patch is all that is visible in the webview.
I tried changing the user agent of the WebView. On putting the user agent string of a desktop chrome browser all websites including flipkart opened perfectly, however, their desktop versions opened. I need the mobile version. As soon as I revert to using the user agent of a mobile browser, the same problem occurs. This led me to believe that maybe something is wrong with the user agents so I faked the user agent to chrome's user agent and the problem still persisted.
Here is the code for the two activities and other relevant files:
UrlBrowser.Java
public class UrlBrowser extends AppCompatActivity
{
WebView webView;
ProgressBar progressBar;
String finalUrl;
Context appcontext;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_url_browser);
webView=(WebView)findViewById(R.id.webView);
progressBar=(ProgressBar)findViewById(R.id.progressBar);
progressBar.setMax(100);
Bundle b= getIntent().getExtras();
String finalUrl="http://"+b.getString("url");
appcontext=getApplicationContext();
viewPage(finalUrl);
}
public class MyWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Toast.makeText(appcontext,"Waiting for "+url ,Toast.LENGTH_SHORT).show();
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
progressBar.setVisibility(view.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url)
{
progressBar.setVisibility(view.INVISIBLE);
}
}
public void viewPage(String url)
{
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.ge webView.getSettings().setAppCachePath("/data/data"+getPackageName()+"/cache");
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
webView.loadUrl(url);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_url_browser, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CallingActivity.java
public class CallingActivity extends AppCompatActivity{
Button goButton;
EditText urlField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calling);
goButton=(Button)findViewById(R.id.goButton);
urlField=(EditText)findViewById(R.id.urlField);
}
public void onClick(View v)
{
String url=urlField.getText().toString();
Intent i=new Intent(this,UrlBrowser.class);
Bundle b=new Bundle();
b.putString("url",url);
i.putExtras(b);
startActivity(i);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_calling, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
How can I solve this? Any help appreciated

Categories