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?
答案 0 :(得分:3)
你错过了
setContentView(R.layout.yourlayout);
在View初始化之前的onCreate(...)
中
答案 1 :(得分:0)
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayout);
后插入
您需要向另一个线程中的服务器发出请求。它可能看起来像
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();
}
}
}
接下来,您需要更改按钮处理程序
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();
}
});
承诺会理解,而不仅仅是复制并忘记它: - )
答案 2 :(得分:0)
使用
setContentView(YOUR_LAYOUT)
super(savedinstate)
方法中的onCreate()
方法后
。
等。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(YOUR_LAYOUT_HERE);
}