0

我的应用程序在创建 UserData 时崩溃,这是我项目中端点库中的一个类。谁能帮我弄清楚为什么?应用程序崩溃的行是 UserData userData = new UserData(); 这应该完全没问题。我的日志猫在代码之后。

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



}

这是我的日志:

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)
4

1 回答 1

2

该行java.lang.NoClassDefFoundError: com.indigohate.userdataendpoint.model.UserData表明 java 无法找到您要创建的类new UserData();(或在 UserData 中创建的基础类),这可能是由您的项目设置问题引起的。

如果 UserData.java 在另一个 Project 或 library.jar 中,请检查是否正确导入。在 Eclipse 中:右键单击您的项目 -> 首选项 -> Java 构建路径 -> 订购和导出 -> 确保选中“Android Private Libraries”和“Android dependencies”。在 10 例中的​​ 9 例中,这是自修订版 22 以来设置的问题

于 2013-05-28T06:06:25.473 回答