扩展实现ToolBar的基本活动时,活动中未显示工具栏

时间:2018-08-07 16:43:47

标签: java android android-activity android-toolbar android-menu

我是一名初学者Android开发人员,我一直在努力解决在继承基本活动的任何活动中都看不到工具栏的问题。根据其他资源,在不同的活动中使用相同的工具栏。我必须在基本活动中实现它,并在需要使用它的地方继承它。有人可以帮我解决问题吗?

styles.xml

[debug] [MJSONWP] Calling AppiumDriver.findElements() with args: ["id","com.android.packageinstaller:id/permission_allow_button","8e38c406-877d-470b-a6c8-7df870d0a94b"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.android.packageinstaller:id/permission_allow_button","context":"","multiple":true}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.android.packageinstaller:id/permission_allow_button","context":"","multiple":true}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.android.packageinstaller:id/permission_allow_button' using 'ID' with the contextId: '' multiple: true
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[RESOURCE_ID=com.android.packageinstaller:id/permission_allow_button]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[RESOURCE_ID=com.android.packageinstaller:id/permission_allow_button]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0)
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=com.android.packageinstaller:id/permission_allow_button]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (1)
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=1, RESOURCE_ID=com.android.packageinstaller:id/permission_allow_button]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":[{"ELEMENT":"1"}]}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [MJSONWP] Responding to client with driver.findElements() result: [{"ELEMENT":"1"}]
[HTTP] <-- POST /wd/hub/session/8e38c406-877d-470b-a6c8-7df870d0a94b/elements 200 7373 ms - 89
[HTTP] --> GET /wd/hub/session/8e38c406-877d-470b-a6c8-7df870d0a94b/element/1/displayed {}
[debug] [MJSONWP] Calling AppiumDriver.elementDisplayed() with args: ["1","8e38c406-877d-470b-a6c8-7df870d0a94b"]
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"element:getAttribute","params":{"attribute":"displayed","elementId":"1"}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"element:getAttribute","params":{"attribute":"displayed","elementId":"1"}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getAttribute
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [MJSONWP] Responding to client with driver.elementDisplayed() result: true
[HTTP] <-- GET /wd/hub/session/8e38c406-877d-470b-a6c8-7df870d0a94b/element/1/displayed 200 295 ms - 76
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":"true"}
[HTTP] --> POST /wd/hub/session/8e38c406-877d-470b-a6c8-7df870d0a94b/element {"using":"id","value":"com.android.packageinstaller:id/permission_allow_button"}
[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["id","com.android.packageinstaller:id/permission_allow_button","8e38c406-877d-470b-a6c8-7df870d0a94b"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.android.packageinstaller:id/permission_allow_button","context":"","multiple":false}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.android.packageinstaller:id/permission_allow_button","context":"","multiple":false}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.android.packageinstaller:id/permission_allow_button' using 'ID' with the contextId: '' multiple: false
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.android.packageinstaller:id/permission_allow_button]
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [MJSONWP] Responding to client with driver.findElement() result: {"ELEMENT":"2"}
[HTTP] <-- POST /wd/hub/session/8e38c406-877d-470b-a6c8-7df870d0a94b/element 200 18 ms - 87
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":{"ELEMENT":"2"}}
[HTTP] --> POST /wd/hub/session/8e38c406-877d-470b-a6c8-7df870d0a94b/element/2/click {"id":"2"}
[debug] [MJSONWP] Calling AppiumDriver.click() with args: ["2","8e38c406-877d-470b-a6c8-7df870d0a94b"]
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"element:click","params":{"elementId":"2"}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"element:click","params":{"elementId":"2"}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: click
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [MJSONWP] Responding to client with driver.click() result: true
[HTTP] <-- POST /wd/hub/session/8e38c406-877d-470b-a6c8-7df870d0a94b/element/2/click 200 3223 ms - 76
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":true}
[HTTP] --> POST /wd/hub/session/8e38c406-877d-470b-a6c8-7df870d0a94b/element/2/click {"id":"2"}
[debug] [MJSONWP] Calling AppiumDriver.click() with args: ["2","8e38c406-877d-470b-a6c8-7df870d0a94b"]
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"element:click","params":{"elementId":"2"}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"element:click","params":{"elementId":"2"}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: click
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [MJSONWP] Responding to client with driver.click() result: true
[HTTP] <-- POST /wd/hub/session/8e38c406-877d-470b-a6c8-7df870d0a94b/element/2/click 200 3377 ms - 76
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":true}
[HTTP] --> DELETE /wd/hub/session/8e38c406-877d-470b-a6c8-7df870d0a94b {}
[debug] [MJSONWP] Calling AppiumDriver.deleteSession() with args: ["8e38c406-877d-470b-a6c8-7df870d0a94b"]
[debug] [BaseDriver] Event 'quitSessionRequested' logged at 1533841411213 (14:03:31 GMT-0500 (Central Daylight Time))
[Appium] Removing session 8e38c406-877d-470b-a6c8-7df870d0a94b from our master session list
[debug] [AndroidDriver] Shutting down Android driver

菜单文件夹中的quiz.menu.xml

<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>

BaseActivity

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/contact"
    android:icon="@drawable/ic_contacts_black_24dp"
    android:title="Contact"
    app:showAsAction="ifRoom" />

<item android:id="@+id/language"
    android:title="Language"
    app:showAsAction="never" />

<item android:id="@+id/speech"
    android:title="Speech"
    app:showAsAction="never">

    <munu>
        <item android:id="@+id/subitem1"
            android:title="Sub Item 1"/>

        <item android:id="@+id/subitem2"
            android:title="Sub Item 2"/>
    </munu>
</item>
</menu>

WelcomeActivity(继承基本活动)

public class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.quiz_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.contact:
            Toast.makeText(this, "Contact is selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.language:
            Toast.makeText(this, "Language is selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.speech:
            Toast.makeText(this, "Speech is selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.subitem1:
            Toast.makeText(this, "Language is selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.subitem2:
            Toast.makeText(this, "Speech is selected", Toast.LENGTH_SHORT).show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }
}

问卷

public class WelcomeActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);

    final Button databaseButton = findViewById(R.id.database);
    databaseButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Code here executes on main thread after user presses button
            Intent databaseIntent = new Intent(WelcomeActivity.this, Questionnaire.class);
            startActivity(databaseIntent);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

答案实际上很简单。您首先在BaseActivity类的OnCreate()方法中设置contentview,然后在子类的OnCreate()方法中将视图更改为另一个xml文件。

我建议您不要在BaseActivity类中实现OnCreate()方法,而在BaseActivity中实现SetContentView()方法。

简而言之,请从BaseActivity()中删除onCreate()方法,然后在下面添加setContentView()方法。

@Override
public void setContentView(int layoutResID) {
    super.setContentView(layoutResID);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

P.S-您的quiz_menu.xml文件看起来不像菜单文件。 :) P.P.S-让我知道它是否有效,或者遇到麻烦。 :)