我看到了许多问题,像我这样的
,我尝试修复我的代码,但失败...
这是我的代码:setOnClickListener错误空对象
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! ");
}
}
});
,这是我的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" />
但我得到这个错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
问题是什么?
1.你错过了'setContentView'。 2.在ui线程上调用网络。 3.使用已弃用的方法 – Raghunandan
没有任何布局的设置,请将Layout for Activity设置为setContentView(R.layout.main); –
是的先生,谢谢 – user1772630