小编Mr.*_*.Me的帖子

Service中的NetworkOnMainThreadException

我得到一个NetworkOnMainThreadException在我的Service类,它本质上是没有意义的,因为服务是后台进程,据我了解.

在Service方法中,我正在调用静态帮助器方法来下载数据.我也在使用DefaultHttpClient.

这里发生了什么?

android networkonmainthread

4
推荐指数
1
解决办法
3355
查看次数

如果存在,则绑定到服务

我正在开发一个项目,如果该服务正在运行,则需要一个活动来连接到本地服务,如果它没有运行则启动它.这种方法的适用标志是什么.

android android-intent android-service android-activity

4
推荐指数
1
解决办法
2025
查看次数

TextView作为带有textcolor minipulation的进度条?

我正在努力改进我的应用程序的用户界面.在我正在使用的设计中,我有一个TextView,它将在某个时间充当进度条.ruslt应如下所示:

在此输入图像描述

问题是文本的一部分会在进度发生变化时改变颜色

我在android中查看了spannablestring,如果我能找到进度所涵盖的字母,它会起作用(但我不认为这很容易/准确)

我现在计划的是使用以下内容:

  • FrameLayout:
  • 背景textView与绿色文本和白色背景.
  • 前面的Linearlayout,在进行中改变宽度.
  • TextView与绿色背景和匹配framelayout宽度的白色文本.

有更好的方法吗?

user-interface animation android android-layout android-progressbar

4
推荐指数
1
解决办法
2304
查看次数

AVSpeechSynthesizer支持哪些语言?

AVSpeechSynthesizer似乎支持多代表的英语(britsh,我们......)

它支持其他语言吗?法语,葡萄牙语......某处有这些语言的列表吗?

objective-c ios ios7 avspeechsynthesizer

4
推荐指数
1
解决办法
3068
查看次数

NetworkOnMainThreadException从Web上读取时

当我尝试从网站上读取单行文本时我得到错误(NetworkOnMainThreadException).我尝试了一些东西但到目前为止没有任何作用.所以这里是代码,如果有人可以帮助.在清单中我有互联网许可,所以不应该是问题.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Weather extends Activity {

    Button button;
    TextView t;
    String result;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        setContentView(R.layout.weather);

        t = (TextView)findViewById(R.id.textView1);

    }   

    public void myButtonClickHandler (View view) throws ClientProtocolException, IOException {
        result = getContentFromUrl("http://url.com");
        t.setText(result);
    }

    public static String getContentFromUrl(String url) throws ClientProtocolException, IOException {

        HttpClient httpClient = …
Run Code Online (Sandbox Code Playgroud)

android networkonmainthread

3
推荐指数
2
解决办法
5751
查看次数

Android HttpClient:NetworkOnMainThreadException

我在下面有一些代码:

protected void testConnection(String url) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    ResponseHandler<String> responsehandler = new BasicResponseHandler();

    try {
        String connection = httpclient.execute(httpget, responsehandler);
        Toast.makeText(getBaseContext(), R.string.connection_succeed, Toast.LENGTH_SHORT).show();
        view_result.setText(connection);
    } catch(IOException e) {
        Toast.makeText(getBaseContext(), R.string.connection_failed, Toast.LENGTH_SHORT).show();
    }
    httpclient.getConnectionManager().shutdown();
}
Run Code Online (Sandbox Code Playgroud)

并在Menifest中添加权限:

<uses-permission android:name="android.permission.INTERNET"/>
Run Code Online (Sandbox Code Playgroud)

但它有一个例外:NetworkOnMainThreadException,我该怎么办?

android networkonmainthread

3
推荐指数
1
解决办法
8014
查看次数

Xcode UI测试无法找到以编程方式添加的视图

