【发布时间】:2015-05-15 19:47:24
【问题描述】:
我知道很多人都提出了同样的问题,但我读了所有的问题,我找不到我的错误。
与所有其他问题一样,我有 2 项活动。主要的一个创建和意图并放置一个包含一些信息的捆绑包,第二个接收它。
错误日志
android.content.ActivityNotFoundException: Unable to find explicit activity class
{/com.example.tkota.testintent.MapActivity};
have you declared this activity in your AndroidManifest.xml?
主活动:
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);
}
}
地图活动:
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);
}
}
清单:
<?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>
我阅读了这些答案https://stackoverflow.com/a/9552169/2257448 并更新了所有 Android API 和 Eclipse。问题仍然存在,所以我在 Android Studio 上创建了相同的应用程序,但仍然没有解决方案。
我也尝试将完整的包裹信息放在清单上。
<activity
android:name="com.example.tkota.testintent.MapActivity"
android:label="TestIntent"/>
有什么想法吗?
编辑
正如Karan Mer 在评论中所写,通过在 onListItemClick
中创建意图来解决问题所以代码将是这些:
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);
}
}
【问题讨论】:
-
你清理并重建了项目吗?
-
尝试在
listonitemclick中创建您的意图,如下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); } -
是的,我在 Eclipse 上做了,但是没有用,我只在 Android Studio 上用这两个类创建了一个新的项目,也没有用。这就是奇怪的事情
-
Karan,谢谢,实际上,天哪。我现在觉得自己好傻。真的谢谢
-
m 发帖可以标记为答案。
标签: java android eclipse android-intent android-studio