Error: This class should provide a default constructor - java

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

Related

how to fix my intent when i put the parameters

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

Class 'Anonymous class derived from FirebaseAdapter' must be declared abstract in Adapter

I've been getting an error (in my FirebaseRecyclerAdapter) saying that the class must be declared abstract or implement method 'onCreateViewHolder(ViewGroup, int) in 'Adapter', as well as Method does not override superclass on the "Override" just below that code.
However, I have no idea how to change my code to get rid of that error. Can someone please help me? I would really appreciate the help. Thank you!
DiaryActivity.java package com.shiminu1521462c.fyp_2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
public class DiaryActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private DatabaseReference fNotesDatabase;
private RecyclerView mNotesList;
private GridLayoutManager gridLayoutManager;
private DatabaseReference noteRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diary);
mNotesList = (RecyclerView) findViewById(R.id.notes_list);
gridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
mNotesList.setHasFixedSize(true);
mNotesList.setLayoutManager(gridLayoutManager);
mNotesList.addItemDecoration(new GridSpacingDecoration(2, dpToPx(10), true));
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child(mAuth.getCurrentUser().getUid());
}
loadData();
}
#Override
public void onStart() {
super.onStart();
}
private void loadData() {
Query query = fNotesDatabase.orderByValue();
FirebaseRecyclerAdapter<NoteModel, NoteViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<NoteModel, NoteViewHolder>(
NoteModel.class,
R.layout.single_note_layout,
NoteViewHolder.class,
query
) {
#Override
protected void populateViewHolder(final NoteViewHolder viewHolder, NoteModel model, int position) {
final String noteId = getRef(position).getKey();
fNotesDatabase.child(noteId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("title") && dataSnapshot.hasChild("timestamp")) {
String title = dataSnapshot.child("title").getValue().toString();
String timestamp = dataSnapshot.child("timestamp").getValue().toString();
viewHolder.setNoteTitle(title);
//viewHolder.setNoteTime(timestamp);
GetTimeAgo getTimeAgo = new GetTimeAgo();
viewHolder.setNoteTime(getTimeAgo.getTimeAgo(Long.parseLong(timestamp), getApplicationContext()));
viewHolder.noteCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(DiaryActivity.this, DiaryNewNoteActivity.class);
intent.putExtra("noteId", noteId);
startActivity(intent);
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mNotesList.setAdapter(firebaseRecyclerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.diary_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.new_note_button:
Intent newIntent = new Intent(DiaryActivity.this, DiaryNewNoteActivity.class);
startActivity(newIntent);
break;
}
return true;
}
/**
* Converting dp to pixel
*/
private int dpToPx(int dp) {
Resources r = getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
}
com.firebase.ui.database.FirebaseRecyclerAdapter is abstract.
If you want to extend it (as you do with an anonymous class in your method loadData()) you have to implement all its abstract methods. In that case apparently onCreateViewHolder(ViewGroup, int).
Add this method implementation to your anonymous class and you are done.

implements View.OnClickListener on MainActivity doesn't work

Still in my learning phase and either I misunderstand the implements keyword. Am at the chapter where the app's MainActivity is supposed to handle all click related functions.
I don't know if it's the book missing something, or I misread something in the book. But when I debug the app I never get the OnClick call. Otherwise the app runs with no errors.
While I already know how to assign a new button listener am stuck on this chapter and would love to be able to move on.
Here's the entire code, it's very short.
Would appreciate knowing what I am missing here.
Thank you
package ted.com.eventhandling;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity2 extends AppCompatActivity
implements View.OnClickListener
{
#Override
protected void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View view)
{
switch(view.getId())
{
case R.id.button1:
show("Button One");
break;
case R.id.button2:
show("Button Two");
break;
case R.id.button3:
show("Button Three");
break;
default:
show("Oh shit");
break;
}
}
void show(String message)
{
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
Log.i(getClass().getName(), message);
}
}
Use the below code,
package ted.com.eventhandling;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity2 extends AppCompatActivity
implements View.OnClickListener
{
#Override
protected void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
}
#Override
public void onClick(View view)
{
switch(view.getId())
{
case R.id.button1:
show("Button One");
break;
case R.id.button2:
show("Button Two");
break;
case R.id.button3:
show("Button Three");
break;
default:
show("Oh shit");
break;
}
}
void show(String message)
{
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
Log.i(getClass().getName(), message);
}
}
You implemented the onClick but forgot to give clickListeners for your buttons, so none of buttons were not linked.

How to update(refresh) ListView Widget from Activity?

package com.example.ss.new_my_wid;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;
public class ItemChanging extends Activity implements View.OnClickListener, View.OnKeyListener
{
EditText EdT;
Button btn_OK;
Button btn_Cancel;
Button btn_Delete;
Intent intent;
String str, changed_text;
Toast tst;
final static String CURRENT_TEXT_IN_ITEM = "current_text_in_item";
final static String POSITION = "position";
public ItemChanging() {
super();
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.type_to_item);
EdT = (EditText) findViewById(R.id.editText);
intent = getIntent();
str = intent.getStringExtra(CURRENT_TEXT_IN_ITEM);
EdT.setText(str, TextView.BufferType.EDITABLE);
btn_OK = (Button)findViewById(R.id.but_OK);
btn_Cancel = (Button) findViewById(R.id.but_Cancel);
btn_Delete = (Button) findViewById(R.id.but_Del);
btn_OK.setOnClickListener(this);
btn_Cancel.setOnClickListener(this);
btn_Delete.setOnClickListener(this);
EdT.setOnKeyListener(this);
}
#Override
public void onClick(View v)
{
int pos = intent.getIntExtra(POSITION,0);
switch (v.getId())
{
case R.id.but_OK:
changed_text = EdT.getText().toString();
MyFactory.data.set(pos, changed_text);
/*HERE need update of ListView*/
tst = Toast.makeText(this, changed_text, Toast.LENGTH_SHORT);
tst.show();
finish();
break;
case R.id.but_Cancel:
finish();
break;
case R.id.but_Del:
MyFactory.data.remove(pos);
MyFactory.data.add("");
finish();
break;
}
}
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
return false;
}
}
I have widget with ListView and activity with button. In activity I change an item in the ListView. When I click on the button changes are saved in ListView, but I dont know how to update the listView and the widget from activit.How can I do this?
P.S. sorry for my english =)
Call notifyDataSetChanged() on your Adapter object once you've modified the data in that adapter.
How to refresh Android listview?
When ever you Made changes in your listview's data then you need to notify your adapter with new data
you can do it by calling notifyDataSetChanged(); method on your adaptor
for examlple
adapter.notifyDataSetChanged();

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