The dead hour can not understand why the studio emphasizes return new ProfessionListLoader (MainActivity.this); and does not allow to start the application.

import android.os.Bundle; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; import com.tanat.english_for_professions.R; import com.tanat.english_for_professions.loader.ProfessionListLoader; import java.util.ArrayList; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<ArrayList<String>> { private int LOADER_ID = 0; @BindView(R.id.professionList) ListView professionList; @BindView(R.id.toolbar) Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); ButterKnife.bind(this); setSupportActionBar(toolbar); loadData(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public Loader<ArrayList<String>> onCreateLoader(int id, Bundle args) { return new ProfessionListLoader(MainActivity.this); } @Override public void onLoadFinished(Loader<ArrayList<String>> loader, ArrayList<String> data) { } @Override public void onLoaderReset(Loader<ArrayList<String>> loader) { } private void loadData() { ArrayList data = null; if (data == null || data.size() == 0) { getSupportLoaderManager().initLoader(LOADER_ID, null, this); } else { createListAdapter(data); } } public void createListAdapter(ArrayList data) { ArrayAdapter<String> adapter; if (data != null) { adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); professionList.setAdapter(adapter); } } } 

Loader:

 import android.content.AsyncTaskLoader; import android.content.Context; import android.util.Log; import com.tanat.english_for_professions.R; import com.tanat.english_for_professions.utils.JsonDisperse; import org.json.JSONException; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; public class ProfessionListLoader extends AsyncTaskLoader<ArrayList<String>> { private Context context; public ProfessionListLoader(Context context) { super(context); this.context = context; } String jsonString = ""; InputStream inputStream; HttpURLConnection urlConnection = null; BufferedReader reader = null; @Override public ArrayList<String> loadInBackground() { try { URL url = new URL("url-url-url"); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); inputStream = url.openConnection().getInputStream(); StringBuffer buffer = new StringBuffer(); reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } jsonString = buffer.toString(); } catch (Exception e) { e.printStackTrace(); return null; } Log.d("loader", jsonString); JsonDisperse jsonDisperse = new JsonDisperse(); jsonDisperse.saveStaticString(jsonString); try { return jsonDisperse.getListProfession(Integer.parseInt(context.getString(R.string.language))); } catch (JSONException e) { e.printStackTrace(); return null; } } } 

If to direct forgive context, but I also give it. enter image description here Help me please.

  • one
    Well, what does it say, if you hover with the mouse, what correction options does it offer, if you click on the red light bulb on the left? Now it ’s fashionable to switch to live data , Loaders in disgrace, it suddenly turned out that they are not as good as they had been assured at the beginning. - pavlofff

1 answer 1

In general, the problem was solved by replacing the line:

 public class ProfessionListLoader extends AsyncTaskLoader<ArrayList<String>> { 

on:

 public class ProfessionListLoader extends android.support.v4.content.AsyncTaskLoader<ArrayList<String>>{ 

I still do not understand why.