gpt4 book ai didi

java - 从菜单启动新 Activity

转载 作者:行者123 更新时间:2023-12-01 15:45:00 25 4
gpt4 key购买 nike

我编写了一个简单的 Android 应用程序,但存在错误。当从菜单中打开第二个 Activity 时 - Android 停止了应用程序。

代码:Manifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activit"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<activity android:name=".Activit2"/>
</manifest>

第一个 Activity :

  package com.example;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class Activit extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.me, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){

switch(item.getItemId()){
case R.id.ok: Activ();

};


return super.onOptionsItemSelected(item);
}

private void Activ(){

Intent inten=new Intent();
inten.setClass(Activit.this, Activit2.class);
startActivity(inten);
}

}

具有临时布局的第二个 Activity(与第一个 Activity 相同):

    package com.example;
import android.app.Activity;
import android.os.Bundle;
public class Activit2 extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}


}

最佳答案

您的标签<Activity />应该在标签 <application> 之内(而不是之外) :

试试这个:

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activit"
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=".Activit2"/> // you should declare your activity Here
</application>

关于java - 从菜单启动新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7232119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com