1

パッケージに com.blah.blah1 という Android アクティビティがあり、別のパッケージに com.blah.blah2 という Java クラスがあります。アクティビティから Java クラスにアクセスしようとしていますが、次のようにエラーがスローされます。

02-06 14:35:03.360: E/dalvikvm(580): メソッド com.blah.blah2.Login.login から参照されるクラス 'org.json.simple.parser.JSONParser' が見つかりませんでした

解決にご協力ください

  • 私は日食を使用しています
  • 私のjarファイルはlibsフォルダーにあります
  • ビルドパスにjson jarを追加しました
  • jar json-simple-1.1.jar を使用しています
  • 私は Android 4.0.3 で作業しています
  • Eclipse Java コンパイラ バージョン 1.6

以下のアクティビティと Java クラスを参照してください。

public class Chat_NewActivity extends Activity {

    private String className = "Chat_NewActivity";

    /** Asyn parameters */
    private String response;

    /** login() parameters */
    LinkedHashMap<String,String> parameters ;
    String responseToAsync = null;
    UrlRequestAndResponse request = null;
    static ResponseParameters param = null;
    static String userId = null;
    static String secureKey = null;
    static String status = null;
    static String sessionId = null;

    /** Called when the activity is first created and loads main.xml content */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(className, "Creating mail layout for the app...");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i(className, "Created");
    }

    /** action for items selected in Preferences menu */
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.settings_menu:
            Intent settingsActivity = new Intent(getBaseContext(),
                    Chat_PreferenceActivity.class);
            startActivity(settingsActivity);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    /** action calling on Start Chat button is clicked */
    public void startChat(View view) {
        Log.i(className, "Start Chat button is clicked");
        Log.i(className, "performing action for staring chat");
        Log.i(className, "performing user validation");
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        Chat_AppInfo.pref_email = sharedPref.getString("email",null);
        if (Chat_AppInfo.pref_email != null && Chat_AppInfo.pref_email.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+") 
                && Chat_AppInfo.pref_email.length() > 0) {

            /** Intent i = new Intent(getApplicationContext(), AppActivity.class); */

            Chat_AppInfo.pref_firstName = sharedPref.getString("firstName",null).toString();
            Chat_AppInfo.pref_lastName = sharedPref.getString("lastName",null).toString();
            EditText getsubject = (EditText) findViewById(R.id.subject);
            Chat_AppInfo.pref_subject = getsubject.getText().toString();
            Log.i(className, "first name : "+Chat_AppInfo.pref_firstName);
            Log.i(className, "last name : "+Chat_AppInfo.pref_lastName);
            Log.i(className, "last name : "+Chat_AppInfo.pref_email);
            Log.i(className, "subject : "+Chat_AppInfo.pref_subject);
            if(Chat_AppInfo.pref_firstName != null || 
                    Chat_AppInfo.pref_lastName != null ||
                    Chat_AppInfo.pref_subject.length() > 0) {
                Log.i(className, "validation completed!!!");
                checkNetworkConnection();
                /**i.putExtra(AppInfo.firstName, get_FirstName_From_Pref);
                i.putExtra(AppInfo.lastName, get_LastName_From_Pref);
                i.putExtra(AppInfo.email, get_MailId_From_Pref);
                i.putExtra(AppInfo.connection_Url, get_MailId_From_Pref);
                i.putExtra(AppInfo.subject, subject);
                startActivity(i);*/
            } else {
                Toast.makeText(getApplicationContext(), "Insufficient Credentials", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(getApplicationContext(), "E-mail is Invalid!!!", Toast.LENGTH_SHORT).show();
        }
    }

    protected void checkNetworkConnection() {
        Log.i(className, "Checking for Network / wi-fi connection!!!");
        ConnectivityManager connMgr = (ConnectivityManager) 
        getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            Log.i(className, "Network connection available!!!");
            new AsyncConnectionEstablisher().execute(Chat_AppInfo.service_url+"LoginAction");
        } else {
            Log.i(className, "Network connection not available!!!");
            Toast.makeText(getApplicationContext(), "Network Connection Not Available", Toast.LENGTH_SHORT).show();
        }
    }

    private class AsyncConnectionEstablisher extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... urls) {
            // params comes from the execute() call: params[0] is the url.
            Log.i(className, "Performing asynchronous connection...");
            String output = null;
            for (String url : urls) {
                output = getOutputFromUrl(url);
            }
            return output;
        }

        private String getOutputFromUrl(String url) {
            String output = null;
            try {
                Login createSessionObj = new Login();
                createSessionObj.login(url);
                output = createSessionObj.sessionId(Chat_AppInfo.service_url+"JoinAction");
                //output = login(url);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return output;
        }
        // onPostExecute displays the results of the AsyncTask.
        protected void onPostExecute(String result) {
            Log.i(className, "response is : "+result);
        }
    }
}

import java.io.IOException;
import java.util.LinkedHashMap;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import android.util.Log;

public class Login {

    private String className = "Login";
    LinkedHashMap<String,String> parameters ;
    String responseToAsync = null;
    UrlRequestAndResponse request = null;
    String response;
    static ResponseParameters param = null;
    static String userId = null;
    static String secureKey = null;
    static String status = null;
    static String sessionId = null;

    public void login(String url) {
        Log.i(className, "Calling login action with service URL : "+url);
        parameters = new LinkedHashMap<String,String>();
        parameters.put("firstname", Chat_AppInfo.pref_firstName);
        parameters.put("lastname", Chat_AppInfo.pref_lastName);
        parameters.put("email", Chat_AppInfo.pref_email);
        parameters.put("subject", Chat_AppInfo.pref_subject);

        request = new UrlRequestAndResponse();
        request.setUrlRequestParameters(url, parameters, null);
        request.convertStreamToJson();
        response = request.getUrlResponseJson();
        try {
            JSONObject json = (JSONObject)new JSONParser().parse(response);
            param.setUserId((String) json.get("userId"));
            param.setSecureKey ((String) json.get("secureKey"));
            param.setStatus((String) json.get("status"));

        } catch (NullPointerException e) {
            System.out.println("Properly set the setUrlRequestParameters(url, parameters, header) , convertStreamToJson() in the request...");
        } catch (ParseException e) {
            System.out.println("Parse the JSON String properly... Or Check the Servlet Response is in JSON format...");
        }

        try {
            userId  = param.getUserId();
            secureKey = param.getSecureKey();
            status = param.getStatus();
            Log.i(className, "{\"userId\":\""+userId+"\",\"secureKey\":\""+secureKey+"\", \"status\":\""+status+"\"} ");
            //sessionId(Chat_AppInfo.service_url+"JoinAction");
        } catch (NullPointerException e) {
            System.out.println("Check the parameters in response...");
        }
    }
}
4

1 に答える 1

3

org.json.simple.parser.JSONParserアプリにクラスが含まれていますか? android http://developer.android.com/reference/org/json/package-summary.htmlのデフォルトのjsonパッケージにはないように見えるので、これらのクラスを手動で含めるようにしてくださいhttp:// code.google.com/p/json-simple/source/browse/trunk/src/org/json/simple/parser/JSONParser.java?r=73

于 2013-02-06T15:10:19.103 に答える