gpt4 book ai didi

java - 带有空指针异常的Android setonclicklistener

转载 作者:行者123 更新时间:2023-11-30 01:58:19 27 4
gpt4 key购买 nike

这是我的java代码......

public class Account extends Activity 
{

private Database v1;
EditText telefon,sifre1,sifre2,isim,mail,kod;
Button kaydet;
@TargetApi(11)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#586361")));
getActionBar().setTitle("FindMe");
telefon=(EditText)findViewById(R.id.etTelefon);
sifre1=(EditText)findViewById(R.id.etPass);
sifre2=(EditText)findViewById(R.id.etPass2);
isim=(EditText)findViewById(R.id.etUserName);
mail=(EditText)findViewById(R.id.etMail);
kod=(EditText)findViewById(R.id.PhoneCode);
kaydet=(Button)findViewById(R.id.btnAAccount);

kaydet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
kaydet2(telefon.getText().toString(), sifre1.getText().toString(), sifre2.getText().toString(), isim.getText().toString(), mail.getText().toString(), kod.getText().toString());
}
});


}

public final static boolean isValidEmail(CharSequence target) {
if (TextUtils.isEmpty(target)) {
return false;
} else {
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}


public void kaydet2(String tel, String sifre1, String sifre2, String isim, String mail, String kod) {

if(!kod.contains("+")){
kod="+"+kod;
}

if (tel.isEmpty() || sifre1.isEmpty() || sifre2.isEmpty() || isim.isEmpty() || mail.isEmpty() || kod.isEmpty()) {
Toast.makeText(getApplicationContext(), "All field must be filled...", Toast.LENGTH_SHORT).show();
} else if (!sifre1.equals(sifre2)) {
Toast.makeText(getApplicationContext(), "Passwords don't match...", Toast.LENGTH_SHORT).show();
} else if (!isValidEmail(mail)) {
Toast.makeText(getApplicationContext(), "E-mail address is invalid...", Toast.LENGTH_SHORT).show();
}
else if (!PhoneNumberUtils.isGlobalPhoneNumber(kod+tel)) {
Toast.makeText(getApplicationContext(), "Phone number is invalid...", Toast.LENGTH_SHORT).show();
}
else {
try {
Database v1 = new Database(this);
SQLiteDatabase db = v1.getWritableDatabase();
ContentValues con = new ContentValues();
con.put("Email", mail);
con.put("Name", isim);
con.put("Phone", kod + tel);
con.put("Password", sifre1);
db.insertOrThrow("Account", null, con);
db.close();
Toast.makeText(getApplicationContext(), "Congratulation...", Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
Toast.makeText(getApplicationContext(), "Error occurred in recording...", Toast.LENGTH_SHORT).show();
}
}
}

这是我的布局

<LinearLayout

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/linearLayout"
android:paddingBottom="100dp">

<EditText
android:id="@+id/etMail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="E-Mail"
android:textColorHint="#586361"
android:inputType="text"
android:ems="15"
android:paddingTop="80dp" />
<EditText
android:id="@+id/etUserNameAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-2dp"
android:hint="Name"
android:textColorHint="#586361"
android:paddingTop="20dp" />
<EditText
android:id="@+id/etPassAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-2dp"
android:hint="Password"
android:textColorHint="#586361"
android:password="true"
android:paddingTop="20dp" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-2dp"
android:id="@+id/etPass2"
android:hint="Password"
android:textColorHint="#586361"
android:password="true"
android:paddingTop="20dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:layout_width="100dp"
android:layout_height="50dp"
android:paddingTop="20dp"
android:hint="(+) Code"
android:textColorHint="#586361"
android:id="@+id/PhoneCode"/>

<EditText
android:layout_width="227dp"
android:layout_height="50dp"
android:id="@+id/etTelefon"
android:hint="Phone"
android:textColorHint="#586361"
android:paddingTop="20dp"/>


</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingLeft="90dp"
android:id="@+id/linearLayout2">

<Button
android:layout_width="147dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="4dp"
android:background="#00A3DA"
android:text="Create Account"
android:id="@+id/btnAAccount"
android:layout_below="@+id/etPass"
android:layout_centerHorizontal="true"/>
</LinearLayout>

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Account"
android:id="@+id/MarkaTextView"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:paddingRight="90dp"
android:textColor="#586361"
android:typeface="sans"
android:textSize="25dp"
android:textStyle="bold|normal"
android:fontFamily="@string/app_name"
android:paddingTop="50dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/TxtWarning"
android:textColor="#586361"
android:typeface="sans"
android:textSize="15dp"
android:textStyle="bold|normal"
android:fontFamily="@string/app_name"
android:text="All Field Must Be Filled!"
android:paddingTop="450dp"
android:paddingLeft="92dp"
android:textIsSelectable="false"
android:width="150dp" />

这是我的logcat......

08-06 03:55:23.149    4718-4718/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.emre.findme.Account$1.onClick(Account.java:42)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

我收到 NullPointerException 错误代码 >>>

kaydet2(telefon.getText().toString(), sifre1.getText().toString(), sifre2.getText().toString(), isim.getText().toString(), mail.getText().toString(), kod.getText().toString()); 

请帮帮我

最佳答案

这些项目都没有在您的布局中定义

sifre1=(EditText)findViewById(R.id.etPass);
isim=(EditText)findViewById(R.id.etUserName);

因此任何使用变量 sifire1isim 的代码都会抛出空指针异常

关于java - 带有空指针异常的Android setonclicklistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31850002/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com