M
M
Mr Mojito2018-10-20 15:26:14
Java
Mr Mojito, 2018-10-20 15:26:14

How to launch a new activity with a click on a ListView item?

there is a code
package com.kaleidosstudio.listview_load_data_from_json;

import java.util.ArrayList;

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

import com.example.listview_load_data_from_json.R;
import com.kaleidosstudio.listview_load_data_from_json.Download_data.download_complete;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity implements download_complete {

  public ListView list;
    public ArrayList<Articles> countries = new ArrayList<Articles>();
    public ListAdapter adapter;

  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    list = (ListView) findViewById(R.id.list);			
    adapter = new ListAdapter(this);
    list.setAdapter(adapter);
    
    Download_data download_data = new Download_data((download_complete) this);
    download_data.download_data_from_link("http://jsonplaceholder.typicode.com/posts");

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View itemClicked, int position,
                  long id) {

                Intent intent = new Intent(MainActivity.this, Article.class);
                startActivity(intent);

//				TextView textView = (TextView) itemClicked.findViewById(R.id.name);
//				String strText = textView.getText().toString(); // получаем текст нажатого элемента
//
//				if(strText.equalsIgnoreCase(getResources().getString(R.string.app_name))) {
//					// Запускаем активность, связанную с определенным именем кота
//					startActivity(new Intent(MainActivity.this, Article.class));
//				}
      }
    });}
    

  


  public void get_data(String data)
  {
    
    try {
      JSONArray data_array=new JSONArray(data);
      
      for (int i = 0 ; i < data_array.length() ; i++)
      {
        JSONObject obj=new JSONObject(data_array.get(i).toString());

        Articles add=new Articles();
        add.title = obj.getString("title");
        //add.code = obj.getString("id");
        
        countries.add(add);

      }

      adapter.notifyDataSetChanged();
      
    } catch (JSONException e) {
      e.printStackTrace();
    }

  }
  

}


After clicking on an element from the ListView in the application emulator, an error pops up
5bcb1e900fef8047341444.jpeg
. As soon as I did not twist the code to switch the second activity, this error always pops up.
Why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mr. Mojito, 2018-10-21
@replicate

java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.example.listview_load_data_from_json/com.kaleidosstudio.listview_load_data_from_json.Article}: java.lang.IllegalStateException: 
You need to use a Theme.AppCompat theme (or descendant) with this activity.

Thanks to everyone, it turns out there was an elementary thing, the theme for the activity was not written in the manifest.
Added to AndroidManifest.xml:
<activity android:name="com.kaleidosstudio.listview_load_data_from_json.Article"
            android:theme ="@style/Theme.AppCompat">

and everything worked

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question