gpt4 book ai didi

java - 其他 Activity 中的OnCreate扩展了基本 Activity

转载 作者:太空宇宙 更新时间:2023-11-04 10:17:44 25 4
gpt4 key购买 nike

这是基本 Activity ..当我运行应用程序时,它的强制关闭没有显示错误..请帮助我,因为我是 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);
}

这是图书馆 Activity ..我认为问题就在这里,但找不到问题所在

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" />

最佳答案

setSupportActionBar((Toolbar) findViewById(R.id.main_toolbar)); 返回 null。也许是在找到 id 之前进行的强制转换导致了这种情况。

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

工具栏 tb = this.findViewById(R.id.YOUR_TOOLBAR);

this.setSupportActionBar(tb);

引用:android documentation for toolbar .

关于java - 其他 Activity 中的OnCreate扩展了基本 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51487463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com