I saw many questions like mine
and I try to fix my codes but failed...
this is my code:
public class MainActivity extends ActionBarActivity {
EditText usernameEditText;
EditText passwordEditText;
public Button saveme;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
usernameEditText = (EditText) findViewById(R.id.username);
passwordEditText = (EditText) findViewById(R.id.pass);
saveme = (Button) findViewById(R.id.loginn);
saveme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
String givenUsername = usernameEditText.getEditableText().toString();
String givenPassword = passwordEditText.getEditableText().toString();
// CALL GetText method to make post method call
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://fb.facenegah.com/android/login.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", givenUsername));
nameValuePairs.add(new BasicNameValuePair("pass", givenPassword));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
Toast bread = Toast.makeText(getApplicationContext(), responseText, Toast.LENGTH_LONG);
bread.show();
}
catch(Exception ex)
{
// content.setText(" url exeption! " );
}
}
});
and this is my xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="ایمیل/شناسه کاربری"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/username"
android:width="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="رمز عبور"
android:id="@+id/textView2"
android:layout_below="@+id/username"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/pass"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:width="150dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ورود"
android:id="@+id/loginn"
android:layout_below="@+id/pass"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
But I get this error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
What is the issue?
You missed
setContentView(R.layout.yourlayout);
in onCreate(...)
before View initialized
After super.onCreate(savedInstanceState);
insert setContentView(R.layout.YourLayout);
you need to make a request to a server in another thread. It might look like
public class LoginTask extends AsyncTask<Void, Void, String>{
private String username;
private String password;
private Context context;
public LoginTask(Context context, String username, String password) {
this.username = username;
this.password = password;
this.context = context;
}
@Override
protected String doInBackground(Void... voids) {
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://fb.facenegah.com/android/login.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", username));
nameValuePairs.add(new BasicNameValuePair("pass", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
return responseText;
}
catch(Exception ex)
{
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s != null) {
Toast bread = Toast.makeText(context, s, Toast.LENGTH_LONG);
bread.show();
}
}
}
Next, you need to change your button handler
saveme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String givenUsername = usernameEditText.getEditableText().toString();
String givenPassword = passwordEditText.getEditableText().toString();
new LoginTask(MainActivity.this, givenUsername, givenPassword).execute();
}
});
promise that will understand and not just copy it and forget it :-)
use
setContentView(YOUR_LAYOUT)
after super(savedinstate)
method in onCreate()
method.
like.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(YOUR_LAYOUT_HERE);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.