Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Stormy HourlyForecastActivity crashed . RecycleView InflateException

07-21 01:11:25.258: E/AndroidRuntime(2490): FATAL EXCEPTION: main
07-21 01:11:25.258: E/AndroidRuntime(2490): Process: com.example.testrc, PID: 2490
07-21 01:11:25.258: E/AndroidRuntime(2490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testrc/com.example.testrc.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class android.support.v7.widget.RecyclerView
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.os.Looper.loop(Looper.java:136)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.app.ActivityThread.main(ActivityThread.java:5001)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at java.lang.reflect.Method.invoke(Method.java:515)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at dalvik.system.NativeStart.main(Native Method)
07-21 01:11:25.258: E/AndroidRuntime(2490): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.support.v7.widget.RecyclerView
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at com.example.testrc.MainActivity.onCreate(MainActivity.java:13)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.app.Activity.performCreate(Activity.java:5231)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-21 01:11:25.258: E/AndroidRuntime(2490):     ... 11 more
07-21 01:11:25.258: E/AndroidRuntime(2490): Caused by: java.lang.reflect.InvocationTargetException
07-21 01:11:25.258: E/AndroidRuntime(2490):     at java.lang.reflect.Constructor.constructNative(Native Method)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
07-21 01:11:25.258: E/AndroidRuntime(2490):     ... 22 more
07-21 01:11:25.258: E/AndroidRuntime(2490): Caused by: java.lang.NoClassDefFoundError: android.support.v7.recyclerview.R$styleable
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:434)
07-21 01:11:25.258: E/AndroidRuntime(2490):     at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:404)
07-21 01:11:25.258: E/AndroidRuntime(2490):     ... 25 more

1 Answer

package evich.app.stormy;

public class MainActivity extends AppCompatActivity {

public static final String TAG = MainActivity.class.getSimpleName();
public static final String URL = "https://api.forecast.io/forecast/4efd8d4613e6eead55d1abc020f528d4/10.8518,106.7210?units=si";
public static final String URL_OPTION_LANGUAGE = "&lang=it";
public static final String URL_OPTION_UNIT_SI = "&units=si";

private WeatherForecast mWeatherForecast;

@Bind(R.id.temperatureTxtView)
TextView mTemperatureTxtView;
@Bind(R.id.timeTxtView)
TextView mTimeTxtView;
@Bind(R.id.locationTxtView)
TextView mLocationTxtView;
@Bind(R.id.humidityValueTxtView)
TextView mHumidityValueTxtView;
@Bind(R.id.precipeValueTxtView)
TextView mPrecipeValueTxtView;
@Bind(R.id.summaryTxtView)
TextView mSummaryTxtView;

@Bind(R.id.iconImgView)
ImageView mIconImgView;

@Bind(R.id.refreshImgView)
ImageView mRefreshImgView;

@Bind(R.id.progressBar1)
ProgressBar mProgressBar;

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

    setUpViews();
    updateWeatherForecast();
}

private void setUpViews() {
    ButterKnife.bind(this);
}

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}

private void updateWeatherForecast() {
    if (isNetworkAvailable()) {
        /*
         * private long startTime = 0; private long endTime = 0; startTime =
         * System.currentTimeMillis(); endTime = System.currentTimeMillis();
         * Log.d(TAG, endTime - startTime + "ms");
         */

        getWeatherForecastFromNetwork();
        // usingOkHttp();
    }
}

private void getWeatherForecastFromNetwork() {
    final ObjectAnimator animator = ObjectAnimator.ofFloat(mRefreshImgView,
            "rotation", 0, 360);
    animator.setDuration(2000);
    animator.setRepeatMode(ObjectAnimator.RESTART);
    animator.setRepeatCount(ObjectAnimator.INFINITE);
    animator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation) {
            // TODO Auto-generated method stub
            super.onAnimationStart(animation);
            mRefreshImgView.setEnabled(false);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            // TODO Auto-generated method stub
            super.onAnimationEnd(animation);
            mRefreshImgView.setEnabled(true);
        }
    });
    animator.start();

    VolleySingleton volleySingleTon = VolleySingleton.getInstance(this);
    RequestQueue queue = volleySingleTon.getRequestQueue(this
            .getApplicationContext());

    JsonObjectRequest request = new JsonObjectRequest(
            com.android.volley.Request.Method.GET, URL,
            new Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    // TODO Auto-generated method stub
                    mWeatherForecast = parseWeatherForecastJSONResponse(response);

                    animator.end();
                    updateDisplay();
                }
            }, new ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub
                    animator.end();
                    alertRequestError();
                }
            });
    queue.add(request);
}

private WeatherForecast parseWeatherForecastJSONResponse(JSONObject response) {
    try {
        return WeatherForecastJsonParser.parse(response);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        Log.e(TAG, e.getMessage());
    }
    return null;
}

private void updateDisplay() {
    // TODO Auto-generated method stub
    if (mWeatherForecast != null
            && mWeatherForecast.getCurrentWeatherForecast() != null) {
        Current mCurrentWeatherForecast = mWeatherForecast
                .getCurrentWeatherForecast();

        mTemperatureTxtView.setText(mCurrentWeatherForecast
                .getTemperatureInInCelsius() + "");
        mTimeTxtView
                .setText("At " + mCurrentWeatherForecast.getTimeFormated()
                        + " it will be");
        mLocationTxtView.setText(mCurrentWeatherForecast.getTimeZone());
        mHumidityValueTxtView
                .setText(mCurrentWeatherForecast.getHumidity());
        mPrecipeValueTxtView.setText(mCurrentWeatherForecast
                .getPrecipChanceInPercentage() + "%");
        mSummaryTxtView.setText(mCurrentWeatherForecast.getSummary());
        mIconImgView.setImageResource(mCurrentWeatherForecast.getIconId());
    }
}

