How to show a Toast using a RunOnUIThread method - java

I have been trying to figure out why the toast is not showing. from the code, the onReceive method does get executed and change the button color, but the Toast does not display. I dont know why.
client = new Client(
(publisher, topic, params) -> runOnUiThread(
() -> onReceive((Publisher) publisher, topic, params)
));
client.connectToServer("192.168.3.52",5050);
private void onReceive(Publisher publisher, String topic, Map<String, Object> params) {
sendBtn.setBackgroundColor(getColor(R.color.white));
Toast.makeText(MainActivity.this, "Hey", Toast.LENGTH_LONG).show();
}
Imports
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Class name
public class MainActivity extends AppCompatActivity {

hey can you share your file name and import section ........
or else try to run on clicklistener after sendbtn and try to link with your button

Related

How to fix RecognitionEngine error on android app using API.ai

I've been following some tutorials on how to make an app with API.ai integrated with it. But so far, unlike the tutorials, it gives me an error when I apply the Recognition Engine. This is the code on my Manifest.
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import ai.api.AIConfiguration;
import ai.api.AIListener;
import ai.api.android.AIService;
import ai.api.model.AIError;
import ai.api.model.AIResponse;
import ai.api.model.Result;
import ai.api.ui.AIButton;
public class MainActivity extends AppCompatActivity implements AIListener{
private AIService aiService;
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState) {
AIService aiService;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=findViewById(R.id.textView);
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO);
if (permission != PackageManager.PERMISSION_GRANTED) {
makeRequest();
}
final AIConfiguration config = new AIConfiguration("CLIENT_ACCESS_TOKEN",
AIConfiguration.SupportedLanguages.English,
AIConfiguration.RecognitionEngine.System);
aiService = AIService.getService(this, (ai.api.android.AIConfiguration) config);
aiService.setListener(this);
}
This line of code: AIConfiguration.RecognitionEngine.System);
I have all the dependencies that API.ai requests to add to the app gradle.
It says "Cannot resolve the symbol". Does anyone know why this error occurs or how to fix it?
You need to add "import ai.api.android.AIConfiguration;"

"Method does not override method from its superclass" - When creating ContextMenu in Android

I'm trying to show contextmenu on longpress in MainActivity.java. But I'm getting error "Method does not override method from its superclass" on #Override and "Cannot resolve symbol ... " for both ContextMenu and ContextMenuInfo in the onCreateContextMenu method.
My code is:
package com.syver.adhper;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.*;
import android.view.View;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.io.IOException;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.Queue;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createpnl();
//more codes here
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
//some codes here
//error is only in this section at #Override, ContextMenu and ContextMenuInfo
}
public void createpnl()
{
registerForContextMenu(...); //... means a layout
//more codes here
}
}
Why am I getting this error? What should I do to avoid this error? Please give me corrected complete code as I'm new in android developing. I have searched here and also google, but proper ans I did't find and also didn't understand.
My API is 19 (kitkat)
try this, it work in my android studio
#Override
public void onCreateContextMenu (ContextMenu menu,
View v,
ContextMenu.ContextMenuInfo menuInfo)
{
}
reference:https://developer.android.com/reference/android/app/Activity.html
also import the class by click the red, and press alt+enter, choose import the class, I think this would solve your problem

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

Using DialogFragment in android

I am learning to use DialogFragments
In my main activity in a condition i have
MyAlertDialogFragment alert = new MyAlertDialogFragment();
alert.show(getFragmentManager(), "Alert_Dialog");
MyAlertDialogFragment.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.widget.TextView;
import android.support.v4.app.DialogFragment;
import com.example.findmybuffet.R;
public class MyAlertDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
OnClickListener positiveClick = new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity().getBaseContext(), "Application finishing ...", Toast.LENGTH_SHORT).show();
getActivity().finish();
}
};
OnClickListener negativeClick = new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity().getBaseContext(), "No option selecting", Toast.LENGTH_SHORT).show();
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Do you want Yes or No ?");
builder.setNegativeButton("No", negativeClick);
builder.setPositiveButton("Yes", positiveClick);
builder.setTitle("Confirmation");
Dialog dialog = builder.create();
return dialog;
}
}
my problem::
In the line alert.show(getFragmentManager(), "Alert_Dialog");
I am getting error as The method show(FragmentManager, String) in
the type DialogFragment is not applicable for the arguments
(FragmentManager, String)
How to resolve this so that i am able to pop the fragment
As you're using android.support.v4.app.DialogFragment, you should pass to show() an instance of android.support.v4.app.FragmentManager which can be queried using an getSupportFragmentManager() call instead of getFragmentManager().
Replace getFragmentManager() with getSupportFragmentManager().

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

Categories