【问题标题】:Cannot Start a Method In a Different Class Using a Library无法使用库在不同的类中启动方法
【发布时间】:2013-08-27 14:49:40
【问题描述】:

我刚刚为我构建的 Android 应用程序创建了我的第一个库(我有我需要在未来跨不同应用程序重用的代码)并且我需要从库中启动我在主项目中的方法- 但是,当我尝试使用以下行时:

com.project.sample.datasettings.UpdateActivity.success();

我收到一个编译器错误说明:

com.project.sample.UpdateActivity Cannot Be Resolved To A Type

来源:

package com.project.sample.networktasklibrary;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLException;
import com.project.sample.networktasklibrary.XmlParserHandlerFinal;
import com.project.sample*;

import org.xml.sax.SAXException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

// 这个类在后台执行对webservice的调用

public class NetworkTask extends AsyncTask<String, String, InputStream> {
    private static final String LOG_TAG = "STDataSettings";
    private static final String TAG_RESULT = "success";
    private static InputStream stream;

    @Override
    protected InputStream doInBackground(String... params) {

        try {
            stream = getQueryResults("https://dl.dropboxusercontent.com/u/31771876/GetPhoneSettings-ST-rsp-eng.xml");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return stream;
    }

    /*
     * Sends a query to server and gets back the parsed results in a bundle
     * urlQueryString - URL for calling the webservice
     */
    protected static synchronized InputStream getQueryResults(
            String urlQueryString) throws IOException, SAXException,
            SSLException, SocketTimeoutException, Exception {
        Bundle queryResults = new Bundle();

        HttpsURLConnection https = null;
        String uri = urlQueryString;
        URL urlo = new URL(uri);

        https = (HttpsURLConnection) urlo.openConnection();
        https.setConnectTimeout(50000); // 20 second timeout
        https.setRequestProperty("Connection", "Keep-Alive");
        try {
            https = (HttpsURLConnection) urlo.openConnection();

            if ("gzip".equals(https.getContentEncoding())) {
                stream = new GZIPInputStream(stream);
            } else
                stream = https.getInputStream();

        } catch (SSLException e) {
            Log.e(LOG_TAG, e.toString());
            e.printStackTrace();

        } catch (SocketTimeoutException e) {
            Log.e(LOG_TAG, e.toString());
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(LOG_TAG, e.toString());
            e.printStackTrace();

        } catch (Exception e) {
            Log.e(LOG_TAG, e.toString());
            e.printStackTrace();

        } finally {

        }

        String queryResult = null;
        queryResults.putString(TAG_RESULT, queryResult);

        return stream;
    }

    public InputStream getInputStream() {
        return stream;
    }

    protected void onPostExecute(InputStream queryResults) {
        // TODO Auto-generated method stub
        super.onPostExecute(queryResults);
        com.project.sample.datasettings.UpdateActivity.success();
    }
}

更新活动代码示例:

public void success() {

        // to parse the response
        try {
            handler.getQueryResponse(stream);

        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // to set method to save the ArryaLists from the parser
        setArrayList();
        Intent i = new Intent(this, ConfigFinalActivity.class);
        startActivity(i);

    }

【问题讨论】:

  • 请复制并粘贴确切的错误,而不是重新输入。您的错误消息中至少有一个错误。我们还需要查看您的整个类文件,而不仅仅是其中的一部分。
  • com.project.sample.datasettings.UpdateActivity.success(); 是静态方法吗,您是否导入了UpdateActivity
  • 你刚才是不是说 LIBRARY 正在尝试调用 MAIN 应用程序中的某些内容?只是我还是这听起来不像一个库......如果它依赖于您的主应用程序中的代码,它不会在其他应用程序上重新运行。库在主应用程序中调用代码的唯一方法是通过某种接口。
  • 谢谢大家!我在上面更新了我的来源(如果您需要其他任何内容,请告诉我)
  • @Cruncher - 我只需要一种库和主应用程序进行通信的方式 - 库从 XML 文件中获取数据 - 我只需要允许主应用程序使用它(我是从中添加一个包含我尝试使用的 success() 方法的 sn-p - 也许它有助于阐明解决方案)

标签: java android shared-libraries


【解决方案1】:

您是否曾尝试将库添加到主项目中? 右键单击项目名称 -> 属性 -> Android -> 添加。然后选择库项目。

【讨论】:

  • 还有其他建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 2011-02-21
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 2012-04-12
相关资源
最近更新 更多