1

我一直在查看其他几篇文章,并试图整合一种将我的 android 应用程序中的数据发布到我们网站上的 php 页面的方法。基本上这是一个域检查功能,我希望用户能够在应用程序中输入域名,然后当他们单击“立即检查”时,他们会被带到网页并显示结果。我现在不担心在应用程序中显示结果。

这是我到目前为止的代码:

public class Domain_check extends Activity {

public void onCreate(Bundle savedInstanceState) {
    postData();
}

public void postData() {
    EditText domainText = (EditText) findViewById(R.id.hosting_link);
    if (domainText.length()>0)
    {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://www.oursite.com/domainchecker.php");



        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("id",domainText.getText().toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
    else
    {
        // display message if text fields are empty
        Toast.makeText(getBaseContext(),"Please enter a domain name",Toast.LENGTH_SHORT).show();
    }

} 

目前,当我单击按钮进行检查(运行此活动)时,我得到一个 nullPointerError。

我对 Java 还是很陌生,所以这可能是一种相当简单的方法!

谢谢

编辑:

错误:

01-18 11:05:39.720: E/AndroidRuntime(353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oursite/com.oursite.Domain_check}: java.lang.NullPointerException

修改后的代码,代码现在位于绘制输入文本的视图的活动中,而不是单独的活动中:

public class Hosting extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hosting);

   View DomainButton = findViewById(R.id.domain_button);
   DomainButton.setOnClickListener((OnClickListener) this);     
}

public void onClick (View thisView) {
    postData();
}

public void postData() {
    EditText domainText = (EditText) findViewById(R.id.hosting_link);
    if (domainText.length()>0)
    {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://www.oursite.com/domainchecker.php");



        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("id",domainText.getText().toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
    else
    {
        // display message if text fields are empty
        Toast.makeText(getBaseContext(),"Please enter a domain name",Toast.LENGTH_SHORT).show();
    }
}

}

4

1 回答 1

0

setContentView()我在您的活动中没有看到该方法。因此,domainText当您尝试获取长度时,变量为空

于 2012-01-18T11:07:14.280 回答