1

我有一个奇怪的问题,认为应该简单。我已经创建了我的自定义操作栏类,并将它放在一些活动中。我希望我的自定义操作栏有方法“设置文本”和“setBackButtonVisible”。这些方法应该从我的操作栏所在的活动类中调用。关键是从我的活动类主体中调用这些方法会导致错误。现在我被迫将按钮/文本视图从我的操作栏注入到活动中,以便我可以操纵它们。(请注意,我使用的是 RoboGuice 2.0)类,您可以在错误日志中看到。

编辑:我忘了提到我的 MainMenuActivity 位于 TabBar

动作栏类:

    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
4

1 回答 1

1

好,我知道了。

这是我的可怕错误。

出了什么问题?我通过调用我的自定义 ActionBar

 @InjectView(R.id.action_bar)
 CustomActionBar actionBar;

关键是 R.id.action_bar 不是操作栏 ID ...这是我的操作栏主布局 ID ...

于 2012-10-27T15:13:09.237 回答