05-03 18:48:55.478 2924-2924/com.example.farias.cores E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.farias.cores, PID: 2924
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.farias.cores/com.example.farias.cores.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at com.example.farias.cores.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Alguém aqui sabe o motivo do erro ?
Código fonte completo:
MainActivity.class ----------------------------------------------------------------------------------------------
package com.example.farias.cores;
import android.app.ActionBar;
import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView texto = (TextView) findViewById(R.id.texto);
texto.setTextAppearance(this, R.style.estilo);
}
@Override
public boolean onCreateOptionsMenu (Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.search);
SearchView sc = (SearchView) item.getActionView();
sc.setOnQueryTextListener(onSearch());
return true;
}
private SearchView.OnQueryTextListener onSearch() {
return new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String s) {
AlertDialog.Builder al = new AlertDialog.Builder(getBaseContext());
al.setTitle("Pesquisar");
al.setMessage("Você digitou: " + s);
al.show();
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
};
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.search) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml--------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.farias.cores.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
style="@style/estilo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="135dp"
android:id="@+id/texto" />
</RelativeLayout>
AndroidManifest.xml------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.farias.cores">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:label="Pesquisar"
android:theme="@style/AppTheme"
android:value="splitActionBarWhenNarrow"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
menu_main.xml---------------------------------------------------------------------------------------------
<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.farias.myapplication.MainActivity">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_black_24dp"
android:title="Configurações"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
/>
</menu>
css.xml-------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="estilo">
<item name="android:textSize">34sp</item>
<item name="android:textColor">#3F51B5</item>
<item name="android:textStyle">italic</item>
</style>
</resources>
styles.xml-------------------------------------------------------------------------------------
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
Lembrando que eu criei uma activity vazia e implementei o menu manualmente. Que puder me ajudar fico muito grato