2017-06-23 13 views
0

私はアンドロイドスタジオの初心者です。チュートリアルガイド androidhiveに従っています。しかし、私はLoginActivity.javaの部分に固執しています。著者はそれを解決したいと思っていません。その後、Androidスタジオ:シンボル変数ツールバーが見つかりません

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

コードをコンパイルすることができますが、:事は、私は、コードをコンパイルするときに、

Error:(41, 54) error: cannot find symbol variable toolbar

が現れたとR.idですべてのコードを強調表示し、私はツールバーのコードを削除した場合ということですそれをインストールして電話機で開くと、クラッシュします。私は多くのスレッドを読んだが、彼らはプロジェクトをきれいにするか、またはgradleを再構築すると言いましたが、動作しません。

LoginActivity.java

package com.testapps.basicsinter; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.text.TextUtils; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
import android.widget.Toast; 

import com.google.android.gms.tasks.OnCompleteListener; 
import com.google.android.gms.tasks.Task; 
import com.google.firebase.auth.AuthResult; 
import com.google.firebase.auth.FirebaseAuth; 

public class LoginActivity extends AppCompatActivity { 

    private EditText inputEmail, inputPassword; 
    private FirebaseAuth auth; 
    private ProgressBar progressBar; 
    private Button btnSignup, btnLogin, btnReset; 

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

     //Get Firebase auth instance 
     auth = FirebaseAuth.getInstance(); 

     if (auth.getCurrentUser() != null) { 
      startActivity(new Intent(LoginActivity.this, MainActivity.class)); 
      finish(); 
     } 

     // set the view now 
     setContentView(R.layout.activity_login); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     inputEmail = (EditText) findViewById(R.id.email); 
     inputPassword = (EditText) findViewById(R.id.password); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar); 
     btnSignup = (Button) findViewById(R.id.btn_signup); 
     btnLogin = (Button) findViewById(R.id.btn_login); 
     btnReset = (Button) findViewById(R.id.btn_reset_password); 

     //Get Firebase auth instance 
     auth = FirebaseAuth.getInstance(); 

     btnSignup.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(LoginActivity.this, SignupActivity.class)); 
      } 
     }); 

     btnReset.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class)); 
      } 
     }); 

     btnLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String email = inputEmail.getText().toString(); 
       final String password = inputPassword.getText().toString(); 

       if (TextUtils.isEmpty(email)) { 
        Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show(); 
        return; 
       } 

       if (TextUtils.isEmpty(password)) { 
        Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show(); 
        return; 
       } 

       progressBar.setVisibility(View.VISIBLE); 

       //authenticate user 
       auth.signInWithEmailAndPassword(email, password) 
         .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() { 
          @Override 
          public void onComplete(@NonNull Task<AuthResult> task) { 
           // If sign in fails, display a message to the user. If sign in succeeds 
           // the auth state listener will be notified and logic to handle the 
           // signed in user can be handled in the listener. 
           progressBar.setVisibility(View.GONE); 
           if (!task.isSuccessful()) { 
            // there was an error 
            if (password.length() < 6) { 
             inputPassword.setError(getString(R.string.minimum_password)); 
            } else { 
             Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show(); 
            } 
           } else { 
            Intent intent = new Intent(LoginActivity.this, MainActivity.class); 
            startActivity(intent); 
            finish(); 
           } 
          } 
         }); 
      } 
     }); 
    } 
} 

