我正在尝试实现登录 Activity 。在登录 Activity 中,我必须输入手机号码和密码,然后单击登录按钮。
这是我的activity_login.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background2"
tools:context="com.myayubo.LoginActivity"
android:fillViewport="false">
<RelativeLayout
android:id="@+id/RelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp">
<LinearLayout
android:id="@+id/LinearLayouttxtayubo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textAyubo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ayubo"
android:layout_gravity="center"
android:typeface="normal"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="35dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayouttxtexplorer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/LinearLayouttxtayubo"
android:orientation="vertical">
<TextView
android:id="@+id/textExplore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explore places nearby you"
android:layout_centerHorizontal="true"
android:typeface="serif"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:textSize="12dp"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:layout_marginTop="224dp"
android:layout_below="@+id/RelativeLayout1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<EditText
android:id="@+id/mobileNo"
android:layout_width="250dp"
android:layout_height="30dp"
android:hint="Mobile Number"
android:textColorHint="#FFFFFF"
android:gravity="center"
android:inputType="phone"
android:layout_gravity="center"
android:background="@drawable/shape"
android:layout_marginTop="10dp"
android:textSize="15dp"
>
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="250dp"
android:layout_height="30dp"
android:hint="Password"
android:textColorHint="#FFFFFF"
android:inputType="textPassword"
android:layout_gravity="center"
android:background="@drawable/shape"
android:layout_marginTop="10dp"
android:gravity="center"
android:textSize="15dp"/>
<Button
android:id="@+id/loginBtn"
android:text="Login"
android:layout_width="260dp"
android:layout_height="38dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:alpha="0.5" />
<Button
android:id="@+id/fbbtn"
android:text="Connect with Facebook"
android:layout_width="250dp"
android:layout_height="26dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#ADD8E6"
android:alpha="0.5"
/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fbbtn"
android:layout_marginTop="20dp"
android:text="or Sign Up"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
这是我的LoginActivity.java
package com.myayubo;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.myayubo.services.MessageHandler;
import com.myayubo.services.ServiceHandler;
import org.json.JSONObject;
public class LoginActivity extends ActionBarActivity implements View.OnClickListener {
public final static String URL = "";
public static String Uid;
private EditText contact;
private EditText password;
private Button login;
private Button fbBtn;
private TextView signUp;
private boolean errorStatus;
private ServiceHandler sh = new ServiceHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
contact = (EditText) findViewById(R.id.mobileNo);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.loginBtn);
fbBtn = (Button) findViewById(R.id.fbbtn);
signUp = (TextView) findViewById(R.id.signUp);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!ServiceHandler.isOnline(getApplicationContext())){
MessageHandler.showMessage("No network connection",
getApplicationContext());
}
new Thread(new Runnable() {
public void run() {
Looper.prepare();
String mobile = contact.getText().toString();
String passwrd = password.getText().toString();
if (mobile.length() == 0){
runOnUiThread(new Runnable() {
public void run() {
MessageHandler.showMessage(
"Please Enter Your Contact Number",
getApplicationContext());
errorStatus = true;
}
});
;
}
if (passwrd.length() == 0){
runOnUiThread(new Runnable() {
public void run() {
MessageHandler.showMessage(
"Please Enter Your Password",
getApplicationContext());
errorStatus = true;
}
});
;
}
String jsonStr = null;
if (!errorStatus) {
if (!ServiceHandler.isOnline(getApplicationContext())) {
MessageHandler.showMessage("No network connection",
getApplicationContext());
} else {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// notify user you are online
try{
runOnUiThread(new Runnable() {
public void run() {
}
});
;
jsonStr = sh.makeServiceCall(URL + "/" + mobile + "/"
+ passwrd, ServiceHandler.GET);
System.out.println(URL + "/" + mobile + "/"
+ passwrd);
}
catch (Exception e){
runOnUiThread(new Runnable() {
public void run() {
MessageHandler.showMessage("No network connection",
getApplicationContext());
}
});
;
}
}
if (jsonStr != null) {
String status = "";
String msg = "";
try {
JSONObject jsonObj = new JSONObject(jsonStr);
runOnUiThread(new Runnable() {
public void run() {
}
});
;
if (jsonObj != null
&& jsonObj.has("status")) {
status = jsonObj.getString("status");
msg = jsonObj.getString("message");
if(jsonObj.has("uid"))
Uid = jsonObj.getString("uid");
System.out.println(jsonObj);
if (status.equals("OK")) {
Intent myIntent = new Intent(
getBaseContext(),
ExtractMenu.class);
startActivityForResult(myIntent, 0);
} else if (status.equals("ERROR")) {
final String errorMsg = msg;
runOnUiThread(new Runnable() {
public void run() {
MessageHandler
.showMessage(
errorMsg,
getApplicationContext());
}
});
;
} else {
runOnUiThread(new Runnable() {
public void run() {
MessageHandler
.showMessage(
"Oops..! something wrong with the service. Please try again Later.",
getApplicationContext());
}
});
;
}
}
} catch (Exception e) {
System.out
.println("Creation of json object failed");
}
}
}
}
}).start();
}
});
signUp.setOnClickListener(this);
}
@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_login, 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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(this, sign_up.class);
startActivity(intent);
}
}
这里好像出了点问题。因为,当我尝试运行此代码时,我的应用程序崩溃了。它显示以下错误。
java.lang.RuntimeException:无法启动 Activity ComponentInfo{com.myayubo/com.myayubo.LoginActivity}:java.lang.NullPointerException
但是,如果没有注册按钮,它就可以工作。
这是我的日志猫。
11-05 10:36:58.359 27532-27532/com.myayubo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myayubo, PID: 27532
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myayubo/com.myayubo.LoginActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.myayubo.LoginActivity.onCreate(LoginActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
请告诉我如何处理此错误。
提前致谢。
-编辑-
这是 list 文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myayubo"
android:versionCode="1"
android:versionName="1.0" >
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--
To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. This disconnect
option is required to comply with the Google+ Sign-In developer policies
-->
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps" >
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
<activity
android:name=".Splash"
android:label="Ayubo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ExtractMenu"
android:label="@string/title_activity_extract_menu" >
</activity>
<activity
android:name=".sign_up"
android:label="@string/title_activity_sign_up" >
</activity>
</application>
</manifest>
最佳答案
您收到 NPE 是因为您尝试添加不在 xml 文件中的 View
signUp = (TextView) findViewById(R.id.signUp);
您的 xml 中没有具有此 id 的 TextView 。检查一下,如果删除此代码,您的代码将完美运行
关于java - 由于发生无法启动 Activity ComponentInfo 错误,无法将注册按钮重定向到注册页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33537694/