Angelo Orru Angelo Orru - 4 months ago 26
Android Question

Error: Start an intent which calls an Async task from another class in Android

I am developing a project in Android that request some data from a servlet. I have tried to run the LivingRoomActivity class in its own on Android and it does display the data correctly, but when I start the activity from an Intent it fails to start the async task.
The Async task class is as follows:

import android.app.Activity;
import android.app.ProgressDialog;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Start async task ...
new DownloadFilesTask().execute();
}

/*public void onClick() {
//Start async task ...
new DownloadFilesTask().execute();
}*/

private class DownloadFilesTask extends AsyncTask<String, Void, String> {
ProgressDialog dialog;
TextView textView = (TextView) findViewById(R.id.textView);
String display;

@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting to remote server");
dialog.show();
dialog.setCancelable(false);
}

@Override
protected String doInBackground(String... params) {
String url = "http://192.168.1.4:8080/PhidgetSensorServer/getLivingRoomLuminosityToAndroid";
try {
URL urlRequest = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlRequest.openConnection();
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader br = new BufferedReader(isr);

String response = "";
String line;


while ((line = br.readLine()) != null) {
response = response + line;
display = response;
Log.d("Data: ", response);
}
} catch (ParseException | IOException e) {
e.printStackTrace();
}
return display;
}`enter code here`

protected void onPostExecute(String display) {
//update the text view with the data....
textView.setText(display);

dialog.cancel();

if (!display.contains("")) {
//If unable to fetch data.... return the following msg:
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
}
}


And this is my Home activity: MainActivity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

public void onClickLivingRoom(View v) {
//When button clicked open the activity for searching a restaurants
Intent intent = new Intent(".LivingRoomActivity");
startActivity(intent);
}
}


Now, when I run the app, the compiler hung at the dialog "Connecting to remote server" and throws the following exception:

07-21 19:58:39.821 12401-12401/com.student.mmu.masterprojapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.student.mmu.masterprojapp, PID: 12401
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4756) 
at android.view.View$PerformClick.run(View.java:19749) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=.LivingRoomActivity }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
at android.app.Activity.startActivityForResult(Activity.java:3736)
at android.app.Activity.startActivityForResult(Activity.java:3697)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
at android.app.Activity.startActivity(Activity.java:4007)
at android.app.Activity.startActivity(Activity.java:3975)
at com.student.mmu.masterprojapp.MainActivity.onClickLivingRoom(MainActivity.java:19)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4756) 
at android.view.View$PerformClick.run(View.java:19749) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

To resume:
When pushing the button in MainActivity the class LivingRoomActivity is called, the async task executed and the result displayed in a textView related to the LivingRoomClass. Thanks.

Answer

One of the lines in the exception message is probably the key to your answer

Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=.LivingRoomActivity }

You have probably not defined the LivingRoomActivity class in your manifest file. Please add an activity tag under the application tag in the manifest. Something like this -

<activity android:name=".your.package.name.LivingRoomActivity" />

Every activity that you have in your application should be mentioned in the AndroidManifest.xml file.

Hope this helps :)