我得到了“注册无法解决我的代码中的类型错误,我无法弄清楚为什么。我有一种强烈的感觉,这是一个Eclipse bug,除非有人能看到别的东西。我只是随机得到这个编辑另一个页面时出错,所以我不知道为什么我会这样做。
我尝试清理项目甚至重建项目,仍然出现此错误。
以下是Eclipse中错误的屏幕截图:
更奇怪的是,我在输入代码时尝试查看可用的类,并且Registration.java没有显示在可用列表中:
有关如何让它出现在该列表中的任何想法?
这是我的代码:
package com.smeet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import com.sencide.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidLogin extends Activity implements OnClickListener {
Button ok,back,exit;
TextView result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Login button clicked
ok = (Button)findViewById(R.id.btn_login);
ok.setOnClickListener(this);
result = (TextView)findViewById(R.id.tbl_result);
}
public void postLoginData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
// login.php returns true if username and password match in db
HttpPost httppost = new HttpPost("http://www.somewebsite.com/android/login.php");
try {
// Add user name and password
EditText uname = (EditText)findViewById(R.id.txt_username);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.txt_password);
String password = pword.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("SENCIDE", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("SENCIDE", str);
if(str.toString().equalsIgnoreCase("true"))
{
Log.w("SENCIDE", "TRUE");
result.setText("Login Successful! Please Wait...");
}else
{
Log.w("SENCIDE", "FALSE");
result.setText(str);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
//clicking register button will start up Register xml
public void RegisterButton(View view) {
Intent intent = new Intent(this, Registration.class);
startActivity(intent);
}
@Override
public void onClick(View view) {
postLoginData();
// turns the text in the textview "Tbl_result" into a text string called "tblresult"
TextView tblresult = (TextView) findViewById(R.id.tbl_result);
// If "tblresult" text string matches the string "Login Successful! Please Wait..." exactly, it will switch to next activity
if (tblresult.getText().toString().equals("Login Successful! Please Wait...")) {
Intent intent = new Intent(this, Homepage.class);
EditText uname2 = (EditText)findViewById(R.id.txt_username);
String username2 = uname2.getText().toString();
intent.putExtra("username2", username2);
startActivity(intent);
}
}
}
我的宣言:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sencide"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name="com.sencide.AndroidLogin"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.sencide.NextActivity"
android:label="@string/title_activity_next" >
</activity>
<activity
android:name="com.sencide.Registration"
android:label="@string/title_activity_registration" >
</activity>
<activity
android:name="com.sencide.Homepage"
android:label="@string/title_activity_next_screen" >
</activity>
</application>
</manifest>
这是我的Registration.java
package com.smeet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.sencide.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
class RequestTask extends AsyncTask<String, String, String>{
String responseString = null;
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = null;
responseString = out.toString();
Log.d("check response", responseString);
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
String result = null;
public class Registration extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
TextView detail = (TextView)findViewById(R.id.resulttext);
detail.setText(result);
}
public void SubmitRegistration(View view) {
// assign text in fields to string values
EditText first = (EditText)findViewById(R.id.first);
String first2 = first.getText().toString();
EditText last = (EditText)findViewById(R.id.last);
String last2 = last.getText().toString();
EditText display = (EditText)findViewById(R.id.display);
String display2 = display.getText().toString();
//calculates the number of characters in the display field
int length2 = display2.length();
EditText email = (EditText)findViewById(R.id.email);
String email2 = email.getText().toString();
EditText password = (EditText)findViewById(R.id.password);
String password2 = password.getText().toString();
EditText vpassword = (EditText)findViewById(R.id.vpassword);
String vpassword2 = vpassword.getText().toString();
//calculates the number of characters in the password field
int length = vpassword2.length();
// verifying the following in order: Passwords match? A Password field is empty?
//Password and Display Name less than 6 characters long? Email contains an @ sign and a period?
if(!vpassword2.equals(password2))
{
Toast.makeText(getApplicationContext(), "Passwords do not match!", Toast.LENGTH_SHORT).show();
}
else if (password2.isEmpty() || vpassword2.isEmpty()){
Toast.makeText(getApplicationContext(), "Password field is empty", Toast.LENGTH_SHORT).show();
}
else if (length < 6 || length2 < 6 ) {
Toast.makeText(getApplicationContext(), "Password and Display Name must be at least 6 characters long", Toast.LENGTH_LONG).show();
}
else if (!email2.contains("@") || !email2.contains(".")){
Toast.makeText(getApplicationContext(), "Must enter valid email address.", Toast.LENGTH_SHORT).show();
}
else {
//send php with all the data to server for validation and insertion into table
new RequestTask().execute("http://www.somewebsite.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&dispname=" + display2 + "&email=" + email2 + "&password=" + password2 );
}
}
}
}
答案 0 :(得分:1)
根据您帖子中的评论和图片,我怀疑您有Registration.java
public class Register ...
何时应该
public class Registration ...
此外,既然你已经添加了代码,注册类应该是独立的,而不是嵌套在RequestTask中。
答案 1 :(得分:0)
Registration
课程位于GCM backend module
。如果你使用Android Studio,那么集成是无缝的,但是在Eclipse中你需要手动做一些技巧才能让它工作。