带有空指针异常的Android setonclicklistener

时间:2015-08-06 07:58:07

标签: java android nullpointerexception onclicklistener

这是我的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错误此代码&gt;&gt;&gt;

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

请帮帮我

4 个答案:

答案 0 :(得分:1)

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

<ul>
    <li>Dummy</li>
    <li>Dummy</li>
    <li>Dummy</li>
    <li><img src="logo.jpg" />
    <li>Dummy</li>
    <li>Dummy</li>
    <li>Dummy</li>  
</ul>

因此任何使用变量sifre1=(EditText)findViewById(R.id.etPass); isim=(EditText)findViewById(R.id.etUserName); sifire1的代码都会抛出空指针异常

答案 1 :(得分:0)

其中一个视图(telefon,sifre1,sifre2 ......)为空。 使用调试器或日志消息来检查哪一个。

答案 2 :(得分:0)

我认为这是sifre1=(EditText)findViewById(R.id.etPass);。您的布局中没有@+id/etPass的视图。这就是为什么当你尝试执行此代码sifre1.getText().toString()时抛出NullPointerException。请检查一下。希望这可以帮助你:)

答案 3 :(得分:0)

在某些情况下,由于您的视图处于另一种布局,因此您可能具有空视图。在这种情况下使用inflate方法。某种:

public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_account);

       View secondLayout = Inflater.inflate(R.layout.secondLayout, null, false);
       View view = secondLayout.findViewById(R.id.yourView);

       // yourView is in secondLayout xml