Style on incompatible Android

2

Good morning,

First of all forgive me that I am new with Android. It's my second application.

The problem I have is that with Android Studio I created a project with Navigation Drawer Activity.

Then I create an Activity blank and introduce the following since I want to put a bar with a series of buttons:

    getActionBar().setTitle(R.string.title_devices);

I get the following error:

06-28 13:39:13.880 1192-1192/com.example.shokimc.proyecto E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shokimc.proyecto, PID: 1192
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shokimc.proyecto/com.example.shokimc.proyecto.Escaneo}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(int)' on a null object reference
        at com.example.shokimc.proyecto.Escaneo.onCreate(Escaneo.java:54)
        at android.app.Activity.performCreate(Activity.java:6033)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

I think it's due to incompatibility of styles, but I'm a little lost. This is my style.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>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

My Mainactivity.java

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle(getString(R.string.title_devices));
mHandler = new Handler();

// Use this check to determine whether BLE is supported on the device.      Then you can
// selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
    finish();
}

// Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();

// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
    Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
    finish();
    return;
}
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

Can someone help me?

    
asked by Shokimc 28.06.2016 в 13:47
source

2 answers

0

Before using getActionBar() you must use getSupportActionBar() and the activity that extends from AppCompatActivity

I use the following to establish the title:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

...

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

if (toolbar != null) {
    toolbar.setTitle("ActionBar Title");
    toolbar.setSubtitle("Subtitle");
}
    
answered by 28.06.2016 / 14:17
source
0

The problem is that your activity extends a ListActivity , to be able to do this:

Actionbar actionbar = getSupportActionBar();
actionbar.setTitle("Hola StackOverflow!");

Your Activity should extend from FragmentActivity , ActionBarActivity or AppCompatActivity .

I recommend changing to any of the 3 for example:

MainActivity extends AppCompatActivity

The change is simple, within setContentView() of the method onCreate() add the layout containing the ListView .

    
answered by 28.06.2016 в 21:07