其他活动中的OnCreate扩展了基本活动

时间:2018-07-23 21:29:20

标签: java android

这是基本活动。.当我运行应用程序时,它的强制关闭未显示错误。.由于我是androidstudio的新手,请帮助我,我无法弄清错误是什么..

public abstract class BaseActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

protected BottomNavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getContentViewId());

    navigationView = findViewById(R.id.navigation);
    navigationView.setOnNavigationItemSelectedListener(this);

}

abstract int getContentViewId();


@Override
protected void onStart() {
    super.onStart();
    updateNavigationBarState();
}

private void updateNavigationBarState() {
    int actionId = getNavigationMenuItemId();
    selectBottomNavigationBarItem(actionId);
}

abstract int getNavigationMenuItemId();

private void selectBottomNavigationBarItem(int itemId) {
    MenuItem item = navigationView.getMenu().findItem(itemId);
    item.setChecked(true);
}
@Override
public void onPause() {
    super.onPause();
    overridePendingTransition(0, 0);
}

这是图书馆活动。。我想问题就在这里,但是找不到问题所在

public class LibraryActivity extends BaseActivity {
@Override
int getContentViewId() {
    return R.layout.activity_library;
}

@Override
int getNavigationMenuItemId() {
    return R.id.navigation_library;
}
private static final String TAG = "LibraryActivity";

private SlidingUpPanelLayout mLayout;

@Override
public void onCreate (Bundle savedInstanceState) {
    setContentView(R.layout.activity_library);
    super.onCreate(savedInstanceState);
    setSupportActionBar((Toolbar) findViewById(R.id.main_toolbar));

    ListView lv = (ListView) findViewById(R.id.list);
    lv.setOnItemClickListener((parent, view, position, id) -Toast.makeText(LibraryActivity.this, "onItemClick", Toast.LENGTH_SHORT).show());

这是Library.xml ..不知道是什么问题

      <?xml version="1.0" encoding="utf-8"?>
       <RelativeLayout
       xmlns:sothree="http://schemas.android.com/apk/res-auto"
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:id="@+id/container"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LibraryActivity">

<com.sothree.slidinguppanel.SlidingUpPanelLayout
    android:id="@+id/sliding_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    sothree:umanoPanelHeight="68dp"
    sothree:umanoShadowHeight="4dp"
    sothree:umanoParallaxOffset="100dp"
    sothree:umanoDragView="@+id/dragView"
    sothree:umanoOverlay="true"
    sothree:umanoScrollableView="@+id/list">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            xmlns:sothree="http://schemas.android.com/apk/res-auto"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/main_toolbar"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            sothree:theme="@style/ActionBar"
            android:layout_width="match_parent"/>
        <TextView
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize"
            android:gravity="center"
            android:text="Main Content"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="true"
            android:textSize="16sp" />
    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical"
        android:clickable="true"
        android:focusable="false"
        android:id="@+id/dragView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/name"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textSize="14sp"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"/>

            <Button
                android:id="@+id/follow"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textSize="14sp"
                android:gravity="center_vertical|right"
                android:paddingRight="10dp"
                android:paddingLeft="10dp"/>

        </LinearLayout>

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </ListView>
    </LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>

<include
    android:id="@+id/navigation"
    layout="@layout/bottom_navgation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

1 个答案:

答案 0 :(得分:0)

setSupportActionBar((Toolbar) findViewById(R.id.main_toolbar));返回null。也许是在找到ID之前进行的强制转换会导致此问题。

创建工具栏变量,然后设置操作栏,例如:

Toolbar tb = this.findViewById(R.id.YOUR_TOOLBAR);

this.setSupportActionBar(tb);

参考:android documentation for toolbar