private void alertRequestError() {
    // TODO Auto-generated method stub
    AlertDialogFragment dialog = new AlertDialogFragment();
    dialog.show(getSupportFragmentManager(), "dialog_error");
}

private void usingOkHttp() {
    final ObjectAnimator animator = ObjectAnimator.ofFloat(mRefreshImgView,
            "rotation", 0, 360);
    animator.setDuration(1000);
    animator.setRepeatMode(ObjectAnimator.RESTART);
    animator.setRepeatCount(ObjectAnimator.INFINITE);
    animator.start();

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(URL).build();

    Call call = client.newCall(request);
    call.enqueue(new Callback() {

        @Override
        public void onResponse(final Response response) throws IOException {
            // TODO Auto-generated method stub
            if (response.isSuccessful()) {
                try {
                    mWeatherForecast = WeatherForecastJsonParser
                            .parse(response);
                    // We aren't in main thread it's still in background
                    // thread, so we can't update our views
                    // -> use runOnUiThread(Runnable) , we can update our
                    // views
                    // in here
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            animator.end();
                            updateDisplay();
                        }
                    });
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.e(TAG, e.getMessage());

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    Log.e(TAG, e.getMessage());
                }
            } else {
                Log.e(TAG, response.code() + "");
            }
        }

        @Override
        public void onFailure(Request arg0, IOException arg1) {
            // TODO Auto-generated method stub
            animator.end();
            alertRequestError();
        }
    });
}

@OnClick(R.id.refreshImgView)
public void onClick() {
    updateWeatherForecast();
}

@OnClick(R.id.dailyBtn)
public void startDailyActivity(View view) {
    if (mWeatherForecast != null
            && mWeatherForecast.getDailyWeatherForecast() != null) {
        Intent intent = new Intent(this, DailyForecastActivity.class);
        intent.putParcelableArrayListExtra("dailyWeatherForecast",
                mWeatherForecast.getDailyWeatherForecast());
        startActivity(intent);
        overridePendingTransition(0, 0);
    }
}

@OnClick(R.id.hourlyBtn)
public void startHourlyActivity(View view) {
    if (mWeatherForecast != null
            && mWeatherForecast.getHourlyWeatherForecast() != null) {
        Intent intent = new Intent(this, HourlyForecastActivity.class);
        intent.putParcelableArrayListExtra("hourlyWeatherForecast",
                mWeatherForecast.getHourlyWeatherForecast());
        startActivity(intent);
        overridePendingTransition(0, 0);
    }
}

}

I try to create a new project and still get this exception :

Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.support.v7.widget.RecyclerView Caused by: java.lang.reflect.InvocationTargetException Caused by: java.lang.NoClassDefFoundError: android.support.v7.recyclerview.R$styleable

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Hi, can you explain how the error occurs? Is it right when the app opens, or when you press something?

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

I see, have you made sure you added the recyclerview to your gradle file?

Hi Jon , when i press Hourly button to switch to HourlyActivity , this error occurs and my app crashed

public class HourlyForecastActivity extends AppCompatActivity {

private ArrayList<Hour> mHourlyWeatherForecast;

@Bind(R.id.recycleView) RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hourly_forecast);
    ButterKnife.bind(this);

    mHourlyWeatherForecast = getIntent().getParcelableArrayListExtra("hourlyWeatherForecast");

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);

    HourAdapter adapter = new HourAdapter(mHourlyWeatherForecast);
    recyclerView.setAdapter(adapter);
}

}

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Nhat,

In your xml file for your HourlyActivity... is the recyclerview called "recycleview" or "recyclerview"?

I'm using Eclipse because my androidstudio is slowly so i add recyclerview library "android-support-v7-recyclerview.jar" (C:\Program Files (x86)\Android\android-sdk\extras\android\support\v7\recyclerview\libs) to my project in eclipse to use

Here is my xml file :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="evich.app.stormy.HourlyForecastActivity" >

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycleView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</RelativeLayout>

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

It looks like the error could be that the height of your recyclerview should be "match_parent" instead of "wrap_content"

Hi Jon, i'm still getting this error ... It seem android-support-v7-recyclerview.jar has some problems

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

It's not showing in the xml posted, but is your Recyclerview contained within a RelativeLayout?

It's exactly the same in tutorial video , yes my Recyclerview contained within a RelativeLayout

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

When I did the tutorial, my activity looked like this:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hourly_forecast);
        ButterKnife.inject(this);

        Intent intent = getIntent();
        Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.HOURLY_FORECAST);
        mHours = Arrays.copyOf(parcelables, parcelables.length, Hour[].class);

        HourAdapter adapter = new HourAdapter(this, mHours);
        mRecyclerView.setAdapter(adapter);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);

        mRecyclerView.setHasFixedSize(true);
    }

Maybe try posting your HourAdapter class?

That's ok, i will try to update my library or androidsdk , google to solve the problem. Thanks for your help . I am grateful to you, have a nice day