java.lang.RuntimeException:无法在Android Studio

时间:2016-09-24 19:29:46

标签: android-studio nullpointerexception crash

我是Android开发的新手并尝试创建一个简单的注册应用程序,但每次单击Register Here TextView时应用程序都会崩溃。除此之外,我编写登录按钮来尝试调用另一个活动但是在我点击按钮后它没有响应。我努力找到错误,但我不确定我在编码中错过了什么。

这是主要活动

package com.fyp4201.universityguide;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

public class MainActivity extends AppCompatActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TabHost host = (TabHost) findViewById(R.id.tabHost);
    host.setup();

    //Tab 1
    TabHost.TabSpec spec = host.newTabSpec("tab1");
    spec.setContent(R.id.tab1);
    spec.setIndicator("Home");
    host.addTab(spec);

    //Tab 2
    spec = host.newTabSpec("tab2");
    spec.setContent(R.id.tab2);
    spec.setIndicator("Search Courses");
    host.addTab(spec);

    //Tab 3
    spec = host.newTabSpec("tab3");
    spec.setContent(R.id.tab3);
    spec.setIndicator("Search University");
    host.addTab(spec);

    //Tab4
    spec = host.newTabSpec("tab4");
    spec.setContent(R.id.tab4);
    spec.setIndicator("Compare Universities");
    host.addTab(spec);

    //Tab5
    spec = host.newTabSpec("tab5");
    spec.setContent(R.id.tab5);
    spec.setIndicator("Ranking of Universities");
    host.addTab(spec);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@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.
   switch( item.getItemId()){

    //noinspection SimplifiableIfStatement
       case R.id.action_settings: {
        return true;
    }
       case R.id.action_login:{
       Intent intent= new Intent(getApplicationContext(),LoginActivity.class);
       startActivity(intent);
   }
       default:
    return super.onOptionsItemSelected(item);
}
}}

这是登录活动的代码

package com.fyp4201.universityguide;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class LoginActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TextView regis = (TextView) findViewById(R.id.register);
    Button login = (Button) findViewById(R.id.login);

login.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getApplicationContext(), Homeafterlogin.class);
        LoginActivity.this.startActivity(intent);
    }
});


regis.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(LoginActivity.this, Register.class);
        LoginActivity.this.startActivity(intent);
    }
});

}}

这是注册活动的代码

package com.fyp4201.universityguide;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;

public class Register extends AppCompatActivity {
private static final String REGISTER_URL = "http://jrfyp4201.site88.net/volleyRegister.php";
public static final String KEY_USERNAME = "username";
public static final String KEY_NAME = "name";
public static final String KEY_PASSWORD = "password";
public static final String KEY_EMAIL = "email";
public static final String KEY_CONTACTNO = "contactno";

private EditText etname,etuname,etpass,etconpass,etemail,etcontactno;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

     etname= (EditText) findViewById(R.id.name);
     etuname= (EditText) findViewById(R.id.uname);
     etpass= (EditText) findViewById(R.id.pass);
     etconpass= (EditText) findViewById(R.id.conpass);
     etemail= (EditText) findViewById(R.id.email);
    etcontactno= (EditText) findViewById(R.id.contactno);
}

public void onRegisterbtnClick(View view ){
   registerUser();
}


public void registerUser(){
    final String username = etuname.getText().toString().trim();
    final String password = etpass.getText().toString().trim();
    final String name = etname.getText().toString().trim();
    final String email = etemail.getText().toString().trim();
    final String contactno = etcontactno.toString().trim();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(Register.this,response,Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(Register.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put(KEY_USERNAME,username);
            params.put(KEY_PASSWORD,password);
            params.put(KEY_NAME, name);
            params.put(KEY_EMAIL, email);
            params.put(KEY_CONTACTNO, contactno);

            return params;
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
}

这是logcat错误文件

   Process: com.fyp4201.universityguide, PID: 20646
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp4201.universityguide/com.fyp4201.universityguide.Register}: java.lang.NullPointerException
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
                                                                             at android.app.ActivityThread.access$1100(ActivityThread.java:141)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:136)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711)
                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: java.lang.NullPointerException
                                                                             at com.fyp4201.universityguide.Register.onCreate(Register.java:20)
                                                                             at android.app.Activity.performCreate(Activity.java:5340)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313) 
                                                                             at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:136) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711) 
                                                                             at dalvik.system.NativeStart.main(Native Method) 

注册活动的XML文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.fyp4201.universityguide.Register"
tools:showIn="@layout/activity_register">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name :"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editname"
         />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username :"
        android:id="@+id/textView3" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edituname"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password :"
        android:id="@+id/textView4" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/editpass"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Confirm Password :"
        android:id="@+id/textView7" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/editconpass"
       />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="E-mail address :"
        android:id="@+id/textView5" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/editemail"
       />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Contact Number :"
        android:id="@+id/textView6" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/editcontactno"
       />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register"
        android:id="@+id/registerbtn"
        android:layout_gravity="center_horizontal"
        android:onClick="onRegisterbtnClick" />
</LinearLayout>

登录活动的XML文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.fyp4201.universityguide.LoginActivity"
tools:showIn="@layout/activity_login2">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username"
        android:id="@+id/textView1" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/loginuname" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password"
        android:id="@+id/textView" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/loginpass" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/login"
        android:layout_gravity="center_horizontal"
        android:clickable="true"
        android:focusableInTouchMode="false"
        android:onClick="onLoginClick" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Register Here!"
        android:id="@+id/register"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/accent_material_light"
        android:clickable="true"
        android:onClick="onRegisterClick" />

</LinearLayout>

请告诉我错过了什么。谢谢。

2 个答案:

答案 0 :(得分:0)

检查xml文件中的所有ID并在java中更正其名称。 您似乎引用了与此活动无关的另一个xml文件的ID,就像您可能在注册活动中引用login xml文件的用户名字段一样,反之亦然

答案 1 :(得分:0)

Nvm,我解决了这个问题。编码没有问题,只是在更改后应用程序没有更新。在我清理并重建项目后修复它。谢谢。