我正在开发一个应用程序,其中我有一次打开活动(名为“logIn”),然后是我的main_activity。在一些博客的帮助下,我试图在main_activity中实现导航抽屉(名为“聊天”)。
我的Chats.java文件:
public class Chats extends AppCompatActivity {
String TITLES[] = {"Home","Events","Mail","Shop","Travel"};
int ICONS[] = {R.drawable.ic_action_alarms,R.drawable.ic_action_alarms,R.drawable.ic_action_alarms,R.drawable.ic_action_alarms,R.drawable.ic_action_alarms};
String NAME = "Karan Nagpal";
String EMAIL = "nagpalkaran95@gmail.com";
int PROFILE = R.drawable.ic_action_alarms;
private Toolbar toolbar; // Declaring the Toolbar Object
RecyclerView mRecyclerView; // Declaring RecyclerView
RecyclerView.Adapter mAdapter; // Declaring Adapter For Recycler View
RecyclerView.LayoutManager mLayoutManager; // Declaring Layout Manager as a linear layout manager
DrawerLayout Drawer; // Declaring DrawerLayout
ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_chats);
Boolean isFirstTime;
SharedPreferences app_preferences = getSharedPreferences("mainuser",MODE_PRIVATE);
SharedPreferences.Editor editor = app_preferences.edit();
isFirstTime = app_preferences.getBoolean("isFirstTime", true);
if (isFirstTime) {
Intent i = new Intent(this, logIn.class);
startActivity(i);
editor.putBoolean("isFirstTime", false);
editor.commit();
}
else{
setContentView(R.layout.activity_chats);
final String username = app_preferences.getString("username", null);
if(username==null){
Intent i = new Intent(this, logIn.class);
startActivity(i);
editor.putBoolean("isFirstTime", false);
editor.commit();
}
TextView main_user = (TextView) findViewById(R.id.main_user);
main_user.setText(username);
}
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View
mRecyclerView.setHasFixedSize(true); // Letting the system know that the list objects are of fixed size
mAdapter = new MyAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE);
mRecyclerView.setAdapter(mAdapter);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
Drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.openDrawer,R.string.closeDrawer){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
Drawer.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
记录错误:
10-02 23:53:31.484 2258-2334/com.nagpal.karan.radarchat I/OpenGLRenderer: Initialized EGL, version 1.4
10-02 23:53:31.503 2258-2334/com.nagpal.karan.radarchat D/OpenGLRenderer: Enabling debug mode 0
10-02 23:53:31.516 2258-2258/com.nagpal.karan.radarchat E/Xposed: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.View.getContext()' on a null object reference
at tw.fatminmin.xposed.minminguard.blocker.NameBlocking.clearAdViewInLayout(NameBlocking.java:87)
at tw.fatminmin.xposed.minminguard.blocker.NameBlocking$3.afterHookedMethod(NameBlocking.java:150)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:754)
at android.view.LayoutInflater.inflate(<Xposed>)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:740)
at android.view.LayoutInflater.inflate(<Xposed>)
at com.nagpal.karan.radarchat.MyAdapter.onCreateViewHolder(MyAdapter.java:98)
at com.nagpal.karan.radarchat.MyAdapter.onCreateViewHolder(MyAdapter.java:11)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5836)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5060)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3568)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1221)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2097)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1069)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5915)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessag
10-02 23:53:31.517 2258-2258/com.nagpal.karan.radarchat E/Xposed: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.View.getContext()' on a null object reference
at tw.fatminmin.xposed.minminguard.blocker.NameBlocking.clearAdViewInLayout(NameBlocking.java:87)
at tw.fatminmin.xposed.minminguard.blocker.NameBlocking$3.afterHookedMethod(NameBlocking.java:150)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:754)
at android.view.LayoutInflater.inflate(<Xposed>)
at com.nagpal.karan.radarchat.MyAdapter.onCreateViewHolder(MyAdapter.java:98)
at com.nagpal.karan.radarchat.MyAdapter.onCreateViewHolder(MyAdapter.java:11)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5836)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5060)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3568)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1221)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2097)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1069)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5915)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5298)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndAr
10-02 23:53:31.517 2258-2258/com.nagpal.karan.radarchat D/AndroidRuntime: Shutting down VM
10-02 23:53:31.520 2258-2258/com.nagpal.karan.radarchat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nagpal.karan.radarchat, PID: 2258
android.view.InflateException: Binary XML file line #43: Error inflating class de.hdodenhof.circleimageview.CircleImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:740)
at android.view.LayoutInflater.inflate(<Xposed>)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:740)
at android.view.LayoutInflater.inflate(<Xposed>)
at com.nagpal.karan.radarchat.MyAdapter.onCreateViewHolder(MyAdapter.java:98)
at com.nagpal.karan.radarchat.MyAdapter.onCreateViewHolder(MyAdapter.java:11)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5836)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5060)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3568)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1221)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15715)
at android.view.ViewGroup.layout(ViewGroup.java:5046)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2097)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1069)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5915)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameD
10-02 23:53:31.521 2258-2258/com.nagpal.karan.radarchat D/AppTracker: App Event: crash
10-02 23:53:31.870 2258-2258/com.nagpal.karan.radarchat I/Process: Sending signal. PID: 2258 SIG: 9
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="178dp"
android:background="@drawable/ic_action_alarms"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textColor="#ffffff"
android:text="Akash Bangad"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:text="akash.bangad93@gmail.com"
android:textSize="14sp"
android:textStyle="normal"/>
</LinearLayout>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_action_alarms"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"
android:id="@+id/circleView"
/>
答案 0 :(得分:0)
setContentView()
仅在if-else语句的else
部分中调用。这意味着如果你的逻辑进入if
部分,它将开始一个像你一样的新活动但是它会继续,因为你没有完成活动。
在if-else之后,你正在做findViewById()
。如果没有附加到活动的视图,则无法工作。
只需在this.finish()
部分
if
即可
OR
只需取消注释setContentView(R.layout.activity_chats);
,然后将其从else
部分移除。
我推荐第二个选项。如果您打算开始一项新活动,如果您首先附加一个视图,它将不会有所作为。
编辑:
替换它:
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_action_alarms"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"
android:id="@+id/circleView" />
有了这个:
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/circleView"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_action_alarms"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp" />