I have a weird problem with somethink what should be simple. I have created my custom action bar class and i put it in some activity. I whated my custom action bar to have method “set text” and “setBackButtonVisible”. Those methods should be invoked from activity class where my action bar is. The point is that invoking those methods from my activity class body results in error. Now i am forced to Inject button/textview to from my action bar to activity so I can manipulate them. (Note that I’m using RoboGuice 2.0) class, as you can see in error logs.
edit: I forgot to mention that My MainMenuActivity lay on TabBar
ActionBar Class:
public class CustomActionBar extends RelativeLayout{
//View v;
@InjectView(R.id.action_back_button)
Button backButton;
@InjectView(R.id.action_bar_text)
TextView tv;
public CustomActionBar(Context context, AttributeSet attrs){
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*v = */layoutInflater.inflate(R.layout.action_bar, this);
}
public CustomActionBar(Context context) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*v = */layoutInflater.inflate(R.layout.action_bar, this);
}
// Cannot invoke those methods from MainMenuActivity
public void setText(String text){
tv.setText(text);
}
public void setBackButtonVisible(boolean set){
// invoking this method in MainMenuActivity constructor will produce funny error
if(!set){
backButton.setVisibility(View.GONE);
}else{
backButton.setVisibility(View.VISIBLE);
}
}
}
activity class
@ContentView(R.layout.activity_main_menu)
public class MainMenuActivity extends RoboActivity {
@InjectView(R.id.action_back_button)
Button actionBackButton;
@InjectView(R.id.action_bar_text)
TextView actionBarText;
@InjectView(R.id.action_bar)
CustomActionBar actionBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBackButton.setVisibility(View.GONE);
// Hardcoded title for actionbar
// This one works: actionBarText.setText("TEST");
actionBar.setText("Main Menu");
}
public void onBackButtonClick(View v){
this.finish();
}
}
error log
main java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at roboguice.event.eventListener.ObserverMethodListener.onEvent(ObserverMethodListener.java:32)
at roboguice.event.EventManager.fire(EventManager.java:130)
at roboguice.activity.RoboActivity.onCreate(RoboActivity.java:80)
at com.activities.MainMenuActivity.onCreate(MainMenuActivity.java:31)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:1499)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:731)
at android.widget.TabHost.setCurrentTab(TabHost.java:403)
at android.widget.TabHost.addTab(TabHost.java:242)
at com.altiweb.explorius.activities.TabHostActivity.addTab(TabHostActivity.java:77)
at com.altiweb.explorius.activities.TabHostActivity.onCreate(TabHostActivity.java:33)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.custom.ActionBar
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
at android.app.Activity.setContentView(Activity.java:1657)
at roboguice.inject.ContentViewListener.optionallySetContentView(ContentViewListener.java:21)
... 29 more
Ok.. I got it.
It was my TERRIBLE mistake.
What ws wrong ? I invoked my custom ActionBar by
The point is that R.id.action_bar is not action bar id… it was my actionbars main Layout id…