0

I'm trying to learn Retrofit 2 with this basic example. And I got a problem while trying to pass serializable data from activity to activity...

Have you got any idea why?

This is my code.

logcat:

05-24 00:21:46.215 18156-18156/com.krystian.weatherapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.krystian.weatherapp, PID: 18156
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.krystian.weatherapp/com.krystian.flowerapp.ui.DetailActivity}: android.content.res.Resources$NotFoundException: String resource ID
#0x2
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
   at android.app.ActivityThread.access$800(ActivityThread.java:156)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:211)
   at android.app.ActivityThread.main(ActivityThread.java:5373)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
   at android.content.res.Resources.getText(Resources.java:340)
   at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
   at android.widget.TextView.setText(TextView.java:4171)
   at com.krystian.flowerapp.ui.DetailActivity.setData(DetailActivity.java:52)
   at com.krystian.flowerapp.ui.DetailActivity.onCreate(DetailActivity.java:47)
   at android.app.Activity.performCreate(Activity.java:5990)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
   at android.app.ActivityThread.access$800(ActivityThread.java:156) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:211) 
   at android.app.ActivityThread.main(ActivityThread.java:5373) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

MainActivity.java:

package com.krystian.flowerapp.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.krystian.flowerapp.R;
import com.krystian.flowerapp.adapter.FlowerAdapter;
import com.krystian.flowerapp.controller.RestManager;
import com.krystian.flowerapp.helper.Constants;
import com.krystian.flowerapp.model.Flower;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity implements FlowerAdapter.FlowerClickListener {
@BindView(R.id.recyclerView)
RecyclerView recyclerView;

private RestManager mRestManager;
private FlowerAdapter mFlowerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.bind(this);

    configViews();

    mRestManager = new RestManager();
    Call<List<Flower>> listCall = mRestManager.getFlowerService().getAllFlowers();
    listCall.enqueue(new Callback<List<Flower>>() {
        @Override
        public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) {
            if (response.isSuccessful()) {
                List<Flower> flowerList = response.body();

                for (int i=0; i < flowerList.size(); i++) {
                    Flower flower = flowerList.get(i);
                    mFlowerAdapter.addFlower(flower);
                }
            } else {
                int ac = response.code();
                Log.e("Response code", String.valueOf(ac));
            }
        }

        @Override
        public void onFailure(Call<List<Flower>> call, Throwable t) {

        }
    });
}

private void configViews() {
    recyclerView.setHasFixedSize(true);
    recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));

    mFlowerAdapter = new FlowerAdapter(this);

    recyclerView.setAdapter(mFlowerAdapter);
}

@Override
public void onClick(int position) {
    Flower selectedFlower = mFlowerAdapter.getSelectedFlower(position);
    Intent intent = new Intent(MainActivity.this, DetailActivity.class);
    intent.putExtra(Constants.REFERENCE.FLOWER, selectedFlower);
    startActivity(intent);
}
}

DetailActivity.java:

package com.krystian.flowerapp.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.krystian.flowerapp.R;
import com.krystian.flowerapp.helper.Constants;
import com.krystian.flowerapp.model.Flower;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by: Krystian
 * Date: 23.05.2016.
 */

public class DetailActivity extends AppCompatActivity {
@BindView(R.id.photoImageView)
ImageView photoImageView;
@BindView(R.id.nameTextView)
TextView nameTextView;
@BindView(R.id.idTextView)
TextView idTextView;
@BindView(R.id.categoryTextView)
TextView categoryTextView;
@BindView(R.id.priceTextView)
TextView priceTextView;
@BindView(R.id.instructionsTextView)
TextView instructionsTextView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    ButterKnife.bind(this);

    Intent intent = getIntent();
    Flower flower = (Flower) intent.getSerializableExtra(Constants.REFERENCE.FLOWER);

    setData(flower);
}

private void setData(Flower flower) {
    nameTextView.setText(flower.getName());
    idTextView.setText(flower.getProductId());
    categoryTextView.setText(flower.getCategory());
    priceTextView.setText(String.valueOf(flower.getPrice()));
    instructionsTextView.setText(flower.getInstructions());

    Picasso
            .with(getApplicationContext())
            .load(Constants.HTTP.PHOTO_URL + flower.getPhoto())
            .into(photoImageView);
}
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.krystian.flowerapp">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name="com.krystian.flowerapp.ui.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.krystian.flowerapp.ui.DetailActivity" />
</application>

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
y07k2
  • 1,898
  • 4
  • 20
  • 36
  • 1
    What is on line 52 in DetailActivity? `idTextView.setText(flower.getProductId());` ? This looks suspect. Show us your layout xml for DetailActivity. – t0mm13b May 23 '16 at 22:39
  • @t0mm13b You are right! But why this method (which returns `int`) doesn't show any underline when I forget `String.valueOf()`? `getPrice()` method did it... – y07k2 May 23 '16 at 22:43
  • Have made an answer to explain why. – t0mm13b May 23 '16 at 22:58

1 Answers1

1

The line that is causing the issue is on line 52 in DetailActivity:

idTextView.setText(flower.getProductId());

The declaration of setText is:

void setText (int resid)

The clue is in the parameter name, it is a resource ID, when flower.getProductId() is passed in to the view's setText method, it was expecting a string resource in the res/strings.xml or an identifier indicating a string resource which is an int. And the confusion occurred between a resource ID and actual getter/method getProductId() of the flower class.

The easiest way to resolve it is to use the valueOf method of the String class, which will convert the Product ID of flower class to a string type.

idTextView.setText(String.valueOf(flower.getProductId()));
t0mm13b
  • 34,087
  • 8
  • 78
  • 110