所以我以编程方式在我的视图中添加了一个子视图,当我这样做时,我将所有可访问性参数附加到它:

  [labelView setAccessibilityLabel:@"label"];
  [labelView setIsAccessibilityElement:YES];
  [labelView setUserInteractionEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

但是当我像这样查询UI时:

 let app = XCUIApplication()
 app.staticTexts["label"]
Run Code Online (Sandbox Code Playgroud)

测试失败,因为它无法找到视图.

知道如何处理这个问题,如何为UI测试提供动态添加的视图?

xcode ui-testing ios xcode7 xcode-ui-testing

3
推荐指数
1
解决办法
2266
查看次数

在android中添加发光到TextView

我正在开展一个项目,我应该使用具有发光背景的文本,就像这样 在此输入图像描述

有任何想法吗 ?

java android android-layout

2
推荐指数
1
解决办法
4364
查看次数

Android应用中出现奇怪的NetworkOnMainThreadException?

想法是开始聊天.所以我在班上有这个属性:

private MulticastSocket so;
private EditText messageBoard;
private InetAddress serverAddress;
private int port;
Run Code Online (Sandbox Code Playgroud)

然后我在onCreate()方法中有这个代码:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    // connect to server
    connect();

    // Associate a variable with the Button on the interface
    final Button sendButton = (Button) findViewById(R.id.button2);
    sendButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // when the button is clicked the next screen is loaded
            sendMessage();
        }
    });

}   // end of onCreate
Run Code Online (Sandbox Code Playgroud)

这是我的connect()方法: …

java android network-programming networkonmainthread

2
推荐指数
1
解决办法
5473
查看次数

避免NetworkOnMainThreadException

我知道GUI线程上不允许网络操作.对我没问题.但为什么在Dialog按钮单击回调上使用此代码仍然产生NetworkOnMainThreadException

new Thread(new Runnable() {                         
  @Override
  public void run() {
    heavyAndTimeConsumingOperation();
  }
}).run();
Run Code Online (Sandbox Code Playgroud)

也许我不会以这种方式产生一个新线程?

networking multithreading android exception networkonmainthread

1
推荐指数
1
解决办法
2238
查看次数

在以3.0+为目标时,在runnable中获取NetworkOnMainThreadException

由于ActionBarSherlock的问题,我将我的Manifest更改为目标API 16,从那时起我检查当前正在播放的歌曲的处理程序不再有效.它在我下面标记的行上抛出NetworkOnMainThreadException.

我究竟做错了什么?我以为我的多线程设置正确.

这是我的代码:

    handler = new Handler();

    updateSongTask = new Runnable() {
        public void run() {
            Log.d("asdf", "scraper started");
            Scraper scraper = new ShoutCastScraper(); // THIS LINE throws the exception
            List<Stream> streams;
            try {
                streams = scraper.scrape(new URI(url));
                for (Stream stream : streams) {                     
                    Intent songIntent = new Intent(CUSTOM_INTENT);

                    String[] songAndArtist = songAndArtist(stream.getCurrentSong());
                    songIntent.putExtra("song", songAndArtist[0]);
                    songIntent.putExtra("artist", songAndArtist[1]);
                    songIntent.putExtra("stationurl", url);
                    sendBroadcast(songIntent);
                    Log.d("asdf", "should send broadcast" );

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

            handler.postDelayed(this, …
Run Code Online (Sandbox Code Playgroud)

multithreading android asynchronous handler networkonmainthread

1
推荐指数
1
解决办法
1298
查看次数

如何解析没有数组名称的JSON数组

我有一个没有数组名称的json数组.如何解析没有名字的JSON数组?这是我的阵列

[
{
    "id": "13",
    "email": "jcheck1@gmail.com",
    "first_name": "jcheck",
    "last_name": "check",
    "country": "india",
    "city": "tvm",
    "zip_code": "695581",
    "phone": "4712584632",
    "status": "Success"
}
]
Run Code Online (Sandbox Code Playgroud)

我试图将它转换为JSON对象,并尝试从数组中直接获取String.但我和时间都有例外.

java android

1
推荐指数
1
解决办法
1113
查看次数