Start new Activity from menu
I wrote simple android app with bugs. When open second activity from menu- android stopped apps.
Code: 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>
First 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);
}
}
Second Activity with temporary layout(it the same in the first):
package com.example;
import android.app.Activity;
i开发者_开发问答mport android.os.Bundle;
public class Activit2 extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
your tag <Activity />
should be in ( not out ) of the tag <application>
:
try this :
<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>
The tag <activity android:name=".Activit2"/>
should be inside the <application>
tag in your Manifest.xml
Your second activity definition is outside the application definition in your AndroidManifest.xml.
精彩评论