My source code is about making a list with titles by crawling. clicking a title, as a click event, move to another page for showing the content about the title. But the click event(changing page) doesn't work and I can't find the reason. I'm new in android programming hope that I can get some advice..!
*Main_Activity.java
package com.example.listview_prac;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
public class MainActivity extends AppCompatActivity {
ListView listView;
String title; //title
//Vector<String> title_str = new Vector<String>(15);
Vector<String> content_arr = new Vector<String>(15);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, new ArrayList<String>()));
new MyTask().execute();
}
class MyTask extends AsyncTask<Void, String, String>
{
ArrayAdapter<String> adapter;
@Override
protected void onPreExecute() {
adapter = (ArrayAdapter<String>)listView.getAdapter();
}
@Override
protected String doInBackground(Void... voids) {
final StringBuilder builder = new StringBuilder();
String url1 = "https://";
String my_link = null;
try {
Connection con = Jsoup.connect(url1);
Document doc = con.get();
Elements mElementDatas = doc.select("#body_line > nobr ");
String text = mElementDatas.text();
int i=0;
for (Element elem : mElementDatas) {
String my_title = elem.select("a span b").text();
title = my_title;
publishProgress(title);
my_link = elem.select("a").attr("href");
String url2 = my_link;
Connection con2 = Jsoup.connect("https://" + url2);
Document doc2 = con2.get();
Elements EDatas = doc2.select("#articles");
for (Element elem2 : EDatas) {
// span
String content1 = elem2.select("span").text();
String content2 = elem2.select("p").text();
String content = content1 + content2;
content_arr.add(content1 + content2); //Is this wrong?
}
i++;
if(i>15) {
break;
}
}
} catch (IOException e) {
builder.append("Error");
}
return "All the datas were added successfully";
}
@Override
protected void onProgressUpdate(String... values) {
adapter.add(values[0]);
}
@Override
protected void onPostExecute(String result) {
//content_arr.add(result);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked! ", Toast.LENGTH_SHORT).show();
//String c_list = content_arr.get(position);
Intent intent = new Intent(MainActivity.this, Clicked.class);
intent.putExtra("arr_text", content_arr.get(position));
startActivity(intent);
}
});
}
}
}
*Clicked.java
package com.example.listview_prac;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Clicked extends Activity {
TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clicked);
textView = (TextView)findViewById(R.id.textView);
Bundle bundle = getIntent().getExtras();
String text = bundle.getString("arr_text");
textView.setText(text);
}
}
and these are the error messages
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.listview_prac, PID: 6750
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.listview_prac/com.example.listview_prac.Clicked}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2065)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
at android.app.Activity.startActivityForResult(Activity.java:5320)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:5278)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at android.app.Activity.startActivity(Activity.java:5664)
at android.app.Activity.startActivity(Activity.java:5617)
at com.example.listview_prac.MainActivity$MyTask$1.onItemClick(MainActivity.java:118)
at android.widget.AdapterView.performItemClick(AdapterView.java:330)
at android.widget.AbsListView.performItemClick(AbsListView.java:1187)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3179)
at android.widget.AbsListView$3.run(AbsListView.java:4097)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I deleted the url of Main_Acitivity.java but if it's necessary for you, I can put it back
have you declared this activity in your AndroidManifest.xml?
Clicked Activity Declare in AndroidManifest file.