Dies ist meine BaseActivity-Klasse, die von anderen Aktivitäten erweitert wird.get null-Zeiger in getSupportActionBar()
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.bidhee.jme.R;
import com.bidhee.jme.controller.bus_and_events.EventBus;
import com.bidhee.jme.controller.bus_and_events.Events;
import com.bidhee.jme.model.SearchViewModel;
import com.bidhee.jme.utils.Metadata;
import com.bidhee.jme.utils.UtilityMethods;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* Created by ronem on 6/5/16.
*/
public abstract class BaseActivity extends AppCompatActivity {
private String TAG = getClass().getSimpleName();
@Bind(R.id.toolbar)
Toolbar toolbar;
public SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getResourceLayout());
/**
* Bind {@link ButterKnife}
*/
ButterKnife.bind(this);
EventBus.register(this);
/**
* Setting toolbar in the place of actionbar
* and enabling the back arrow on it so that use can go back to the previous activity
*/
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
if (getSupportActionBar() != null) {
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.getSupportActionBar().setHomeButtonEnabled(true);
}
/**
* Loop through all the child's of toolbar
* and change the attributes value
*/
if (getActivityId() != 0) {
if (toolbar != null) {
for (int i = 0; i < toolbar.getChildCount(); i++) {
View view = toolbar.getChildAt(i);
if (view instanceof ImageButton) {
ImageButton tv = (ImageButton) view;
tv.setColorFilter(Color.WHITE);
}
}
}
}
/**
* If getActivityId matches with the {@link Metadata.DASHBOARD_ID}
* set actionbar title to empty
* change the navigation icon to custom icon
* and background of the action bar to transparent
*
*/
if (getActivityId() == Metadata.DASHBOARD_ID) {
if (getSupportActionBar() != null) {
this.getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_person_outline_black_24dp);
this.getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
}
}
if (getSupportActionBar() != null)
getSupportActionBar().setTitle(getActivityTitle());
/**
* change the status bar color
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
}
//returns layout resource
public abstract int getResourceLayout();
//returns Activity's ID
public abstract int getActivityId();
//returns Activity Title
public abstract String getActivityTitle();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_japan_money_express, menu);
//setting all menu items tint color to white
UtilityMethods.setTintColorForMenuItems(menu);
//referencing to search menu item
MenuItem menuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
EventBus.post(new Events.SearchViewEvent(new SearchViewModel(newText)));
return false;
}
});
if (getActivityId() == Metadata.DASHBOARD_ID) {
//show only chat notification icon on toolbar menu and hide rest of the items of toolbar
showMenu(menu, 0);
} else if (getActivityId() == Metadata.MONEY_TRANSFER_INITIAL_ID) {
//show only search view icon on toolbar menu and hide rest of the items of toolbar
showMenu(menu, 1);
} else {
hideAllMenu(menu);
}
return super.onCreateOptionsMenu(menu);
}
private void hideAllMenu(Menu menu) {
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setVisible(false);
menu.getItem(i).setVisible(false);
}
}
private void showMenu(Menu menu, int position) {
for (int i = 0; i < menu.size(); i++) {
if (position == i) {
menu.getItem(i).setVisible(true);
} else {
menu.getItem(i).setVisible(false);
}
}
}
/**
* Overriding the onOptionItemSelected so that we can access the actions of the actionbar
*
* @param item
* @return
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (getActivityId() == Metadata.DASHBOARD_ID) {
Toast.makeText(getApplicationContext(), "user info under constrction", Toast.LENGTH_SHORT).show();
} else {
this.finish();
}
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.zoom_in_from_top_left_to_bottom_right, R.anim.slide_down_dialog);
}
@Override
protected void onDestroy() {
super.onDestroy();
/**
* must unbind the ButterKnife on Destroy
*/
ButterKnife.unbind(this);
EventBus.unregister(this);
}
}
Und es gibt mehr als 3 Aktivitäten, wird diese Basisklasse Aktivität erstreckt. Einer von ihnen ist das.
CheckRateActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bidhee.jme.R;
import com.bidhee.jme.controller.bus_and_events.Events;
import com.bidhee.jme.model.CountryModel;
import com.squareup.otto.Subscribe;
import butterknife.Bind;
import butterknife.OnClick;
/**
* Created by ronem on 6/13/16.
*/
public class CheckRateActivity extends BaseActivity {
private String TAG = getClass().getSimpleName();
@Bind(R.id.sendingFromLayout)
LinearLayout sendingFromLayout;
@Bind(R.id.sendingtoLayout)
LinearLayout sendingToLayout;
CountryModel country;
ImageView fromCountryFlag, toCountryFlag;
TextView fromCountryTitle, toCountryTitle;
View arrowDownTo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fromCountryFlag = (ImageView) sendingFromLayout.findViewById(R.id.row_image);
fromCountryTitle = (TextView) sendingFromLayout.findViewById(R.id.row_title);
fromCountryFlag.setImageResource(R.drawable.jamaica);
fromCountryTitle.setText("Jamaica");
toCountryFlag = (ImageView) sendingToLayout.findViewById(R.id.row_image);
toCountryTitle = (TextView) sendingToLayout.findViewById(R.id.row_title);
arrowDownTo = (View) sendingToLayout.findViewById(R.id.arrow_down_view);
arrowDownTo.setVisibility(View.VISIBLE);
}
@Override
public int getResourceLayout() {
return R.layout.check_rate_layout;
}
@Override
public int getActivityId() {
return 0;
}
@Override
public String getActivityTitle() {
return "Check rates";
}
@OnClick(R.id.sendingtoLayout)
public void onSendinToLayoutClicked() {
Intent intent = new Intent(CheckRateActivity.this, SelectCountryListActivity.class);
startActivity(intent);
}
@Subscribe
public void getCountry(Events.CountryModelEvent _country) {
if (_country != null) {
Log.d(TAG, _country.getCountryModel().toString());
country = _country.getCountryModel();
toCountryFlag.setImageResource(country.getCountryIcon());
toCountryTitle.setText(country.getCountryName());
Intent intent = new Intent(CheckRateActivity.this, MakeTransferActivity.class);
// Bundle bundle = new Bundle();
// bundle.putString(Metadata.COUNTRY_ID, c.getId());
// bundle.putString(Metadata.COUNTRY_NAME, c.getCountryName());
// bundle.putInt(Metadata.COUNTRY_FLAG, c.getCountryIcon());
//
// intent.putExtras(bundle);
Bundle box = new Bundle();
box.putParcelable("country", country);
intent.putExtras(box);
startActivity(intent);
}
}
}
Das ist mein AndroidManifest.xml Dateiinhalt
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bidhee.jme">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".controller.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:launchMode="singleTask"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".views.activities.JapanMoneyExpress"
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=".views.activities.Dashboard"
android:label="@string/dashboard"
android:screenOrientation="portrait" />
<activity
android:name=".views.activities.RegisterNewAccount"
android:screenOrientation="portrait" />
<activity
android:name=".views.activities.CheckRateActivity"
android:screenOrientation="portrait" />
<activity
android:name=".views.activities.SelectCountryListActivity"
android:screenOrientation="portrait" />
<activity
android:name=".views.activities.MakeTransferActivity"
android:screenOrientation="portrait" />
</application>
</manifest>
Diese Arten sind
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:windowBackground">@color/white</item>
<!--<item name="colorControlHighlight">@color/white</item>-->
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
App für die meisten der reibungslose Ablauf Zeit aber manchmal bekomme ich die NullpointerException in getSupportActionBar(). Also habe ich diesen Code in StackOverflow verwendet.
jetzt App läuft, aber einige Zeit kann ich keinen Titel und zurück Pfeil Home-Taste in der Symbolleiste sehen. Ich bin mit diesem für mehr als 1 Tag fest.