我有一个奇怪的问题,有些想想应该是简单的。我创建了自定义操作栏类,并将其放入某些活动中。我想我的自定义操作栏有方法“设置文本”和“setBackButtonVisible”。应该从我的操作栏所在的活动类调用这些方法。关键是从我的activity类体调用这些方法会导致错误。现在我被迫从我的操作栏注入按钮/文本视图到活动,所以我可以操纵它们。 (请注意,我正在使用RoboGuice 2.0)类,您可以在错误日志中看到。
编辑:我忘了提到My MainMenuActivity位于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);
}
}
}
活动类
@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();
}
}
错误日志
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
答案 0 :(得分:1)
好的..我明白了。
这是我可怕的错误。
错了什么?我通过
调用我的自定义ActionBar @InjectView(R.id.action_bar)
CustomActionBar actionBar;
关键是R.id.action_bar不是动作栏ID ...这是我的动作栏主要布局ID ...