2016-09-14 3 views
0

Мое приложение разбилось, как только я его запустил. Мне нужна помощь. Заранее большое спасибо.Android Studio :: E/AndroidRuntime: FATAL EXCEPTION:

Я новичок в студии Android, не уверен, что я сделал не так.

Ошибка:

09-14 02:55:11.013 4502-4502/com.example.adrian.loginregister E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.example.adrian.loginregister, PID: 4502 
                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adrian.loginregister/com.example.adrian.loginregister.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:148) 
                        at android.app.ActivityThread.main(ActivityThread.java:5417) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
                        at com.example.adrian.loginregister.LoginActivity.onCreate(LoginActivity.java:23) 
                        at android.app.Activity.performCreate(Activity.java:6237) 
                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                        at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                        at android.os.Handler.dispatchMessage(Handler.java:102)  
                        at android.os.Looper.loop(Looper.java:148)  
                        at android.app.ActivityThread.main(ActivityThread.java:5417)  
                        at java.lang.reflect.Method.invoke(Native Method)  
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Ниже приведены LoginActivity.java

package com.example.adrian.loginregister; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class LoginActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 

     final EditText etUsername = (EditText) findViewById(R.id.etUsername); 
     final EditText etPassword = (EditText) findViewById(R.id.etPassword); 
     final Button bLogin = (Button) findViewById(R.id.bLogin); 
     final TextView bRegister = (TextView) findViewById(R.id.bRegister); 

     bRegister.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class); 
       LoginActivity.this.startActivity(registerIntent); 
      } 
     }); 
    } 
} 

Это activty_login.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.adrian.loginregister.LoginActivity"> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/etUsername" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:hint="Username" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/etPassword" 
     android:layout_below="@+id/etUsername" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignRight="@+id/etUsername" 
     android:layout_alignEnd="@+id/etUsername" 
     android:hint="Password" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     android:id="@+id/bLogin" 
     android:layout_below="@+id/etPassword" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Register here" 
     android:id="@+id/tvRegister" 
     android:layout_below="@+id/bLogin" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

Я пытался выяснить, в течение нескольких часов , Кажется, он не работает. Сбой каждый раз, когда я пытаюсь запустить его.

+5

Возможный дубликат [Android - Null pointer Exception - findViewById()] (http://stackoverflow.com/questions/19078461/android-null-pointer-exception-findviewbyid) –

+0

Вы новичок в Java? У вас есть исключение NullPointerException. Также стоит посмотреть - [Что такое исключение NullPointerException] (http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) и [Как читать stacktrace] (http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) –

+0

Затем. 'bRegister' может захотеть быть кнопкой« да »? Обычно вы не устанавливаете прослушиватели кликов в TextViews –

ответ

0

попытаться изменить

final TextView bRegister = (TextView) findViewById(R.id.bRegister); 

строку

final TextView bRegister = (TextView) findViewById(R.id.tvRegister); 

becuase в файле xml вы даете Register TextViewId к tvRegister. вы даете неправильный Id в initliazation.

+0

Большое вам спасибо! я даже не осознавал этого, он решил проблему. Я до сих пор не могу вспомнить, почему я призываю bRegister. еще раз спасибо –