简体   繁体   English

我的应用程序在Android版本2.3.3上运行良好,但在android 4.2.2上无法运行

[英]My application works fine on Android version 2.3.3 but does not work on android 4.2.2

Here is the JAVA code of my app: 这是我的应用程序的JAVA代码:

public class Main extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new GetUrl().execute(20);

}
private class GetUrl extends AsyncTask<Integer, Integer, BufferedReader> {

    @Override
    protected BufferedReader doInBackground(Integer... params) {
        BufferedReader reader = null;
        try {
            URL url = null;
            url = new URL("http://www.google.com");
            URLConnection conn = url.openConnection();
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        }
        catch (Exception e) {
            TextView tv = (TextView) findViewById(R.id.textView1);
            tv.setText("Error");
        }
        return reader;
    } 
    @Override
    protected void onPostExecute(BufferedReader reader) {
        TextView tv = (TextView) findViewById(R.id.textView1);
        String line = null;

        try {
            line = reader.readLine().toString();    /*This line if removed removes the error. Whenever i try to work with the reader, it stops working for 4.2.2 but works fine for 2.3.3*/
            tv.setText(line);


        }
        catch( Exception e) {
            tv.setText(e.getMessage());
        }

    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Whenever i try to work with the reader, it stops working for 4.2.2 but works fine for 2.3.3. 每当我尝试与阅读器一起使用时,它在4.2.2中停止工作,但在2.3.3中工作正常。 I posted a similar question before and i was asked to solve the problem using asyncTask, but that didn't really help me out. 我之前曾发布过类似的问题,并被要求使用asyncTask解决问题,但这并没有真正帮助我。

You should only be accessing the reader in the doInBackground method, which executes on a background thread. 您应该只在doInBackground方法中访问阅读器,该方法在后台线程上执行。

The onPostExecute method runs on the UI thread, and should only update the UI, not do any other work. onPostExecute方法在UI线程上运行,并且应该仅更新UI,而不做任何其他工作。

So, I'd suggest changing your AsyncTask to 因此,我建议将您的AsyncTask更改为

AsyncTask<Integer, Integer, String> 

move the line 移动线

reader.readLine().toString();

to onPostExecute, return the string from doInBackground, and make the parameter to onPostExecute the string that you read. 到onPostExecute,从doInBackground返回字符串,然后将参数设置为onPostExecute您读取的字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 2.3.3中的NotificationCompat破解,但在Android 4.2.2中运行良好 - NotificationCompat crack in Android 2.3.3 but works well in Android 4.2.2 android app在android 2.3.3中工作正常,但在android 4.1.2中停止 - android app works fine in android 2.3.3 but stops in android 4.1.2 Android 4.2.2无法为我的应用程序加载Google地图 - Android 4.2.2 does not load google maps for my application 我在Android模拟器上执行该应用程序,效果很好,但是当我导出为APK并在我的手机上对其进行测试时,它不起作用 - I execute the application on Android emulator it works fine but when I export to APK and I test it on my mobile it does not work Android代码在Gingerbread上运行良好,但在Nexus 7(含4.2.2)上无法正常运行 - Android code works fine on Gingerbread but breaks on Nexus 7 w/ 4.2.2 为什么我的 android 程序在 4.4.3 版本上运行良好,但在 10.0 上不起作用 - Why my android program works fine on 4.4.3 version but doesnt work on 10.0 Android 2.3.3在希伯来语的“自动完成”中搜索不起作用 - Android 2.3.3 Search in AutoComplete in Hebrew does not work CollapsingToolbarLayout在Android 4.2.2上不起作用 - CollapsingToolbarLayout not work on android 4.2.2 广播接收器在不同版本的Android上的工作方式不同(4.1.1和4.2.2) - Broadcast receiver works differently on different Version of Android (4.1.1 & 4.2.2) 我的应用程序在 android 2.3.3 到 android 3.1 上运行,但在 4.0 + 上因错误而停止 - My App works on android 2.3.3 to android 3.1 but stops with error on 4.0 +
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM