I was trying to modify an default empty activity and form a default activity with action bar. There are two activities I made, which are both empty without actionbar. What I try to achieve is to click on the first activity(without actionbar) and navigate to the second activity(with actionbar). What I have done are:
1. add a menu
2. modify the second activity by changing the activity to AppCompatActivity
3. add a support library which is v7-appcompat (which was added as a referenced library. I am not sure whether it should be referenced lib or other format.)
The Manifest file part is shown below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tracker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/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=".SecondActivity"
android:label="@string/second" >
</activity>
</application>
</manifest>
The first activity is:
package com.example.tracker;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the button
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent= new Intent(MainActivity.this,SecondActivity.class);
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
}
});
}
}
The second activity is:
package com.example.tracker;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
The menu in res is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.tracker.MainActivity"
>
<item
android:id="@+id/myitem"
android:orderInCategory="2"
android:title="myitem"
app:showAsAction="never"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="1"
android:title="action_settings"
app:showAsAction="never"/>
</menu>
The only observable problem is that the menu has an error which is written in the title of this problem. Currently, the first activity can run properly, however, when I click the button, the application stops.
By removing app:showAsAction="never" does not solve the problem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…