activity_login.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.testapps.basicsinter.LoginActivity"> 


    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@color/colorPrimary" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:padding="@dimen/activity_horizontal_margin"> 

    <ImageView 
     android:layout_width="@dimen/logo_w_h" 
     android:layout_height="@dimen/logo_w_h" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginBottom="30dp" 
     android:src="@mipmap/ic_launcher" /> 

    <android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/email" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:hint="@string/hint_email" 
      android:inputType="textEmailAddress" 
      android:textColor="@android:color/white" 
      android:textColorHint="@android:color/white" /> 
    </android.support.design.widget.TextInputLayout> 
    <android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/password" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:hint="@string/hint_password" 
      android:inputType="textPassword" 
      android:textColor="@android:color/white" 
      android:textColorHint="@android:color/white" /> 
    </android.support.design.widget.TextInputLayout> 

    <!-- Login Button --> 

    <Button 
     android:id="@+id/btn_login" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dip" 
     android:background="@color/colorAccent" 
     android:text="@string/btn_login" 
     android:textColor="@android:color/black" /> 

    <Button 
     android:id="@+id/btn_reset_password" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dip" 
     android:background="@null" 
     android:text="@string/btn_forgot_password" 
     android:textAllCaps="false" 
     android:textColor="@color/colorAccent" /> 
     <!-- Link to Login Screen --> 

     <Button 
      android:id="@+id/btn_signup" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dip" 
      android:background="@null" 
      android:text="@string/btn_link_to_register" 
      android:textAllCaps="false" 
      android:textColor="@color/white" 
      android:textSize="15dp" /> 
    </LinearLayout> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_gravity="center|bottom" 
     android:layout_marginBottom="20dp" 
     android:visibility="gone" /> 

</android.support.design.widget.CoordinatorLayout> 

のAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.testapps.basicsinter"> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".LoginActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".MainActivity" /> 
     <activity android:name=".SignupActivity"></activity> 
    </application> 

</manifest> 

build.gradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.3" 
    defaultConfig { 
     applicationId "com.testapps.basicsinter" 
     minSdkVersion 16 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.google.firebase:firebase-auth:10.0.1' 
    compile 'com.android.support:appcompat-v7:25.3.1' 
    compile 'com.android.support:design:25.3.1' 
    compile 'com.android.support.constraint:constraint-layout:1.0.2' 
    testCompile 'junit:junit:4.12' 
} 
apply plugin: 'com.google.gms.google-services' 
+0

正しいのGradleファイル –

+0

をアップロードしてください私は、IDのツールバーにコントロールを追加するにはどうすればよいのidツールバー –

+0

で任意のコントロールが表示されませんか?ありがとう。 –

答えて

1

here によれば、レイアウトにツールバーを含める必要があります。

最初にツールバーのレイアウトを作成します。
my_toolbar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:minHeight="?attr/actionBarSize" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:titleTextColor="@android:color/white" 
     android:background="?attr/colorPrimary"> 
    </android.support.v7.widget.Toolbar> 

<!-- Layout for content is here. This can be a RelativeLayout --> 

</LinearLayout> 

次に次にあなたがToolbarを使用することができます

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.testapps.basicsinter.LoginActivity"> 

<include 
     layout="@layout/my_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

<!--Rest of your layout content--> 

</android.support.design.widget.CoordinatorLayout> 

(あなたのケースactivity_login.xmlに)あなたのレイアウトに含めます。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
+0

申し訳ありませんが、コード自体には問題があります。作者がコードをページに貼り付けるだけなので、問題です。まだ非常に詳細な答え:)しかし、あなたの時間をありがとう。 –

0

activity_login.xmlであなたのUI要素のどれもが、あなたはここで言及されているID toolbarを持っていないようです:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

また、私は実際には、あなたが見せたかったとき、あなたはマニフェストファイルを繰り返したと信じていますあなたの質問の説明にgradleファイル:)。

+0

申し訳ありませんが、私の間違いは、今編集。どのようにIDの 'ツールバー'を追加するのですか?私はかなりjusはチュートリアルのコードをコピーして貼り付けているからです。 –

0

activity_login.xmlにツールバー要素がありません。ツールバーのコードは

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay" 
    app:elevation="0dp"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimaryDark" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

です。その他のエラーが発生した場合は、次のようにコメントしてください。

+0

どうすれば元に戻すことができますか?ありがとうございました。 –

+0

'activity_login.xml'を開き、ツールバー要素を追加します。チュートリアルを見ると、ツールバー要素のコードは 'activity_main.xml'の下にあります。コードの編集された答えを参照してください。 – Abhi

+0

これがあなたの質問に答えた場合は、同意してください。 – Abhi

関連する問題