私は、アプリケーションにナビゲーションバーを追加するチュートリアルに従っています。ただし、関連コードを追加する前に、ユーザーを登録した後、ユーザーは自動的にログインし、LoggedInMenuクラスにリダイレクトされます。ナビゲーションバーを作成しようとすると、登録ボタンをクリックするとアプリがクラッシュします。アプリケーションはNav Barを追加しようとした後にクラッシュし続けます
次のように私のマニフェストファイルは次のように私は、ナビゲーションバーを作成するために、自分のコードに変更を加えたところで
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.XXXXXXXXX.computerscienceinduction">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".UserTypeSelection">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegistrationActivity" />
<activity android:name=".LoginActivity" />
<activity android:name=".LoggedInMenu" />
<activity android:name=".GuestMenu" />
<activity android:name=".ListContacts" />
<activity
android:name=".NavDrawerActivity"
android:label="@string/title_activity_nav_drawer"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
マイLoggedInMenuクラスは次のとおりです。
package com.example.XXXXXXXXX.computerscienceinduction;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class LoggedInMenu extends AppCompatActivity implements View.OnClickListener{
private FirebaseAuth firebaseAuth;
private TextView textViewUserEmail;
private Button buttonLogout;
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private String mActivityTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logged_in_menu);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser() == null) {
finish();
startActivity(new Intent(this, LoginActivity.class));
}
FirebaseUser user = firebaseAuth.getCurrentUser();
textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
textViewUserEmail.setText("Welcome " + user.getEmail());
buttonLogout = (Button) findViewById(R.id.buttonLogout);
buttonLogout.setOnClickListener(this);
mDrawerList = (ListView)findViewById(R.id.navList);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
}
private void addDrawerItems() {
String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(LoggedInMenu.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Navigation!");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
public void onClick(View view) {
if(view == buttonLogout) {
firebaseAuth.signOut();;
finish();
startActivity(new Intent(this, LoginActivity.class));
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
チュートリアルではありません私は最初にログイン機能をコーディングするときに使用したAppCompatActivityではなくActionBarActivityを拡張しますが、それが何かの違いをもたらすかどうかはわかりません。
ご協力いただきありがとうございます。
EDIT:次のようにデバッグした後
、スタックトレースは次のとおりです。
あなたがsetupDrawer()メソッドを呼び出しますE/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.XXXXXXXXX.computerscienceinduction, PID: 27827
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.XXXXXXXXX.computerscienceinduction/com.example.XXXXXXXXX.computerscienceinduction.LoggedInMenu}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBarDrawerToggle.syncState()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBarDrawerToggle.syncState()' on a null object reference
at com.example.XXXXXXXXX.computerscienceinduction.LoggedInMenu.onPostCreate(LoggedInMenu.java:135)
at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1216)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
エラースタックトレースは何ですか? – Gudin
こんにちは、私はOPにそれを追加しました – Spartan123