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

Why my app is crashing on device?

My app is crashing on device but not on emulator.

I`m a beginner in Android and this is my first try...

package ro.intuitext.manuale.manualeintuitext.ui;

import android.content.Context; import android.content.Intent; import android.graphics.Typeface; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.webkit.WebView; import android.widget.Button; import android.widget.Toast;

import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import ro.intuitext.manuale.manualeintuitext.R; import ro.intuitext.manuale.manualeintuitext.contents.Book; import ro.intuitext.manuale.manualeintuitext.contents.Unit;

public class MainActivity extends AppCompatActivity {

public static final String TAG = MainActivity.class.getSimpleName();
public static final String CONTENTS_LIST = "CONTENTS_LIST";

private Book mBook;

@BindView(R.id.webViewHtml) WebView mWebView;
@BindView(R.id.homeButton) Button mHomeButton;
@BindView(R.id.contentsButton) Button mContentsButton;
@BindView(R.id.amiiContentsButton) Button mAmiiContentsButton;

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

    Typeface font = Typeface.createFromAsset( getAssets(), "fonts/fontawesome.ttf" );
    mContentsButton.setTypeface(font);
    mAmiiContentsButton.setTypeface(font);
    mHomeButton.setTypeface(font);

    getJSONData();
}

private void getJSONData() {
    String apiKey = "apikeyhere";
    String apiURL = "http://serverhere/api/v1/contents/" + apiKey;

    if (isNetworkAvailable()) {

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

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //do nothing for the moment
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String jsonData = response.body().string();

                    Log.v(TAG, jsonData);

                    if(response.isSuccessful()) {
                        mBook = parseBookDetails(jsonData);
                    }
                } catch (IOException | JSONException e) {
                    Log.e(TAG, "Exception caught: ", e);
                }
            }
        });


    }
    else {
        Toast.makeText(this, getString(R.string.network_unavailable_message), Toast.LENGTH_LONG).show();
    }
}

private Book parseBookDetails(String jsonData) throws JSONException {
    Book book = new Book();

    book.setUnits(getUnitsBook(jsonData));

    return book;
}

private Unit[] getUnitsBook(String jsonData) throws JSONException {

    JSONArray book = new JSONArray(jsonData);

    Unit[] units = new Unit[book.length()];

    for (int i = 0; i < book.length(); i++) {
        JSONObject jsonUnit = book.getJSONObject(i);
        Unit unit = new Unit();

        unit.setTitle(jsonUnit.getString("title"));

        units[i] = unit;
    }

    return units;
}

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

    return isAvailable;
}

@OnClick(R.id.contentsButton)
public void startContentsActivity(View view) {

    Intent intent = new Intent(this, ContentsListActivity.class);

    intent.putExtra(CONTENTS_LIST, mBook.getUnits());
    startActivity(intent);
}

}

When i debug it crash at "intent.putExtra(CONTENTS_LIST, mBook.getUnits());" If i pass 1 as a second parameter it works on mobile devices so i'm thinking is something worng there in that like but i don't know what because on emulator works fine it open a new view with a custom list generated.

I followed the course Build a weather app and Android Lists and Adapters and some others...

Please help me. Thank you.

PS: Is not working on Huawei/MotoG3 phones it seems ... on a Samsung S6 is working fine.

I find out what the problem was :)) ... my Api host in on an internal work network so i`m not receiving any information ...