问题描述
我的应用程序崩溃,它会创建一个的UserData,这是在我的项目一个端点磁带库中的一类的时刻。谁能帮我弄清楚为什么?该行对应用程序崩溃是用户数据的UserData =新的UserData();这shouldBe这样完全罚款。我的日志猫是code后。
包com.indigohate;进口java.io.BufferedReader中;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口java.io.OutputStreamWriter中;
进口java.io.UnsupportedEncodingException;
进口的java.util.ArrayList;
进口java.util.Date;
进口的java.util.List;进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Menu;
进口android.view.View;
导入android.widget *。进口com.google.api.client.extensions.android.http.AndroidHttp;
进口com.google.api.client.http.Htt prequest;
进口com.google.api.client.http.Htt prequestInitializer;
进口com.google.api.client.json.jackson.JacksonFactory;导入com.indigohate.userdataendpoint *。
进口com.indigohate.userdataendpoint.model.UserData;
公共类UserRegister延伸活动{
字符串性别;
按钮btnRegister;
按钮btnLinkToLogin;
的EditText inputFirstName;
的EditText inputLastName;
的EditText inputUsername;
的EditText inputEmail;
的EditText控件inputPassword;
的EditText inputConfirmPassword;
的EditText inputStreetAddress;
的EditText inputCity;
的EditText inputState;
的EditText inputZip; 私人用户数据的UserData = NULL;
//私人Userdataendpoint终点= NULL; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_user_register);
Log.d(注册,里面的OnCreate);
btnRegister =(按钮)findViewById(R.id.btn_register);
btnLinkToLogin =(按钮)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); //倾听登录屏幕链接
btnLinkToLogin.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看视图){
//关闭注册画面
//切换到登录屏幕/关闭登记画面
意图I =新意图(getApplicationContext(),LoginActivity.class);
startActivity(ⅰ);
//关闭注册查看
完();
} }); btnRegister.setOnClickListener(新View.OnClickListener(){ @燮pressWarnings(未使用)
@覆盖
公共无效的onClick(查看视图){
//尝试{
/ *
用户数据的UserData =新的UserData();
。字符串姓氏= inputFirstName.getText()的toString();
。字符串名字= inputLastName.getText()的toString();
字符串username = inputUsername.getText()的toString()。
。字符串email = inputEmail.getText()的toString();
。字符串密码= inputPassword.getText()的toString();
字符串confirmPassword = inputConfirmPassword.getText()的toString()。
字符串的StreetAddress = inputStreetAddress.getText()的toString()。
。字符串城市= inputCity.getText()的toString();
字符串状态= inputState.getText()的toString()。
字符串压缩= inputZip.getText()的toString()。
// code办理登记
userData.setLastname(姓氏);
userData.setFirstname(名字);
userData.setUsername(用户名);
userData.setStreet(的StreetAddress);
userData.setCity(市);
userData.setState(州);
userData.setPassword(密码);
userData.setZip(ZIP);
userData.setEmail(电子邮件);
//userData.setSex(gender); * /
setUserInfo(视图); 意图I =新意图(getApplicationContext(),LoginActivity.class);
startActivity(ⅰ);
//关闭注册查看
完();
}
});
} 公共无效setUserInfo(视图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(注册,在里面setUserInfo);
用户数据的UserData =新的UserData();
Log.d(注册,在名字前);
。字符串名字= inputFirstName.getText()的toString();
。字符串姓氏= inputLastName.getText()的toString();
字符串username = inputUsername.getText()的toString()。
。字符串email = inputEmail.getText()的toString();
。字符串密码= inputPassword.getText()的toString();
字符串confirmPassword = inputConfirmPassword.getText()的toString()。
字符串的StreetAddress = inputStreetAddress.getText()的toString()。
。字符串城市= inputCity.getText()的toString();
字符串状态= inputState.getText()的toString()。
字符串压缩= inputZip.getText()的toString()。
// code办理登记
Log.d(注册,用户数据实例化之前);
userData.setLastname(姓氏);
userData.setFirstname(名字);
userData.setUsername(用户名);
userData.setStreet(的StreetAddress);
userData.setCity(市);
userData.setState(州);
userData.setPassword(密码);
userData.setZip(ZIP);
userData.setEmail(电子邮件);
userData.setSex(性别); // sendHttpSQLUserData(用户数据);
// sendUserData(用户数据);
Log.d(注册,异步之前);
新AsyncRegisterUserData()执行(用户数据)。
}
公共无效onRadioButtonClicked(查看视图){
布尔查=((单选)查看).isChecked();
开关(view.getId()){
案例R.id.radio_male:
如果(选中)
性别=男;
打破;
案例R.id.radio_female:
如果(选中)
性别=女;
打破;
}
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.user_register,菜单);
返回true;
} 公共类AsyncRegisterUserData扩展的AsyncTask<的UserData,太虚,字符串>
{
@覆盖
保护字符串doInBackground(...的UserData用户数据)
{
//可能是一个更好的方式来做到这一点
用户数据用户数据= [0];
HttpClient的HttpClient的=新DefaultHttpClient();
// HttpPost httppost =新HttpPost(http://electric-sheep160.appspot.com/sign);
// HttpPost httppost =新HttpPost(HTTP://本地主机:8888 /符号);
HttpPost httppost =新HttpPost(http://172.9.69.232/wheelshare/register.php);
字符串username = userData.getUsername();
字符串名字= userData.getFirstname();
字符串姓氏= userData.getLastname();
字符串密码= userData.getPassword();
字符串email = userData.getEmail();
串街道= userData.getStreet();
字符串城市= userData.getCity();
字符串状态= userData.getState();
串性别= userData.getSex();
字符串压缩= userData.getZip();
TextView的loginErrorMsg = NULL;
字符串str =结果之前;
Log.d(注册,请求之前);
尝试{
清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(2);
nameValuePairs.add(新BasicNameValuePair(名字,名字));
nameValuePairs.add(新BasicNameValuePair(姓氏,姓氏));
nameValuePairs.add(新BasicNameValuePair(用户名的用户名));
nameValuePairs.add(新BasicNameValuePair(密码,密码));
nameValuePairs.add(新BasicNameValuePair(电子邮件,电子邮件));
nameValuePairs.add(新BasicNameValuePair(街,街道));
nameValuePairs.add(新BasicNameValuePair(城市,市));
nameValuePairs.add(新BasicNameValuePair(国家,状态));
nameValuePairs.add(新BasicNameValuePair(ZIP,邮编));
nameValuePairs.add(新BasicNameValuePair(性,性)); httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中)); //执行HTTP POST请求
HTT presponse响应= httpclient.execute(httppost); // INT状态= response.getStatusLine()的getStatus code(); 。STR = inputStreamToString(response.getEntity()的getContent())的toString();
}赶上(ClientProtocolException E){
loginErrorMsg.setText(e.toString());
}赶上(UnsupportedEncodingException五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
Log.d(注册,异步后);
返回海峡;
} @覆盖
保护无效onPostExecute(字符串结果)
{
//添加某种验证这个结果是正确的。
Log.d(注册,敬酒之前);
Toast.makeText(getApplicationContext(),结果:+结果,Toast.LENGTH_LONG).show(); }
}
私人的StringBuilder inputStreamToString(InputStream为){
串线=;
StringBuilder的总=新的StringBuilder();
//环绕式InputStream的一个BufferedReader
RD的BufferedReader =新的BufferedReader(新的InputStreamReader(是));
//读取响应,直到结束
尝试{
而((行= rd.readLine())!= NULL){
total.append(线);
}
}赶上(IOException异常五){
e.printStackTrace();
}
//返回满弦
总回报;
} 公共无效writeUserDataFile(字符串值,上下文的背景下){
尝试{
FileOutputStream中FOUT = context.openFileOutput(userdatafile.txt,Context.MODE_WORLD_READABLE);
OutputStreamWriter OSW =新OutputStreamWriter(FOUT); //写入字符串到文件 osw.write(值); //保存并关闭
osw.flush();
osw.close();
}赶上(IOException异常五){
e.printStackTrace();
}
}}
下面是我的logcat的:
五月5日至28日:42:05.999:E / AndroidRuntime(2751):致命异常:主要
5月五日至28日:42:05.999:E / AndroidRuntime(2751):java.lang.NoClassDefFoundError的:com.indigohate.userdataendpoint.model.UserData
5月五日至28日:42:05.999:E / AndroidRuntime(2751):在com.indigohate.UserRegister.setUserInfo(UserRegister.java:160)
5月五日至28日:42:05.999:E / AndroidRuntime(2751):在com.indigohate.UserRegister $ 2.onClick(UserRegister.java:133)
5月五日至28日:42:05.999:E / AndroidRuntime(2751):在android.view.View.performClick(View.java:4204)
行 java.lang.NoClassDefFoundError的:com.indigohate.userdataendpoint.model.UserData code>表示的java wasnt能找到你想创建一个
新的UserData(类);
(或它的UserData内部创建一个基础类),这可能是由同一个问题引起您的项目设置。
如果UserData.java是在另一个项目或library.jar,请检查它是否正确导入。在Eclipse中:右键单击您的项目 - > preferences - > Java构建路径 - >在线订单和出口 - >确保Android的私家藏书和Android的依赖关系被选中。
在10例9,这是因为与22版本的安装
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)
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
这篇关于创建从端点库一个新的类,当应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!