2015-06-22 66 views
-4

我看到了许多问题,像我这样的
,我尝试修复我的代码,但失败...
这是我的代码: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 

问题是什么?

+0

1.你错过了'setContentView'。 2.在ui线程上调用网络。 3.使用已弃用的方法 – Raghunandan

+0

没有任何布局的设置,请将Layout for Activity设置为setContentView(R.layout.main); –

+0

是的先生,谢谢 – user1772630

回答

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(); 
     } 
    }); 

承诺,将理解,而不只是将它复制并忘掉它:-)

+0

让我测试它...谢谢 – user1772630

+0

它的工作现在,但当我点击那个按钮注意到 – user1772630

+0

@ user1772630因为你的代码是错误的。你不能在ui线程上进行网络操作。你也需要使用HttpUrlConnection。 http://developer.android.com/reference/org/apache/http/client/HttpClient.html – Raghunandan

2

您在onCreate(...)错过

setContentView(R.layout.yourlayout); 

查看初始化

0

使用

setContentView(YOUR_LAYOUT) 

之后super(savedinstate)方法onCreate()方法。

like。

protected void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 
    setContentView(YOUR_LAYOUT_HERE); 
}