我刚开始使用Android开发,我坚持使用此代码:
public class Home extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button buttonFoo = (Button)findViewById(R.id.buttonFoo);
buttonFoo.setOnClickListener(openFoo);
}
public OnClickListener openFoo = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Home.this, Foo.class);
startActivity(intent);
}
};
}
以下是活动代码Foo:
public class Foo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//populateWithLatest();
}
}
错误日志:
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): FATAL EXCEPTION: main
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.github.progval.openquote/com.github.progval.openquote.sites.FooActivity}; have you declared this activity in your AndroidManifest.xml?
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.app.Activity.startActivityForResult(Activity.java:2827)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.app.Activity.startActivity(Activity.java:2933)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at com.github.progval.openquote.Home$1.onClick(Home.java:34)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.view.View.performClick(View.java:2501)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.view.View$PerformClick.run(View.java:9107)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.os.Handler.handleCallback(Handler.java:587)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.os.Looper.loop(Looper.java:130)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at android.app.ActivityThread.main(ActivityThread.java:3835)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at java.lang.reflect.Method.invoke(Method.java:507)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-18 19:53:35.043: ERROR/AndroidRuntime(4656): at dalvik.system.NativeStart.main(Native Method)
除非我对该行startActivity(intent);
发表评论,否则当我按下Foo按钮时,app force会关闭。
答案 0 :(得分:7)
<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name=".Home"
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=".Foo"></activity>
这是我最喜欢的部分:
07-18 19:53:35.043:ERROR / AndroidRuntime(4656):android.content.ActivityNotFoundException:无法找到显式活动类{com.github.progval.openquote / com.github.progval.openquote.sites。 VdmActivity}; 您是否在AndroidManifest.xml中声明了此活动?
我不知道这是否是实现按钮的最佳方式,但这是我如何做到的:
findViewById(R.id.buttonFoo).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(v.getContext(), Foo.class);
startActivity(i);
}
});
答案 1 :(得分:1)
查看您的清单文件。您没有在那里签署Foo活动。在主要活动标记
下put&lt;活动android:name =“。Foo”&gt;&lt; /活性GT;
答案 2 :(得分:0)
需要在清单文件中声明所有活动。先检查一下。
答案 3 :(得分:0)
实现OnClickListener时,它更容易,代码看起来会更好。因此,您只需添加方法onClick(View v)。作为onClickListener的上下文设置'this'以及intent的上下文。请参阅下面的代码。通常,这将有效。
public class Home extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button buttonFoo = (Button)findViewById(R.id.buttonFoo);
buttonFoo.setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = new Intent(this, Foo.class);
startActivity(intent);
}
}
编辑:我也为你的Foo活动,通过在清单中声明活动有一个领先的点。删除此以查找您的活动。在清单中声明您的活动的最佳方式是使用GUI(Android清单文件&gt;应用程序&gt;应用程序节点&gt;添加&gt;活动&gt;名称:选择您的活动
在Android中享受编码乐趣! Grtz
答案 4 :(得分:0)
你的Foo类应该是setContentView。 也就是说,
public class Foo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Foo.setContentView(R.layout.????)
//populateWithLatest();
}
}
否则