Я изучаю материалы для разработки Android и теперь участвую в создании ActionBar.android Создать ActionBar
Ниже приведен код DisplayMessageActivity.java, который встречается ошибка «java.lang.RuntimeException: Невозможно начать деятельность ComponentInfo»:
package com.mycompany.myfa;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.*;
import android.content.Intent;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
//from SF
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
//getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
//openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Однако, когда я закомментировать код «getActionBar () .setDisplayHomeAsUpEnabled (истина);» Приложения могут работать успешно, но без ActionBar. Я пропускаю что-либо, чтобы «getActionBar(). SetDisplayHomeAsUpEnabled (true);» потерпит неудачу?
Спасибо.
Я попытался использовать getSupportActionBar(). SetDisplayHomeAsUpEnabled (true); но ошибка все еще настаивает. Ниже приводится манифест:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myfa" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName=".MyActivity"
android:theme="@android:style/Theme.Holo.Light" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfa.MyActivity" />
</activity>
</application>
</manifest>
Еще раз спасибо.
показать файл манифеста – Rajesh