在 Android Studio 中测试时应用一直停止

App keep stoping when testing in Android Studio

2019-10-20 19:17:13.479 5647-5683/com.example.listviewapp I/ple.listviewap: ClassLoaderContext是一个特殊的共享库。 2019-10-20 19:17:13.559 5647-5647/com.example.listviewapp I/Perf:连接到 perf 服务。 2019-10-20 19:17:13.565 5647-5647/com.example.listviewapp W/com.example.listviewapp: type=1400 audit(0.0:1191): avc: denied { read } for comm=45474C20496E6974 name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=21652 scontext=u:r:untrusted_app:s0:c15,c257,c512,c768 tcontext=u:object_r :vendor_default_prop:s0 tclass=文件许可=0 2019-10-20 19:17:13.578 5647-5722/com.example.listviewapp E/libc:访问被拒绝发现 属性 "vendor.debug.egl.profiler" 2019-10-20 19:17:13.565 5647-5647/com.example.listviewapp W/com.example.listviewapp: type=1400 audit(0.0:1192): avc: denied { read } for comm=45474C20496E6974 name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=21652 scontext=u:r:untrusted_app:s0:c15,c257,c512,c768 tcontext=u:object_r :vendor_default_prop:s0 tclass=文件许可=0 2019-10-20 19:17:13.578 5647-5722/com.example.listviewapp E/libc:访问被拒绝发现 属性 "vendor.debug.prerotation.disable" 2019-10-20 19:17:13.606 5647-5647/com.example.listviewapp D/AndroidRuntime:关闭虚拟机

--------- beginning of crash

2019-10-20 19:17:13.608 5647-5647/com.example.listviewapp E/AndroidRuntime:致命异常:main 进程:com.example.listviewapp,PID:5647 java.lang.RuntimeException: 无法实例化 activity ComponentInfo{com.example.listviewapp/com.example.listviewapp.MainActivity}: java.lang.NullPointerException: 尝试调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()' 空对象引用 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2977) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3182) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6898) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 由以下原因引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'android.view.Window$Callback android.view.Window.getCallback()' 在 androidx.appcompat.app.AppCompatDelegateImpl.(AppCompatDelegateImpl.java:249) 在 androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182) 在 androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520) 在 androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:191) 在 com.example.listviewapp.MainActivity.(MainActivity.java:15) 在 java.lang.Class.newInstance(本机方法) 在 android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) 在 androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43) 在 android.app.Instrumentation.newActivity(Instrumentation.java:1232) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2965) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3182) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6898) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 2019-10-20 19:17:13.612 5647-5647/com.example.listviewapp W/OPDiagnose: getService:OPDiagnoseService 空 2019-10-20 19:17:13.619 5647-5727/com.example.listviewapp D/OSTracker:OS 事件:崩溃

这是我在物理设备上测试应用程序时的 logcat

我尝试将 Gradle 插件从当前版本更改为以前的版本,但没有帮助

这是MainActivity.java

package com.example.listviewapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ListView MyList = (ListView)findViewById(R.id.MyList);

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

    final String[] foods = {"Bacon", "Tuna", "Potato", "Meatball", "Candy", "Ham", "Shushi",
            "Apple", "Orange", "Pizza", "Hotdog", "Chole Bhatoore", "Chole Kulche", "Pasta"};


    //Adapter as a converter
        ListAdapter LAObject = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, foods);
        MyList.setAdapter(LAObject);


        //item listener
        MyList.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String store = String.valueOf(parent.getItemAtPosition(position));
                        Toast.makeText(MainActivity.this, store, Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }

}

我想知道为什么第二次尝试会发生这种情况,因为我第一次 运行 它工作得很好 但是当第二次尝试时,它总是停止。

我认为以下文件也可以帮助您找出问题所在

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.1"
    defaultConfig {
        applicationId "com.example.listviewapp"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

ListView MyList = (ListView)findViewById(R.id.MyList);

你打电话 findViewById() 太早了。

setContentView() 之后将调用移至 onCreate(),其中 activity 有一个 Window 并且布局层次结构已膨胀,因此您可以从中找到视图。