All the examples of invoking the Android camera with androidx.camera.core.ImageCapture are in Kotlin or are really out of date, and I am getting weird compilation errors with Java.
androidx.camera.core.ImageCapture imageCapture;
...
protected void onCreate(Bundle savedInstanceState) {
...
imageCapture = new ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.build();
...
}
public void snapQRCode(View view) {
imageCapture.takePicture(executor, new androidx.camera.core.ImageCapture.OnImageCapturedCallback(){
void onCaptureSuccess(ImageProxy image){}
void onError(ImageCaptureException exception) {}
}
);
}
This throws a compile error:
error: onCaptureSuccess(ImageProxy) in anonymous
xyz.MainActivity$1 cannot override
onCaptureSuccess(ImageProxy) in OnImageCapturedCallback
void onCaptureSuccess(ImageProxy image){}
I am confused because ImageCapture.OnImageCapturedCallback is abstract. I shouldn't be overriding at all. I am pretty sure I have the camera configured correctly, but if someone could explain this error, and perhaps provide a few lines of (Java) code that snaps the picture and returns the image, that would be much appreciated.
Here are the libraries I am using in app/build.gradle:
def cameraxVersion = "1.0.0-beta03"
implementation "androidx.camera:camera-core:${cameraxVersion}"
implementation "androidx.camera:camera-camera2:${cameraxVersion}"
implementation "androidx.camera:camera-lifecycle:${cameraxVersion}"
Related
I'm using retrofit to get my json data in my recycleview. It used to work fine couple of weeks ago but I ran code now then It only loads data one time and then as many times as I make change any text or add new value in json data, It always load the same initial data on loading. I haven't used any cache property and strange thing is once it loads first time, then if i delete my json then still it loads the data instead of throwing exception and giving error of json not found.
What am I missing. I changed the version of retrofit but it dosesn't seem to work. Here is my code of Mainactivity is
GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
Call<List<RetroPhoto>> call = service.getAllPhotos();
call.enqueue(new Callback<List<RetroPhoto>>() {
#Override
public void onResponse(Call<List<RetroPhoto>> call, Response<List<RetroPhoto>> response) {
progressDoalog.dismiss();
generateDataList(response.body());
}
#Override
public void onFailure(Call<List<RetroPhoto>> call, Throwable t) {
progressDoalog.dismiss();
Toast.makeText(NewsActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
}
/*Method to generate List of data using RecyclerView with custom adapter*/
private void generateDataList(List<RetroPhoto> photoList) {
recyclerView = findViewById(R.id.customRecyclerView);
adapter = new CustomAdapter(this,photoList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(NewsActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
and code of GetDataService is
public interface GetDataService {
#GET("b.json")
Call<List<RetroPhoto>> getAllPhotos();
}
and my gradle is
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.okhttp:okhttp:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.7.0'
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
Thanks in advance for any help. I am banging my heads on floor over 3 hours on figuring out what excatly gone through in couple of weeks where I even didn't touch the code.
I realized problem was from server side as I was using ipage hosing, the i moved json to other hosing and immediately changes were shown in app on request but ipage didn't. So code is fine.
I'm trying to integrate DynamoDB into a mobile app I'm developing using Android Studio and Kotlin. I've been following the AWS instructions for Android SDK 2.0 outlined here: https://aws-amplify.github.io/docs/sdk/android/start
The instructions are in Java, which I'm not very familiar with. I've been able to get things to work until step 5, which involves integrating the AWS AppSync client into the onCreate() method in my application. I tried to copy and paste the code into Android Studio, which then tries to convert the Java code to Kotlin, but I'm getting a lot of errors. Are there any Java/Kotlin experts out there that might be able to convert the following code to Kotlin for me?
private AWSAppSyncClient mAWSAppSyncClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAWSAppSyncClient = AWSAppSyncClient.builder()
.context(getApplicationContext())
.awsConfiguration(new AWSConfiguration(getApplicationContext()))
.build();
}
And
public void runMutation(){
CreateTodoInput createTodoInput = CreateTodoInput.builder().
name("Use AppSync").
description("Realtime and Offline").
build();
mAWSAppSyncClient.mutate(CreateTodoMutation.builder().input(createTodoInput).build())
.enqueue(mutationCallback);
}
private GraphQLCall.Callback<CreateTodoMutation.Data> mutationCallback = new GraphQLCall.Callback<CreateTodoMutation.Data>() {
#Override
public void onResponse(#Nonnull Response<CreateTodoMutation.Data> response) {
Log.i("Results", "Added Todo");
}
#Override
public void onFailure(#Nonnull ApolloException e) {
Log.e("Error", e.toString());
}
};
I haven't found many resources on integrating DynamoDB in an AndroidStudio Kotlin project, so if there are better ways of doing this, I'd love to hear them.
MainActivity cannot be converted to LifecycleOwner
I used this as LiveCycle Owner, but it is rejected and I got an error as you see in the picture.
I work on Api 25 and I this the problem may be related to this version
This is info about my sdk
compileSdkVersion 25
buildToolsVersion '25.0.2'
This is my code:
private void retrieveTasks() {
Log.d(TAG, "Actively retrieving the tasks from the DataBase");
// Extract all this logic outside the Executor and remove the Executor
// Fix compile issue by wrapping the return type with LiveData
LiveData<List<TaskEntry>> tasks = mDb.taskDao().loadAllTasks();
// Observe tasks and move the logic from runOnUiThread to onChanged
tasks.observe(this, new Observer<List<TaskEntry>>() {
#Override
public void onChanged(#Nullable List<TaskEntry> taskEntries) {
Log.d(TAG, "Receiving database update from LiveData");
mAdapter.setTasks(taskEntries);
}
});
}
I put LiveData dependencies in my Gradle
compile "android.arch.lifecycle:extensions:1.0.0"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
If anyone knows the reason for the problem, let me know please
Fragments and Activities in Support Library 26.1.0 and later already implement the LifecycleOwner interface by default
but in version 25 you need to implement LifecycleOwner interface
for example
public class MyActivity extends Activity implements LifecycleOwner {
private LifecycleRegistry mLifecycleRegistry;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLifecycleRegistry = new LifecycleRegistry(this);
mLifecycleRegistry.markState(Lifecycle.State.CREATED);
}
#Override
public void onStart() {
super.onStart();
mLifecycleRegistry.markState(Lifecycle.State.STARTED);
}
#NonNull
#Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
}
Source : Handling lifecycles with lifecycle-aware components
As you can read here the LifecycleOwner was added in support library 26.1.0. Easiest way to fix your issue would be upgrading your support library version.
Had the same error. Upgrading to androidx support libraries fixed the issue.
Choose inside Android Studio: Refactor -> Migrate to android x
I wrote some code to get data from Firebase, but it shows some errors. I will explain the errors below and attach a picture.
First error when I put the mouse on it, message show:
'onStart()' in 'com.abdullrahman.eng.myapp.FriendsFragment' clashes with 'onStart()' in 'android.app.Fragment'; attempting to assign weaker access privileges ('protected'); was 'public'
Second error when I put the mouse on it, message show:
Method does not override method from its superclass
Can anyone solve these problems and help me?
The code:
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Friends, FriendsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(
Friends.class,
R.layout.users_single_layout,
FriendsViewHolder.class,
mUsersDatabase
) {
#Override
protected void populateViewHolder(FriendsViewHolder friendsViewHolder, Friends friends, int position) {
friendsViewHolder.setDisplayName(friends.getName());
friendsViewHolder.setUserStatus(friends.getStatus());
friendsViewHolder.setUserImage(friends.getThumb_image(), mMainView.getContext());
final String user_id = getRef(position).getKey();
friendsViewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent profileIntent = new Intent(getActivity(), UserProfile.class);
profileIntent.putExtra("user_id", user_id);
startActivity(profileIntent);
}
});
}
};
mUsersList.setAdapter(firebaseRecyclerAdapter);
}
Errors do say a lot about the problem you have.
The first error is caused by protected access modifier on the onStart() method, while it should be public.
It should be
#Override
public void onStart() {
super.onStart();
// rest of the code
}
instead of
#Override
protected void onStart() {
super.onStart();
// rest of the code
}
You can find some more information about the error reason in docs available on Oracle site.
The second problem is related to the definition of FirebaseRecyclerAdapter. Looks like there is no method like
protected void populateViewHolder(FriendsViewHolder friendsViewHolder, Friends friends, int position).
I'd suggest checking docs/sources of this class to get info about how the override method should look for the version of Firebase you are using in your project.
Also, as I can see you are using IntelliJ IDEA or some similar IDE, so you can use built-in feature to implement/override the correct method.
Change your protected modifier to public and it'll go away.
In Java, the overriding method can have the same or weaker access compared to the parent method... Here the parent class declares the method as public so you have no other choice but to override it with a public access modifier as it's the weakest access possible.
I use RoboSpice-Retrofit for calling my server REST api which has been working without problems until a few days ago when every single call now throws an exception, Example:
D/Retrofit: java.lang.NoSuchMethodError: No direct method <init>(Lcom/squareup/okhttp/OkHttpClient;Lcom/squareup/okhttp/Request;ZZZLcom/squareup/okhttp/Connection;Lcom/squareup/okhttp/internal/http/RouteSelector;Lcom/squareup/okhttp/internal/http/RetryableSink;Lcom/squareup/okhttp/Response;)V in class Lcom/squareup/okhttp/internal/http/HttpEngine; or its super classes (declaration of 'com.squareup.okhttp.internal.http.HttpEngine' appears in /data/app/com.company.app.customerapp-1/base.apk)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.newHttpEngine(HttpURLConnectionImpl.java:362)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.initHttpEngine(HttpURLConnectionImpl.java:312)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:377)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy0.getTest(Unknown Source)
at com.adoperator.tidyapp.TestActivity$TestRequest.loadDataFromNetwork(TestActivity.java:67)
at com.adoperator.tidyapp.TestActivity$TestRequest.loadDataFromNetwork(TestActivity.java:54)
at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:48)
at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:150)
at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:217)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
D/Retrofit: ---- END ERROR
dependencies:
compile 'com.octo.android.robospice:robospice:1.4.14'
compile 'com.octo.android.robospice:robospice-cache:1.4.14'
compile 'com.octo.android.robospice:robospice-retrofit:1.4.14'
I suspect based on the exception that there is something wrong with the compiler, but I just tested on another computer with a fresh install of Java and Android Studio on the same project but same problems still...
This error is driving me crazy...
Anyone knows anything that could be of help? Any help is highly appreciated.
EDIT
MainActivity.java:
SpiceManager spiceManager = new SpiceManager(TestAPIService.class);
protected void onStart() {
super.onStart();
spiceManager.start(this);
spiceManager.execute(new TestRequest(), new RequestListener<ResponseData>() {
...
});
}
TestAPIService.java:
public class TestAPIService extends RetrofitGsonSpiceService {
#Override
public void onCreate() {
super.onCreate();
addRetrofitInterface(TestAPI.class);
}
#Override
protected String getServerUrl() {
return "http://192.168.0.2";
}
}
TestAPI.java:
public interface TestAPI {
#GET("/test")
ResponseData getTest();
}
TestRequest.java:
public class TestRequest extends RetrofitSpiceRequest<ResponseData, TestAPI> {
public TestRequest() {
super(ResponseData.class, TestAPI.class);
}
#Override
public ResponseData loadDataFromNetwork() throws Exception {
ResponseData response;
try {
response = getService().getTest();
}
catch (Exception e) {
e.printStackTrace();
throw e;
}
return response;
}
}
The NoSuchMethodError is happening because HttpURLConnectionImpl is trying to invoke a constructor on HttpEngine that is not defined. Now your project depends on:
com.octo.android.robospice:robospice-retrofit:1.4.14
Which depends on:
com.squareup.retrofit:retrofit:1.6.1
Which depends on both:
com.squareup.okhttp:okhttp:2.0.0
and
com.squareup.okhttp:okhttp-urlconnection:2.0.0
As of version 2.0.0, HttpURLConnectionImpl is in the okhttp-urlconnection module and HttpEngine is in the okhttp module.
The retrofit.client.UrlConnectionClient portion of your stack trace correctly matches retrofit:1.6.1, but the com.squareup.okhttp.internal.huc.HttpURLConnectionImpl portion doesn't match okhttp-urlconnection:2.0.0; it matches okhttp-urlconnection:2.4.0. The constructor that the NoSuchMethodError is complaining about is indeed defined in okhttp:2.4.0, so that means there is a different HttpEngine on your classpath, most likely from a different version of okhttp that is getting pulled in by a transitive dependency. You should be able to fix the problem by specifying the following dependencies:
compile('com.squareup.okhttp:okhttp:2.0.0') {
force = true
}
compile('com.squareup.okhttp:okhttp-urlconnection:2.0.0') {
force = true
}
If that doesn't work for whatever reason, the solution to your problem will still be to get your versions of okhttp and okhttp-urlconnection synced up and at a version that is compatible with your version of retrofit. Declare dependencies similar to above, find the jars that contain HttpURLConnectionImpl and HttpEngine, figure out how they're getting pulled in to your build, and use exclusions to get rid of problematic transitive dependencies.