If I try requesting something from HTTP OnClick, it takes too long to complete the request and so I get a MainThread error that it’s taking too long. If I try to place it on a back thread, the contents aren’t where i need them to be by the time I need to use them.
My code is capable of parsing a JSON string, but the problem I have is getting the string from the server takes forever and doesn't get there by the time I need to parse.
It only works for me when I use the debugging tool, place a breakpoint before the request and after. Then, the string is there quick enough to parse.
FYI: I tried client.execute() and that resulted in a main thread issue which is why I now used client.enqueue() but then i don't get the data on time for the parsing.
Any help is appreciated, thank you!
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.weather_app, PID: 2774
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
**at com.example.weather_app.MainActivity.button1Pressed(MainActivity.java:91)**
My Button implementation: (error at: "** .... **")
public void button1Pressed(View view) {
Log.i("event", "Button 1 Pressed");
final TextView temperature = (TextView) this.findViewById(R.id.Temperature);
final TextView humidity = (TextView) this.findViewById(R.id.Humidity);
final TextView wind_speed = (TextView) this.findViewById(R.id.Wind_Speed);
final TextView precipitation = (TextView) this.findViewById(R.id.Precipitation);
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if(location != null) {
Log.i("location", location.toString());
String url = "https://api.darksky.net/forecast/6e5fd8e33301bec5482dd3656d16cb32/" + location.getLatitude() + "," + location.getLongitude();
getAndParseData(url);
}
}
});
**temperature.setText(String.valueOf(weatherDataList.get(0).getHourly().getSummary()));**
humidity.setText(String.valueOf(weatherDataList.get(0).getCurrently().getHumidity()));
wind_speed.setText(String.valueOf(weatherDataList.get(0).getCurrently().getWindSpeed()));
precipitation.setText(String.valueOf(weatherDataList.get(0).getCurrently().getPrecipIntensity()));
}
Get and Parse Data from HTTP:
void getAndParseData(String url){
Request request = new Request.Builder()
.url(url)
.build();
OkHttpClient client = new OkHttpClient();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
data = "[" + response.body().string() + "]";
parseData(data);
}
}
});
}
void parseData(String weatherData){
try {
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
weatherDataList = mapper.readValue(weatherData, new TypeReference<List<WeatherData>>(){
});
} catch (IOException | NullPointerException ignore){}
}
A snippet of my Activity_Main.xml if needed:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="#string/button1"
android:textSize="20sp"
android:onClick="button1Pressed" />
Related
I am creating an activity that allows you to send a reset link to the mail in the edittext only when the content of the editext is not a null string. But my control isn't working. This is the complete code:
FirebaseAuth fAuth;
private String email;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resetpsw);
fAuth = FirebaseAuth.getInstance();
Button btn_rst = findViewById(R.id.btn_resetpasswr);
EditText emailtxt = findViewById(R.id.txt_emres);
btn_rst.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
email = emailtxt.getText().toString();
if(email != "") {
fAuth.sendPasswordResetEmail(email);
Toast.makeText(getApplicationContext(), "Email inviata!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(resetpsw.this, accesso.class));
}
}
});
}
}
Practically in any case, even when the string is empty (= "") the condition is true and therefore I have this error:
2021-06-02 16:26:55.015 28645-28645/com.conta.pophome
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.conta.pophome, PID: 28645
java.lang.IllegalArgumentException: Given String is empty or null
at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(com.google.android.gms:play-services-basement##17.1.0:5)
at com.google.firebase.auth.FirebaseAuth.sendPasswordResetEmail(com.google.firebase:firebase-auth##21.0.1:1)
at com.conta.pophome.resetpsw$1.onClick(resetpsw.java:37)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I honestly don't know why the condition is true even when the string is an empty string. Could someone help me? Thank you
Use isEmpty() method.
email = emailtxt.getText().toString();
if(email.isEmpty())
return;
fAuth.sendPasswordResetEmail(email);
Toast.makeText(getApplicationContext(), "Email inviata!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(resetpsw.this, accesso.class));
so I have an EditText field,and when the user tap a number,its start counting down.
The problem starts when the are not entering a number the app crash.
this is what I tried to do:
Thanks for the help.
public class MainActivity extends AppCompatActivity {
EditText mEnterNumber;
TextView mTest;
int timerIsRunning = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void StartRemainder(View view){
mTest = findViewById(R.id.mTest);
mEnterNumber = findViewById(R.id.mEnterNumber);
int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
if(userNumb < 0 || mEnterNumber.toString().isEmpty()){
Toast.makeText(MainActivity.this, "Please enter correct number", Toast.LENGTH_SHORT).show();
}
else{
CountDownTimer userTimer = new CountDownTimer(userNumb,1000) {
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
//Convert milliseconds into hour,minute and seconds
String hms = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
mTest.setVisibility(View.VISIBLE);
mTest.setText(hms);
timerIsRunning = 1;
}
#Override
public void onFinish() {
}
}.start();
}
}
----------------------This is the error I get when the app crash:--------------------------------
2020-09-04 04:09:15.024 27096-27096/com.example.drinkme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.drinkme, PID: 27096
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:620)
at java.lang.Integer.parseInt(Integer.java:643)
at com.example.drinkme.MainActivity.StartRemainder(MainActivity.java:40)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
line of error:
int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
I'm guessing the issue is with this line:
int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
And it is because, an empty string("") cannot be converted to a number unlike strings like "1" or "2"
To fix this:
We Provide a try-catch block for the NumberFormatException
int userNumb = 0;
try {
if (mEnterNumber != null && !TextUtils.isEmpty(mEnterNumber.getText().toString())) {
userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
}
}
catch (NumberFormatException ex) {}
You're not getting the text from the edittext before checking weather it's empty or not. Try this :
if(userNumb < 0 && mEnterNumber.`getText()`.toString().isEmpty()){
Toast.makeText(MainActivity.this, "Please enter correct number",
Toast.LENGTH_SHORT).show();
int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
}
As you can see I've put the integer conversion in the if block as it might also be the reason for it.
Like Felix Favour said it's because you're trying to parse a non-numeric item into a string. This line:
mEnterNumber = findViewById(R.id.mEnterNumber);
Will most likely resolve to Null if nothing is entered. You can try setting it to a numeric value:
mEnterNumber = findViewById(R.id.mEnterNumber);
if mEnterNumber.getText().toString().equals("")
{
int userNumb = 0; // or whatever value you want to set as a starting default
}
else
{
int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
}
This invariably could need more improvement for checking or preventing other non-numeric entries ofcourse.
Alternatively, you can also set it in the XML like this:
<EditText
android:id="#+id/mEnterNumber"
...
android:text="0" />
I use something like this -
if (mEnterNumber.getText().toString().matches("")){
Toast.makeText(this, "Please Enter any number", Toast.LENGTH_SHORT).show();
return;
}
I hope this will prevent the crash.
Im writing my project app in Android Studio. The app is connected to microcontroller and send logic 1 on button Click to get data string from microcontroller all by BT. I have question for you how can i automatize it to send this logic 1 with some delay. I tried some methods but them dont work properly. The app stops
//Send Button
sendButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
sendData();
}
catch (IOException ex) { }
}
});
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
sendButton.performClick();
}
},20000);
LogCat:
2020-03-27 18:44:10.913 11754-11754/com.example.bt_serial_test_1_0 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bt_serial_test_1_0, PID: 11754
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5645)
at android.view.View.performClick(View.java:6608)
at com.example.bt_serial_test_1_0.MainActivity$4.run(MainActivity.java:104)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:6878)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5640)
at android.view.View.performClick(View.java:6608)
at com.example.bt_serial_test_1_0.MainActivity$4.run(MainActivity.java:104)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:6878)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
Caused by: java.lang.IllegalArgumentException: Invalid message body
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:337)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:325)
at com.example.bt_serial_test_1_0.MainActivity.sendSms(MainActivity.java:239)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5640)
at android.view.View.performClick(View.java:6608)
at com.example.bt_serial_test_1_0.MainActivity$4.run(MainActivity.java:104)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:6878)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
this method is in onCreate. Can someone please help me to resolve this problem
Here is a Handler example for a repeating task.
// Create the Handler object (on the main thread by default)
Handler handler = new Handler();
// Define the code block to be executed
private Runnable runnableCode = new Runnable() {
#Override
public void run() {
// Do something here on the main thread
Log.d("Handlers", "Called on main thread");
// Repeat this the same runnable code block again another 2 seconds
handler.postDelayed(runnableCode, 2000);
}
};
// Start the initial runnable task by posting through the handler
handler.post(runnableCode);
You can modify it for your case like that
sendButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
handler().postDelayed(r,1000);
}
catch (IOException ex) { }
}
});
final Runnable r = new Runnable()
{
public void run()
{
sendButton.performClick();
}
};
Hi I am trying to parse JSON using Retrofit, save it on Sqlite and display on RecyclerView. However my app crashes when I try to open the activity.
Below is my full codes of related activity. Could you please help me with resolving the issue?
Thank you
public class InventoryProductActivity extends AppCompatActivity implements InventoryProductListAdapter.CustomClickListener {
private static final String TAG = InventoryProductActivity.class.getSimpleName();
private InventoryProductListAdapter mInventoryProductListAdapter;
private RecyclerView mRecyclerView;
private RetrofitClient mRetrofitClient;
LinearLayoutManager mLinearLayoutManager;
private WarehouseDatabase mDatabase;
private ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory_product);
configViews();
mRetrofitClient = new RetrofitClient();
mDatabase = new WarehouseDatabase(this);
loadInventoryProductFeed();
}
private void configViews() {
mRecyclerView = findViewById(R.id.recycler_view_inventory_product);
mRecyclerView.setHasFixedSize(true);
mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mInventoryProductListAdapter = new InventoryProductListAdapter(this);
mRecyclerView.setAdapter(mInventoryProductListAdapter);
}
private void loadInventoryProductFeed() {
mProgressDialog = new ProgressDialog(InventoryProductActivity.this);
mProgressDialog.setMessage("Loading Inventory Data...");
mProgressDialog.setCancelable(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setIndeterminate(true);
mProgressDialog.show();
mInventoryProductListAdapter.reset();
if (getNetworkAvailability()) {
getFeed();
} else {
getFeedFromDatabase();
}
}
private void getFeed() {
Call<List<InventoryProductModel>> listCall = mRetrofitClient.getWarehouseServiceInventoryProduct().getAllInventoryProducts();
listCall.enqueue(new Callback<List<InventoryProductModel>>() {
#Override
public void onResponse(Call<List<InventoryProductModel>> call, Response<List<InventoryProductModel>> response) {
if (response.isSuccessful()) {
List<InventoryProductModel> inventoryProductModelList = response.body();
for (int i = 0; i < inventoryProductModelList.size(); i++) {
InventoryProductModel inventoryProductModel = inventoryProductModelList.get(i);
mInventoryProductListAdapter.notifyDataSetChanged();
}
} else {
int sc = response.code();
switch (sc) {
}
}
mProgressDialog.dismiss();
}
#Override
public void onFailure(Call<List<InventoryProductModel>> call, Throwable t) {
mProgressDialog.dismiss();
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void getFeedFromDatabase() {
List<InventoryProductModel> inventoryProductModelList = mDatabase.getInventoryProducts();
for (int i = 0; i < inventoryProductModelList.size(); i++) {
InventoryProductModel inventoryProductModel = inventoryProductModelList.get(i);
Log.d(TAG, inventoryProductModel.getName() + "||" + inventoryProductModel.getCountryId());
}
mProgressDialog.dismiss();
}
private boolean getNetworkAvailability() {
return Utils.isNetworkAvailable(getApplicationContext());
}
#Override
public void onClick(int position) {
}
}
E/WindowManager: android.view.WindowLeaked: Activity
codes.bala.bmsfinal1.activity.MainActivity has leaked window
DecorView#52c3fcd[] that was originally added here
at android.view.ViewRootImpl.(ViewRootImpl.java:418)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:331)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:322)
at android.app.ProgressDialog.show(ProgressDialog.java:116)
at android.app.ProgressDialog.show(ProgressDialog.java:104)
at codes.bala.bmsfinal1.activity.MainActivity.login(MainActivity.java:61)
at codes.bala.bmsfinal1.activity.MainActivity$1.onClick(MainActivity.java:41)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I/ViewConfigCompat: Could not find method getScaledScrollFactor() on
ViewConfiguration D/AndroidRuntime: Shutting down VM
--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main
Process: codes.bala.bmsfinal1, PID: 12077
java.lang.RuntimeException: Unable to start activity ComponentInfo{codes.bala.bmsfinal1/codes.bala.bmsfinal1.activity.InventoryProductActivity}:
java.lang.NullPointerException: Attempt to invoke interface method
'retrofit2.Call
codes.bala.bmsfinal1.iinterface.WarehouseService.getAllInventoryProducts()'
on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke interface
method 'retrofit2.Call
codes.bala.bmsfinal1.iinterface.WarehouseService.getAllInventoryProducts()'
on a null object reference
at codes.bala.bmsfinal1.activity.InventoryProductActivity.getFeed(InventoryProductActivity.java:88)
at codes.bala.bmsfinal1.activity.InventoryProductActivity.loadInventoryProductFeed(InventoryProductActivity.java:70)
at codes.bala.bmsfinal1.activity.InventoryProductActivity.onCreate(InventoryProductActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Application terminated.
getWarehouseServiceInventoryProduct()
method return null.
You should set breakpoint inside this method and check what happening (or add some logs to this method).
I know this question has been asked here and answered but for some reasons, provided solutions are not working for me.
So here is how I'm trying to do this. I have two buttons in my layout one to open gallery and other to upload the images.
Defined members
int SELECT_PICTURES = 1;
ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
Uri imageUri;
int up = 0;
int k =0;
Gallery button code
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURES);
On Activity result code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SELECT_PICTURES) {
if (resultCode == MainActivity.RESULT_OK) {
if (data.getClipData() != null) {
int count = data.getClipData().getItemCount();
Log.i("count", String.valueOf(count));
int currentItem = 0;
while (currentItem < count) {
imageUri = data.getClipData().getItemAt(currentItem).getUri();
Log.i("uri", imageUri.toString());
mArrayUri.add(imageUri);
currentItem = currentItem + 1;
}
Log.i("listsize", String.valueOf(mArrayUri.size()));
} else if (data.getData() != null) {
String imagePath = data.getData().getPath();
}
}
}
}
Upload button code
StorageReference filepath = FirebaseStorage.getInstance().getReference().child("gpic");
while (up < mArrayUri.size()){
// error is pointing to this line, line 82
filepath.child(mArrayUri.get(k).getLastPathSegment()).putFile(mArrayUri.get(k)).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadURL = taskSnapshot.getDownloadUrl();
Toast.makeText(TestingActivity.this, downloadURL.toString(), Toast.LENGTH_SHORT).show();
up++;
k++;
}
});
}
}
The above code uploads one image and then crashes with the following error
01-24 12:37:37.416 8336-8336/com.codenemesis.uploading E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.codenemesis.uploading, PID: 8336
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.util.concurrent.RejectedExecutionException: Task com.google.firebase.storage.zzs#c59b7fe rejected from java.util.concurrent.ThreadPoolExecutor#4b6eb5f[Running, pool size = 2, active threads = 2, queued tasks = 128, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2049)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:814)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1360)
at com.google.firebase.storage.zzu.zzt(Unknown Source)
at com.google.firebase.storage.UploadTask.schedule(Unknown Source)
at com.google.firebase.storage.StorageTask.zzcls(Unknown Source)
at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
at **com.codenemesis.uploading.TestingActivity.up(TestingActivity.java:82)**
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Found what was causing the problem. I was incrementing the variables "up" and "k" within the onSuccessListener. That might be changing their values before uploading the URI, so I incremented them outside the onSuccessListener and it worked perfectly fine.
Upload button code
StorageReference filepath = FirebaseStorage.getInstance().getReference().child("gpic");
while (up < mArrayUri.size()){
filepath.child(mArrayUri.get(k).getLastPathSegment()).putFile(mArrayUri.get(k)).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadURL = taskSnapshot.getDownloadUrl();
Toast.makeText(TestingActivity.this, downloadURL.toString(), Toast.LENGTH_SHORT).show();
}
});
up++;
k++;
}
}
Completable futures is much better approach for this problem.
Below upload method returns a completable future.
fun uploadImage(
path: String, imageUrl: Uri): CompletableFuture<String> {
val productImagesRef = storage.reference.child("$path/${imageUrl.lastPathSegment}")
val uploadTask = productImagesRef.putFile(imageUrl)
val promise = CompletableFuture<String>()
uploadTask.addOnFailureListener {
promise.completeExceptionally(it)
}.addOnSuccessListener {
productImagesRef.downloadUrl.addOnSuccessListener {
promise.complete(it.toString())
}.addOnFailureListener {
promise.completeExceptionally(it)
}
}
return promise
}
The caller can wait on multiple upload completable futures as below. Further, in android it would be ideal for the caller to spawn a new thread for multiple uploads to not block the UI main thread.
Thread {
val imageUploadPromises = imageUris.map {
uploadImage(product.group, product.type, product.name, it)
}
// wait for all uploads to finish. join() makes it wait.
CompletableFuture.allOf(*imageUploadPromises.toTypedArray()).join()
//To get individual downloadUrls
val downloadUrls = imageUploadPromises.map { it.get() }
}.start()