小编Had*_*adi的帖子

当我在真实手机上运行我的应用程序时,android studio logcat没有显示崩溃的原因

我有一个奇怪的问题.当我使用Android 4.1在Genymotion上运行我的应用程序时,一切都很好,Logcat工作正常并显示一切.但是当我在真正的手机上运行相同的应用程序(华为荣耀7与Android 6),并且我的应用程序崩溃时,logCat没有显示崩溃的原因.换句话说,log cat不显示异常.谁能帮助我?谢谢

也许有用:

logCat崩溃后: 照片

build.gradle(模块应用)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "my.application.id"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    ....
}
Run Code Online (Sandbox Code Playgroud)

crash android logcat

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

如何以编程方式限制下载速度?

我使用以下代码来限制java中文件的下载速度:

package org;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

class MainClass {

    public static void main(String[] args) {
        download("https://speed.hetzner.de/100MB.bin");
    }

    public static void download(String link) {
        try {
            URL url = new URL(link);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(5000);
            con.setReadTimeout(5000);
            InputStream is = con.getInputStream();
            CustomInputStream inputStream = new CustomInputStream(is);
            byte[] buffer = new byte[2024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                System.out.println("downloaded : " + len);
                //save file
            }
        } catch (IOException e) {
            e.printStackTrace();
        } …
Run Code Online (Sandbox Code Playgroud)

java android inputstream limit download-speed

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

Thread.join()在ExecutorService执行Thread时不起作用

这是我的代码:

public static void main(String[] args) {
        System.out.println("program started");
        ExecutorService executor = Executors.newCachedThreadPool();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread finished");
            }
        });
        executor.execute(thread);
        try {
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("thread joined");

    }
Run Code Online (Sandbox Code Playgroud)

当我如上所示启动我的线程时,thread.join()它不起作用,它不会等待线程完成.我需要通过ExecutorService执行我的Thread,并等待该Thread完成.但是我的代码效果不好.谁能帮我?

为什么我不使用Future而不是Thread

因为有时我需要中断我的线程并等待线程完成.但是当我取消一个Future时,future.get()得到一个Exception并且不等待它的Thread完成.

如果我的句子的语法不正确,我会提前道歉.因为我不会说英语.

java multithreading executorservice

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

RuntimeException:无法加载 AndroidJUnit4 的委托运行器

我正在尝试使用 Android Studio 为我的MainActivity.java班级使用 Junit4进行仪器测试 。我不知道我的代码是否正确,但这是我的测试类代码:

import android.widget.EditText;
import android.widget.TextView;

import androidx.test.espresso.Espresso;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class MainActivityTest{

    private EditText edt;
    private TextView txt;

    @Rule
    ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Before
    public void setup(){
        edt = activityTestRule.getActivity().findViewById(R.id.edt);
        txt = activityTestRule.getActivity().findViewById(R.id.txt);
    }

    @Test
    public void edt_text_change() {
        //i want this test to be passed
        assertEquals("147", "147");
    }
}
Run Code Online (Sandbox Code Playgroud)

构建.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29 …
Run Code Online (Sandbox Code Playgroud)

java junit android android-instrumentation

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

如何在Android中使布局高度等于其宽度?

我想使我CardView的所有高度都等于其宽度,并且我希望每个人CardView都占据用户设备屏幕的同一部分。我怎样才能做到这一点?我的代码是这样的:

<android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp">

                <android.support.v7.widget.CardView
                    android:id="@+id/tool1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    android:foregroundGravity="center"
                    app:layout_constraintHorizontal_weight="1"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toLeftOf="@id/tool2">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/too_4_background"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:paddingBottom="10dp"
                        android:paddingTop="10dp">

                        <LinearLayout
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:background="@drawable/emoji_background"
                            android:gravity="center">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:gravity="center"
                                android:text="&#x1F633;"
                                android:textSize="15dp" />

                        </LinearLayout>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:gravity="center"
                            android:text="????? ??????"
                            android:textColor="#ffffff"
                            android:textSize="13dp" />

                    </LinearLayout>


                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/tool2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    app:layout_constraintHorizontal_weight="1"
                    app:layout_constraintLeft_toRightOf="@id/tool1"
                    app:layout_constraintRight_toLeftOf="@id/tool3">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/too_4_background"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:paddingBottom="10dp"
                        android:paddingTop="10dp">

                        <LinearLayout
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:background="@drawable/emoji_background"
                            android:gravity="center">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:gravity="center" …
Run Code Online (Sandbox Code Playgroud)

java xml layout android

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