仅在安装APK时出现错误

时间:2016-12-06 14:48:00

标签: android android-install-apk illegalaccessexception

我在udacity教程后制作了一个简单的应用程序:https://classroom.udacity.com/courses/ud853。 源代码:https://github.com/ionutincau/Vremea

如果我通过USB调试模式(在Android Studio中运行选项)安装应用程序,它可以正常工作,但如果我“构建APK”并安装内置的apk,应用程序崩溃。

我有互联网连接,但当我尝试从http://api.openweathermap.org获取数据时,我收到了网址Connection Error java.io.FileNotFoundException。而且,当我尝试打开设置时,我得到:

FATAL EXCEPTION: main
Process: com.example.ionut.vremea2, PID: 6850
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ionut.vremea2/com.example.ionut.vremea2.SettingsActivity}: java.lang.IllegalAccessException:

只有在从apk文件中安装应用程序

时才会收到错误

SettingsActivity

class SettingsActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
    }
}

我正在使用此代码从互联网上获取数据:

    protected String[] doInBackground(String... params) {
        if (params.length == 0) {
            return null;
        }

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;
        String forecastJsonStr = null;

        String format = "json";
        String units = "metric";
        int numDays = 7;
        String api_key = "my_key"; // I have a valid code in my app

        try {
            // Construct the URL for the OpenWeatherMap query
            final String FORECAST_BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?";
            final String QUERY_PARAM = "id";
            final String FORMAT_PARAM = "mode";
            final String UNITS_PARAM = "units";
            final String DAYS_PARAM = "cnt";
            final String APPID_PARAM = "APPID";

            // Construct the URL for the OpenWeatherMap query
            Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
                    .appendQueryParameter(QUERY_PARAM, params[0])
                    .appendQueryParameter(FORMAT_PARAM, format)
                    .appendQueryParameter(UNITS_PARAM, units)
                    .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
                    .appendQueryParameter(APPID_PARAM, api_key)
                    .build();

            URL url = new URL(builtUri.toString());

            // Create the request to OpenWeatherMap, and open the connection
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            // Read the input stream into a String
            InputStream inputStream = urlConnection.getInputStream();
            // ... do something

1 个答案:

答案 0 :(得分:0)

创建活动的问题是因为您需要公开他们的课程。

public class SettingsActivity extends Activity {
}

对于filenotfound,您可以在浏览器中打开链接并查看实际响应: {" cod":" 502"," message":"错误:找不到城市"}

您需要对其进行配置以获得正确的响应,但您还应该通过针对HttpURLConnection.HTTP_OK检查urlConnection.getResponseCode()来处理错误响应。如果失败,您将获得有关urlConnection.getResponseMessage()或流urlConnection.getErrorStream()的更多信息。

if (urlConnection.getResposeCode() == HttpURLConnection.HTTP_OK) {
...
} else {
...
}