نوشته شده توسط
farhad_shiri_ex
این پیغام خطا که Null Pointer هست که می تونه دلایل مختلفی داشته باشه! بهتر بود اون قسمت از برنامه تون را هم که مشکل داشت را اینجا قرار بدید تا مشکلتون را رفع شود!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_form_info"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Name"
android:id="@+id/input_name"
android:layout_weight="7" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="phone"
android:hint="Phone No."
android:maxLength="11"
android:id="@+id/input_phone"
android:layout_weight="3"
android:textSize="12sp"/>
</LinearLayout>
<CheckBox
android:text="Share my phone number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:id="@+id/cb_share_phone" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#d63605"
android:layout_marginTop="10dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Email"
android:ems="10"
android:id="@+id/input_email"
android:layout_weight="1" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#2391b9"
android:layout_marginTop="10dp" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:text="Submit"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_submit" />
</LinearLayout>
دستورات استفاده شده در xml و جاوا
package ir.mtoosi.helloworld3_acrivity;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class FormInfo implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
private Activity activity;
private LinearLayout layoutFormInfo;
private EditText inputName;
private EditText inputPhone;
private EditText inputEmail;
private CheckBox cbSharePhone;
private Button btnSubmit;
public FormInfo(Activity activity, int layoutId) {
this.layoutFormInfo = (LinearLayout) activity.findViewById(layoutId);
this.activity = activity;
init();
}
private void init() {
if(layoutFormInfo == null){
return;
}
inputName = (EditText) layoutFormInfo.findViewById(R.id.input_name);
inputPhone = (EditText) layoutFormInfo.findViewById(R.id.input_phone);
inputEmail = (EditText) layoutFormInfo.findViewById(R.id.input_email);
cbSharePhone = (CheckBox) layoutFormInfo.findViewById(R.id.cb_share_phone);
btnSubmit = (Button) layoutFormInfo.findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(this);
cbSharePhone.setOnCheckedChangeListener(this);
}
public EditText getInputName() {
return inputName;
}
public EditText getInputPhone() {
return inputPhone;
}
public EditText getInputEmail() {
return inputEmail;
}
public CheckBox getCbSharePhone() {
return cbSharePhone;
}
public Button getBtnSubmit() {
return btnSubmit;
}
@Override
public void onClick(View view) {
if(view.getId() == btnSubmit.getId()){
String email = inputEmail.getText().toString().trim();
String phone = inputPhone.getText().toString().trim();
String name = inputName.getText().toString().trim();
if(isValidInput(name, phone, email)){
Intent intent = new Intent(activity, SecendActivity.class);
intent.putExtra("name" , name);
if(cbSharePhone.isChecked()){
intent.putExtra("phone", phone);
}
intent.putExtra("email", email);
activity.startActivity(intent);
}
}
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(compoundButton.getId() == cbSharePhone.getId()){
inputPhone.setEnabled(isChecked);
}
}
public boolean isValidInput(String name, String phone, String email) {
if(name == null)
name = inputName.getText().toString().trim();
if(phone == null)
phone = inputPhone.getText().toString().trim();
if(email == null)
email = inputEmail.getText().toString().trim();
if(name.length() < 3){
Toast.makeText(activity, "name should be at least 3 characters", Toast.LENGTH_SHORT).show();
inputName.requestFocus();
return false;
}
if(!phone.isEmpty() && (phone.length() != 11 || !phone.startsWith("09"))){
Toast.makeText(activity, "Wrong Phone Number", Toast.LENGTH_SHORT).show();
inputPhone.requestFocus();
return false;
}
if( email.lastIndexOf('@') <= 0 || !email.contains(".")
|| email.lastIndexOf('.') < email.lastIndexOf('@')
|| email.split("@").length > 2)
{
Toast.makeText(activity, "Wrong Email Address", Toast.LENGTH_SHORT).show();
inputEmail.requestFocus();
return false;
}
return true;
}
}
دستوراتی که استفاده کرده ام اینا هستن ، دقیقا وقتی که برنامه رو اجرا میکنم ، متوقف میشه
و خطای زیر رو هم میده
02-05 16:27:18.304 1773-1773/ir.mtoosi.helloworld3_acrivity E/AndroidRuntime: FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit activity class {ir.mtoosi.helloworld3_acrivity/ir.mtoosi.helloworld3_acrivity.FormInfo}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResu lt(Instrumentation.java:1541)
at android.app.Instrumentation.execStartActivity(Inst rumentation.java:1416)
at android.app.Activity.startActivityForResult(Activi ty.java:3351)
at android.support.v4.app.BaseFragmentActivityApi16.s tartActivityForResult(BaseFragmentActivityApi16.ja va:54)
at android.support.v4.app.FragmentActivity.startActiv ityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activi ty.java:3312)
at android.support.v4.app.FragmentActivity.startActiv ityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:3 522)
at android.app.Activity.startActivity(Activity.java:3 490)
at ir.mtoosi.helloworld3_acrivity.MainActivity$4.onCl ick(MainActivity.java:70)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966 )
at android.os.Handler.handleCallback(Handler.java:615 )
at android.os.Handler.dispatchMessage(Handler.java:92 )
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.jav a:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:553)
at dalvik.system.NativeStart.main(Native Method)
02-05 16:27:48.408 1773-1773/? I/Process: Sending signal. PID: 1773 SIG: 9