简体   繁体   中英

Android - Unable to find explicit activity class

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);
    }
}

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);
}

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);
    }
}

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM