当我点击signup_activ上的按钮启动userdetail_activ时,应用程序崩溃,如果我转换到某些其他活动,应用程序运行良好
这是我的清单文件。
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".login_activ"
android:parentActivityName=".MainActivity" />
<activity
android:name=".signup_activ"
android:parentActivityName=".MainActivity" />
<activity
android:name=".userdetail_activ"
android:parentActivityName=".signup_activ" >
</activity>
<activity android:name=".userdetail_activ2" />
<activity android:name=".infopage_activ"></activity>
</application>
</manifest>
应用程序崩溃后堆栈。
AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ozairmansuri.myapplication, PID: 1986
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ozairmansuri.myapplication/com.example.ozairmansuri.myapplication.userdetail_activ}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:118)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:152)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:200)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:184)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:518)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:189)
at com.example.ozairmansuri.myapplication.userdetail_activ.<init>(userdetail_activ.java:22)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
signup_activ
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class signup_activ extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup_activ);
}
public void UserDet_Switch() {
final Intent intent = new Intent(signup_activ.this, userdetail_activ.class);
startActivity(intent);
return;
}
public void Signup(View view) {
EditText editText = (EditText) findViewById(R.id.editTextUserName);
EditText editText2 = (EditText) findViewById(R.id.editTextPassword);
EditText editText3 = (EditText) findViewById(R.id.editTextConfirmPassword);
String A = editText.getText().toString();
String B = editText2.getText().toString();
String C = editText3.getText().toString();
List<String> errorList = new ArrayList<String>();
while (!isValid_pass(B, C, errorList)) { //checking whether password is valid
for (String error : errorList) {
Toast.makeText(getApplicationContext(), error,
Toast.LENGTH_SHORT).show();
return;
}
}
List<String> errorList2 = new ArrayList<String>();
while (!isValid_usern(A,errorList2)){ //checking whethe user name is valid
for (String error : errorList2) {
Toast.makeText(getApplicationContext(), error,
Toast.LENGTH_SHORT).show();
return;
}
}
SQLhelper dbh =new SQLhelper(this) ;
if(dbh.checkUser(A,B)== true){
Toast.makeText(getApplicationContext(),"user alredy exists",
Toast.LENGTH_SHORT).show();
return;
}
dbh.addUser(A,B); //adding the user to database table 1
Toast.makeText(getApplicationContext(), "account created",
Toast.LENGTH_SHORT).show();
//// TODO: 28-09-2017 intent switch activity (userdetil activity)
UserDet_Switch();
return;
}
//function to check userncme
public static boolean isValid_usern(String usern,List<String> errorList){
Pattern lowerCasePatten = Pattern.compile("[a-z ]");
Pattern digitCasePatten = Pattern.compile("[0-9 ]");
errorList.clear();
boolean flag=true;
if (usern.length() < 8) {
errorList.add("user name lenght must have alleast 8 character !!");
flag=false;
}
if (!lowerCasePatten.matcher(usern).find()) {
errorList.add("user name must have atleast one lowercase character !!");
flag=false;
}
if (!digitCasePatten.matcher(usern).find()) {
errorList.add("user name must have atleast one digit character !!");
flag=false;
}
return flag;
}
//function to check password
public static boolean isValid_pass(String passwordhere, String confirmhere, List<String> errorList) {
Pattern lowerCasePatten = Pattern.compile("[a-z ]");
Pattern digitCasePatten = Pattern.compile("[0-9 ]");
errorList.clear();
boolean flag=true;
if (!passwordhere.equals(confirmhere)) {
errorList.add("password and confirm password does not match");
flag=false;
}
if (passwordhere.length() < 8) {
errorList.add("Password lenght must have alleast 8 character !!");
flag=false;
}
if (!lowerCasePatten.matcher(passwordhere).find()) {
errorList.add("Password must have atleast one lowercase character !!");
flag=false;
}
if (!digitCasePatten.matcher(passwordhere).find()) {
errorList.add("Password must have atleast one digit character !!");
flag=false;
}
return flag;
}
}
userdetail_activ
import android.content.ContentValues;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class userdetail_activ extends AppCompatActivity {
public static final String EXTRA_HEIGHT = "height";
public static final String EXTRA_WEIGHT = "weight";
public static final String EXTRA_GENDER = "gender";
RadioButton rb;
RadioGroup rg ;
Boolean gend =null;
int A,B;
EditText editText = (EditText) findViewById(R.id.editText6);
EditText editText2 = (EditText) findViewById(R.id.editText9);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userdetail_activ);
rg = (RadioGroup) findViewById(R.id.radio_group);
}
public void rb_click(View view){ //radio button function
int radioButtonid = rg.getCheckedRadioButtonId();
rb = (RadioButton) findViewById(radioButtonid);
if(radioButtonid == (R.id.radioButton2)) gend=true; //male is true
else gend = false; //female is false
return;
}
public void continue_userdetail(View view){
A = Integer.parseInt(editText.getText().toString());
B = Integer.parseInt(editText2.getText().toString());
if(gend == null)
{
Toast.makeText(getApplicationContext(), "please select gender",
Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(this, userdetail_activ2.class);
intent.putExtra(EXTRA_HEIGHT,A);
intent.putExtra(EXTRA_WEIGHT,B);
intent.putExtra(EXTRA_GENDER,gend);
startActivity(intent);
return;
}
}
答案 0 :(得分:0)
EditText editText = (EditText) findViewById(R.id.editText6);
EditText editText2 = (EditText) findViewById(R.id.editText9);
除非另有说明,否则切勿在调用Activity
之前调用从super.onCreate()
继承的方法。
永远不要在小部件存在之后调用findViewById()
,例如通过调用setContentView()
创建。
移动这两个字段的初始化,直到setContentView()
中调用onCreate()
为止。