问题
My app crashes the moment it creates a UserData, which is a class inside an endpoint library in my project. Can anyone help me figure out why? The line that the app crashes on is UserData userData = new UserData(); which shouldbe totally fine. My log cat is after the code.
package com.indigohate;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
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 android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.*;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.json.jackson.JacksonFactory;
import com.indigohate.userdataendpoint.*;
import com.indigohate.userdataendpoint.model.UserData;
public class UserRegister extends Activity {
String gender;
Button btnRegister;
Button btnLinkToLogin;
EditText inputFirstName;
EditText inputLastName;
EditText inputUsername;
EditText inputEmail;
EditText inputPassword;
EditText inputConfirmPassword;
EditText inputStreetAddress;
EditText inputCity;
EditText inputState;
EditText inputZip;
private UserData userData = null;
//private Userdataendpoint endpoint = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_register);
Log.d("register", "inside oncreate");
btnRegister = (Button) findViewById(R.id.btn_register);
btnLinkToLogin = (Button) findViewById(R.id.btn_link_to_login);
inputFirstName = (EditText) findViewById(R.id.user_firstname);
inputLastName = (EditText) findViewById(R.id.user_lastname);
inputUsername = (EditText) findViewById(R.id.user_username);
inputEmail = (EditText) findViewById(R.id.user_email);
inputPassword = (EditText) findViewById(R.id.user_password);
inputConfirmPassword = (EditText) findViewById(R.id.user_password_confirm);
inputStreetAddress = (EditText) findViewById(R.id.user_street);
inputCity = (EditText) findViewById(R.id.user_city);
inputState = (EditText) findViewById(R.id.user_state);
inputZip = (EditText) findViewById(R.id.user_zip);
// Listening to Login Screen link
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Closing registration screen
// Switching to Login Screen/closing register screen
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
// Close registration View
finish();
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("unused")
@Override
public void onClick(View view) {
//try{
/*
UserData userData = new UserData();
String lastname = inputFirstName.getText().toString();
String firstname = inputLastName.getText().toString();
String username = inputUsername.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
String confirmPassword = inputConfirmPassword.getText().toString();
String streetAddress = inputStreetAddress.getText().toString();
String city = inputCity.getText().toString();
String state = inputState.getText().toString();
String zip = inputZip.getText().toString();
// code to handle registration
userData.setLastname(lastname);
userData.setFirstname(firstname);
userData.setUsername(username);
userData.setStreet(streetAddress);
userData.setCity(city);
userData.setState(state);
userData.setPassword(password);
userData.setZip(zip);
userData.setEmail(email);
//userData.setSex(gender); */
setUserInfo(view);
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
// Close registration View
finish();
}
});
}
public void setUserInfo(View v){
inputFirstName = (EditText) findViewById(R.id.user_firstname);
inputLastName = (EditText) findViewById(R.id.user_lastname);
inputUsername = (EditText) findViewById(R.id.user_username);
inputEmail = (EditText) findViewById(R.id.user_email);
inputPassword = (EditText) findViewById(R.id.user_password);
inputConfirmPassword = (EditText) findViewById(R.id.user_password_confirm);
inputStreetAddress = (EditText) findViewById(R.id.user_street);
inputCity = (EditText) findViewById(R.id.user_city);
inputState = (EditText) findViewById(R.id.user_state);
inputZip = (EditText) findViewById(R.id.user_zip);
Log.d("register", "inside the setUserInfo");
UserData userData = new UserData();
Log.d("register", "before the firstname");
String firstname = inputFirstName.getText().toString();
String lastname = inputLastName.getText().toString();
String username = inputUsername.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
String confirmPassword = inputConfirmPassword.getText().toString();
String streetAddress = inputStreetAddress.getText().toString();
String city = inputCity.getText().toString();
String state = inputState.getText().toString();
String zip = inputZip.getText().toString();
// code to handle registration
Log.d("register","before instantiating userdata");
userData.setLastname(lastname);
userData.setFirstname(firstname);
userData.setUsername(username);
userData.setStreet(streetAddress);
userData.setCity(city);
userData.setState(state);
userData.setPassword(password);
userData.setZip(zip);
userData.setEmail(email);
userData.setSex(gender);
//sendHttpSQLUserData(userData);
//sendUserData(userData);
Log.d("register", "before the async");
new AsyncRegisterUserData().execute(userData);
}
public void onRadioButtonClicked(View view){
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_male:
if(checked)
gender = "male";
break;
case R.id.radio_female:
if(checked)
gender = "female";
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user_register, menu);
return true;
}
public class AsyncRegisterUserData extends AsyncTask<UserData,Void,String>
{
@Override
protected String doInBackground(UserData... userdata)
{
//probably a better way to do this
userData = userdata[0];
HttpClient httpclient = new DefaultHttpClient();
//HttpPost httppost = new HttpPost("http://electric-sheep160.appspot.com/sign");
//HttpPost httppost = new HttpPost("http://localhost:8888/sign");
HttpPost httppost = new HttpPost("http://172.9.69.232/wheelshare/register.php");
String username = userData.getUsername();
String firstname = userData.getFirstname();
String lastname = userData.getLastname();
String password = userData.getPassword();
String email = userData.getEmail();
String street = userData.getStreet();
String city = userData.getCity();
String state = userData.getState();
String sex = userData.getSex();
String zip = userData.getZip();
TextView loginErrorMsg = null;
String str = "before result";
Log.d("register", "before the request");
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("firstname", firstname));
nameValuePairs.add(new BasicNameValuePair("lastname", lastname));
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("street", street));
nameValuePairs.add(new BasicNameValuePair("city", city));
nameValuePairs.add(new BasicNameValuePair("state", state));
nameValuePairs.add(new BasicNameValuePair("zip", zip));
nameValuePairs.add(new BasicNameValuePair("sex", sex));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
//int status = response.getStatusLine().getStatusCode();
str = inputStreamToString(response.getEntity().getContent()).toString();
} catch (ClientProtocolException e) {
loginErrorMsg.setText(e.toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("register", "after the async");
return str;
}
@Override
protected void onPostExecute(String result)
{
//add some kind of verification that the result was correct.
Log.d("register", "before the toast");
Toast.makeText(getApplicationContext(), "Result: " + result, Toast.LENGTH_LONG).show();
}
}
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;
}
public void writeUserDataFile(String value, Context context){
try{
FileOutputStream fOut = context.openFileOutput("userdatafile.txt", Context.MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(value);
// save and close
osw.flush();
osw.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
Here is my logcat :
05-28 05:42:05.999: E/AndroidRuntime(2751): FATAL EXCEPTION: main
05-28 05:42:05.999: E/AndroidRuntime(2751): java.lang.NoClassDefFoundError: com.indigohate.userdataendpoint.model.UserData
05-28 05:42:05.999: E/AndroidRuntime(2751): at com.indigohate.UserRegister.setUserInfo(UserRegister.java:160)
05-28 05:42:05.999: E/AndroidRuntime(2751): at com.indigohate.UserRegister$2.onClick(UserRegister.java:133)
05-28 05:42:05.999: E/AndroidRuntime(2751): at android.view.View.performClick(View.java:4204)
回答1:
The line java.lang.NoClassDefFoundError: com.indigohate.userdataendpoint.model.UserData
indicates that java wasnt able to find the class you want to create with new UserData();
(or an underlying class which is created inside of UserData), which might be caused by a problem with your project setup.
If UserData.java is in another Project or a library.jar, please check if it is correctly imported. In Eclipse: Right click your project -> preferences -> Java Build Path -> Order and Export -> Be sure that "Android Private Libraries" and "Android dependencies" is checked. In 9 of 10 cases, this is the problem with the setup since revision 22
来源:https://stackoverflow.com/questions/16784812/app-crashes-when-creating-a-new-class-from-endpoint-library