0

I know that a lot of people put the same question, but i read all of them and i couldnt found the mistake i have.

As all the other questions, i have 2 activitys. The Main one create and intent and put a bundle with some information and the second one recieve it.

Error log

android.content.ActivityNotFoundException: Unable to find explicit activity class 
{/com.example.tkota.testintent.MapActivity};
have you declared this activity in your AndroidManifest.xml?

MainActivity:

package com.example.tkota.testintent;

import android.app.ListActivity;
import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
import android.os.Bundle;

public class MainActivity extends ListActivity{
String[] strings= {"Test 1","Test 2"};
Intent intent = new Intent(this,MapActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strings));
}

public void onListItemClick(ListView parent, View v, int position, long id){
    Bundle bundle = new Bundle();
    bundle.putString("string", strings[position]);
    intent.putExtras(bundle);
    startActivity(intent);
    }
}

MapActivity:

package com.example.tkota.testintent;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class MapActivity extends Activity{
Button actualizar;
EditText txtDireccion;
WebView myWebView;
public String direccion = new String("");
public String urlDefault = new String("http://maps.google.com/maps?q=");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    actualizar = (Button) this.findViewById(R.id.btnActualizar);
    txtDireccion = (EditText) this.findViewById(R.id.txtDireccion);
    myWebView = (WebView) this.findViewById(R.id.webView);

    Bundle bundle = this.getIntent().getExtras();
    direccion = bundle.getString("string");
    txtDireccion.setText(direccion);
    myWebView.loadUrl(urlDefault+direccion);

    }
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tkota.testintent" >
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MapActivity"
            android:label="TestIntent"/>
    </application>

</manifest>

I read these answer https://stackoverflow.com/a/9552169/2257448 and i update all Android APIs and Eclipse. The problem continues, so i create the same app on Android Studio and still no solution.

I have tried to put the full package info on the manifest like this too.

<activity
   android:name="com.example.tkota.testintent.MapActivity"
   android:label="TestIntent"/>

Any ideas?

EDIT

As Karan Mer wrote on a comment the problem is solved by creating the intent inside onListItemClick

So the code will be these:

package com.example.tkota.testintent;

import android.app.ListActivity;
import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
import android.os.Bundle;

public class MainActivity extends ListActivity{
String[] strings= {"Test 1","Test 2"};
Intent intent = new Intent(this,MapActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strings));
}

public void onListItemClick(ListView parent, View v, int position, long id){
    Intent intent = new Intent(MainActivity.this,MapActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("string", strings[position]);
    intent.putExtras(bundle);
    startActivity(intent);
    }
}
Community
  • 1
  • 1
Fernando
  • 349
  • 2
  • 6
  • 21
  • 1
    did you clean and rebuild the project? – nano_nano Mar 13 '15 at 10:54
  • 2
    try creating your intent in `listonitemclick` as below `public void onListItemClick(ListView parent, View v, int position, long id){ Bundle bundle = new Bundle(); intent = new Intent(getapplicationcontext(),MapActivity.class); bundle.putString("string", strings[position]); intent.putExtras(bundle); startActivity(intent); }` – karan Mar 13 '15 at 10:55
  • Yes, i did it on Eclipse, but didn't work and i create a new proyect only with those 2 clases on Android Studio and didn't work too. That's the strange thing – Fernando Mar 13 '15 at 10:56
  • 1
    Karan, Thanks, actually that work omg. Im feel so stupid right now. Really Thanks – Fernando Mar 13 '15 at 10:59
  • m posting you can mark it as answer. – karan Mar 13 '15 at 11:04

3 Answers3

2

try creating your intent in listonitemclick as below

public void onListItemClick(ListView parent, View v, int position, long id){ 

    Bundle bundle = new Bundle(); 
    intent = new Intent(getapplicationcontext(),MapActivity.class); 
    bundle.putString("string", strings[position]); intent.putExtras(bundle);
    startActivity(intent);
}
karan
  • 8,637
  • 3
  • 41
  • 78
1

As Karan Mer wrote on a comment the problem is solved by creating the intent inside onListItemClick

So the code will be these:

package com.example.tkota.testintent;

import android.app.ListActivity;
import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
import android.os.Bundle;

public class MainActivity extends ListActivity{
String[] strings= {"Test 1","Test 2"};
Intent intent = new Intent(this,MapActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strings));
}

public void onListItemClick(ListView parent, View v, int position, long id){
    Intent intent = new Intent(MainActivity.this,MapActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("string", strings[position]);
    intent.putExtras(bundle);
    startActivity(intent);
    }
}
Community
  • 1
  • 1
Fernando
  • 349
  • 2
  • 6
  • 21
1

register your second activity like this in manifest file, it will work

<activity
        android:name="com.example.tkota.testintent.MapActivity"
        android:label="TestIntent"/>

And this you need to call second activity like this

public void onListItemClick(ListView parent, View v, int position, long id){
    Intent intent = new Intent(MainActivity.this,MapActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("string", strings[position]);
    intent.putExtras(bundle);
    startActivity(intent);
    }
}
Srikanth K
  • 190
  • 3
  • 12
  • As i wrote on my question, I had tried the first thing you wrote. And The second one Is the correct answer of my problem, but Karan wrote it yesterday. Anyway thanks – Fernando Mar 14 '15 at 12:35