我正在尝试像许多应用一样实现折叠工具栏,我尝试添加它但是它崩溃了我的应用
这里是logcat
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: FATAL EXCEPTION: main
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: Process: com.koshur.socialnetwork, PID: 18867
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.koshur.socialnetwork/com.koshur.socialnetwork.activities.ProfilePreview}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2357)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5293)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:206)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at com.koshur.socialnetwork.activities.ProfilePreview.onCreate(ProfilePreview.java:81)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5990)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2310)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5293)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
11-11 12:51:18.622 18867-18867/com.koshur.socialnetwork E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
这是我的.xml布局
<?xml version="1.0" encoding="utf-8"?>
<org.buraktamturk.loadingview.LoadingView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/loginLoadingView"
app:lv_color="@android:color/holo_red_dark"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/userProfileCover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/header"/>
<LinearLayout
android:id="@+id/head"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3f000000"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/userProfilePicture"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/userProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Naveed"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"/>
<TextView
android:id="@+id/userProfileAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Naveed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white"/>
<TextView
android:id="@+id/userProfileJob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Naveed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white"/>
</LinearLayout>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:background="@color/primaryColor"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Total Followers"
android:textColor="@android:color/white"
android:textStyle="bold"/>
<TextView
android:id="@+id/userTotalFollowers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="1,573"
android:textColor="@android:color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Total Posts"
android:textColor="@android:color/white"
android:textStyle="bold"/>
<TextView
android:id="@+id/userTotalPosts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="1,573"
android:textColor="@android:color/white"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/actionProfileArea"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="@android:color/white"
android:gravity="center"
android:orientation="horizontal"
android:visibility="visible">
<TextView
android:id="@+id/userPostsBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp"
android:text="POSTS"
android:textColor="@color/primaryColor"
android:textStyle="bold"/>
<TextView
android:id="@+id/contactProfileBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp"
android:text="CONTACT"
android:textColor="@color/primaryColor"
android:textStyle="bold"/>
<TextView
android:id="@+id/followProfileBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp"
android:text="FOLLOW"
android:textColor="@color/primaryColor"
android:textStyle="bold"/>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/app_bar">
<android.support.v7.widget.RecyclerView
android:id="@+id/postsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/postBg">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</org.buraktamturk.loadingview.LoadingView>
这是我的java活动
public class ProfilePreview extends AppCompatActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
public TextView userProfileName,
userTotalFollowers,
userTotalPosts,
userProfileAddress,
userProfileJob,
followProfileBtn,
contactProfileBtn,
userPostsBtn;
public userItem user;
public RecyclerView postsList;
public LinearLayoutManager layoutManager;
int currentPage = 1;
private HomeListAdapter mHomeListAdapter;
private SwipeRefreshLayout mSwipeRefreshLayout;
public ImageView userProfilePicture, userProfileCover;
public LinearLayout actionProfileArea;
public int userID = 0;
private CacheManager mCacheManager;
private ThinDownloadManager downloadManager;
private Gson mGson;
LoadingView loginLoadingView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (M.getToken(this) == null) {
Intent mIntent = new Intent(this, LoginActivity.class);
startActivity(mIntent);
finish();
} else {
setContentView(R.layout.layout_prof);
initializeView();
loginLoadingView = (LoadingView)findViewById(R.id.loginLoadingView);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
if (getIntent().hasExtra("userID")) {
userID = getIntent().getExtras().getInt("userID");
}
mCacheManager = CacheManager.getInstance(getApplicationContext());
layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
postsList.setLayoutManager(layoutManager);
mHomeListAdapter = new HomeListAdapter(this, new ArrayList<PostsItem>());
postsList.setAdapter(mHomeListAdapter);
mGson = new Gson();
getUser();
getPosts();
}
}
非常抱歉,如果这篇文章太大了。
答案 0 :(得分:0)
您的布局不包含工具栏,因此
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
返回null
因此,当您设置SupportActionBar
时,崩溃已经发生,因为它试图在Toolbar
上设置标题