Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的应用程序正在虚拟设备上运行,但在物理设备上无法运行_Java_Android - Fatal编程技术网

Java 我的应用程序正在虚拟设备上运行,但在物理设备上无法运行

Java 我的应用程序正在虚拟设备上运行,但在物理设备上无法运行,java,android,Java,Android,我是Android Studio的新手,我刚刚完成了我的第一个应用程序。然后我想在我的手机上测试一下,但它似乎不起作用。它甚至在启动之前就崩溃了,尽管它在Android Studio内部的虚拟设备上工作得很好。我会在这里发布一些信息,希望有人能帮助我,因为我真的不明白 Logcat输出: 2020-06-06 05:01:12.188 12297-12297/? I/dlingsassisten: Late-enabling -Xcheck:jni 2020-06-06 05:01:12.218

我是Android Studio的新手,我刚刚完成了我的第一个应用程序。然后我想在我的手机上测试一下,但它似乎不起作用。它甚至在启动之前就崩溃了,尽管它在Android Studio内部的虚拟设备上工作得很好。我会在这里发布一些信息,希望有人能帮助我,因为我真的不明白

Logcat输出:

2020-06-06 05:01:12.188 12297-12297/? I/dlingsassisten: Late-enabling -Xcheck:jni
2020-06-06 05:01:12.218 12297-12297/? E/dlingsassisten: Unknown bits set in runtime_flags: 0x8000
2020-06-06 05:01:12.482 12297-12297/com.example.handlingsassistent I/Perf: Connecting to perf service.
2020-06-06 05:01:12.495 12297-12297/com.example.handlingsassistent I/dlingsassisten: [GL_OOM] ClampGrowthLimit 268435456
2020-06-06 05:01:12.499 12297-12297/com.example.handlingsassistent V/Font: Change font:1
2020-06-06 05:01:12.500 12297-12297/com.example.handlingsassistent V/Font: Default family:android.graphics.Typeface@3668eb82
2020-06-06 05:01:12.507 12297-12332/com.example.handlingsassistent E/Perf: Fail to get file list com.example.handlingsassistent
2020-06-06 05:01:12.507 12297-12332/com.example.handlingsassistent E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-06-06 05:01:12.507 12297-12332/com.example.handlingsassistent E/Perf: Fail to get file list com.example.handlingsassistent
2020-06-06 05:01:12.507 12297-12332/com.example.handlingsassistent E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-06-06 05:01:12.690 12297-12297/com.example.handlingsassistent W/dlingsassisten: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-06-06 05:01:12.692 12297-12297/com.example.handlingsassistent W/dlingsassisten: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-06-06 05:01:12.864 12297-12297/com.example.handlingsassistent D/AndroidRuntime: Shutting down VM
2020-06-06 05:01:12.868 12297-12297/com.example.handlingsassistent E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.handlingsassistent, PID: 12297
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.handlingsassistent/com.example.handlingsassistent.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3387)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3526)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2122)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7695)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:392)
        at android.widget.ListView.setAdapter(ListView.java:585)
        at com.example.handlingsassistent.MainActivity.onCreate(MainActivity.java:42)
        at android.app.Activity.performCreate(Activity.java:7820)
        at android.app.Activity.performCreate(Activity.java:7809)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1318)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3362)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3526) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2122) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7695) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
2020-06-06 05:01:12.910 12297-12297/com.example.handlingsassistent I/Process: Sending signal. PID: 12297 SIG: 9
主要活动

package com.example.handlingsassistent;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List<String> arrList = new ArrayList<String>();

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


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

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                R.layout.list_layout,
                arrList);

        shoppingListView.setAdapter(adapter);
        //Möjliggör att kunna välja flera alternativ i listan.
        shoppingListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        shoppingListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                arrList.remove(position);
                adapter.notifyDataSetChanged();
                saveData();
                return true;
            }
        });
    }


    public void addToList (View v){
        if( ((TextView) findViewById(R.id.inputText)).getText().toString().isEmpty()) {
            Log.d("Empty", "Input is empty");
        }
        else {
            arrList.add(((TextView) findViewById(R.id.inputText)).getText().toString());
            ((ListView) findViewById(R.id.shoppingListView)).invalidateViews();
            ((TextView) findViewById(R.id.inputText)).setText("");
            saveData();
        }
    }

    // Referens: https://developer.android.com/guide/topics/ui/dialogs.html
    public void clearList (View v){

        AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle(R.string.alert_title);
            alert.setMessage(R.string.alert_message);
            alert.setPositiveButton(R.string.alert_yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    arrList.clear();
                    ((ListView) findViewById(R.id.shoppingListView)).invalidateViews();
                    saveData();
                }
            });
            alert.setNegativeButton(R.string.alert_no, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            alert.create().show();

    }

    // Referens: https://www.youtube.com/watch?v=jcliHGR3CHo
    private void saveData(){
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(arrList);
        editor.putString("task list", json);
        editor.apply();
    }
    // Referens: https://www.youtube.com/watch?v=jcliHGR3CHo
    private void loadData(){
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString("task list", null);
        Type type = new TypeToken<ArrayList<String>>() {}.getType();
        arrList = gson.fromJson(json, type);

    }

    public void loadTemplateActivity(View v){
        Intent intent = new Intent(this, TemplateActivity.class);
        startActivity(intent);
    }

    public void loadSpendingActivity(View v){
        Intent intent = new Intent(this, SpendingActivity.class);
        startActivity(intent);
    }
}```

Hope this information is enough. I'll keep an eye out if you request something else to help me out.


package com.example.handlingsasistent;
导入androidx.appcompat.app.AlertDialog;
导入androidx.appcompat.app.appcompat活动;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入com.google.gson.gson;
导入com.google.gson.reflect.TypeToken;
导入java.lang.reflect.Type;
导入java.util.ArrayList;
导入java.util.List;
公共类MainActivity扩展了AppCompatActivity{
List arrList=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadData();
ListView shoppingListView=findViewById(R.id.shoppingListView);
最终ArrayAdapter适配器=新的ArrayAdapter(
这
R.layout.list_布局,
arrList);
shoppingListView.setAdapter(适配器);
//Möjliggör att kunna välja flera alternativ i listan。
shoppingListView.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE);
shoppingListView.setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener(){
@凌驾
公共布尔值长单击(AdapterView父项、视图、整型位置、长id){
删除(位置);
adapter.notifyDataSetChanged();
saveData();
返回true;
}
});
}
公共无效添加列表(视图五){
if(((TextView)findViewById(R.id.inputText)).getText().toString().isEmpty()){
Log.d(“空”、“输入为空”);
}
否则{
add(((TextView)findViewById(R.id.inputText)).getText().toString());
((ListView)findViewById(R.id.shoppingListView)).invalidateViews();
((TextView)findViewById(R.id.inputText)).setText(“”);
saveData();
}
}
//参考:https://developer.android.com/guide/topics/ui/dialogs.html
公共无效清除列表(视图v){
AlertDialog.Builder alert=新建AlertDialog.Builder(此);
alert.setTitle(R.string.alert\u title);
alert.setMessage(R.string.alert\u消息);
alert.setPositiveButton(R.string.alert_yes,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
arrList.clear();
((ListView)findViewById(R.id.shoppingListView)).invalidateViews();
saveData();
}
});
alert.setNegativeButton(R.string.alert_no,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
}
});
alert.create().show();
}
//参考:https://www.youtube.com/watch?v=jcliHGR3CHo
私有void saveData(){
SharedPreferences SharedPreferences=getSharedPreferences(“共享首选项”,模式\私人);
SharedReferences.Editor=SharedReferences.edit();
Gson Gson=新的Gson();
字符串json=gson.toJson(arrList);
putString(“任务列表”,json);
editor.apply();
}
//参考:https://www.youtube.com/watch?v=jcliHGR3CHo
私有void loadData(){
SharedPreferences SharedPreferences=getSharedPreferences(“共享首选项”,模式\私人);
Gson Gson=新的Gson();
String json=SharedReferences.getString(“任务列表”,null);
Type Type=new-TypeToken(){}.getType();
arrList=gson.fromJson(json,类型);
}
公共void loadTemplateActivity(视图v){
Intent Intent=新的Intent(这是TemplateActivity.class);
星触觉(意向);
}
公共void loadSpendingActivity(视图五){
意向意向=新意向(此,SpendingActivity.class);
星触觉(意向);
}
}```
希望这些信息足够。如果你要求别的什么帮助我,我会留意的。

您遇到此异常是因为您试图使用空适配器构建ListView;因为第一次启动应用程序时,在
onCreate()
方法中调用
loadData()
;这将为
arrList
分配一个空值,因为
SharedReference
在ot中仍然没有保存任何内容

您可以解决此问题,首先检查
SharedReference
返回的数据是否不为空;否则,指定一个空数组

因此,更改
loadData()
,如下所示

    private void loadData(){
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString("task list", null);
        Type type = new TypeToken<ArrayList<String>>() {}.getType();
        arrList = gson.fromJson(json, type);

        if (arrList == null)
            arrList = new ArrayList<String>(); 

    }

private void loadData(){
SharedPreferences SharedPreferences=getSharedPreferences(“共享首选项”,模式\私人);
Gson Gson=新的Gson();
String json=SharedReferences.getString(“任务列表”,null);
Type Type=new-TypeToken(){}.getType();
arrList=gson.fromJson(json,类型);
如果(arrList==null)
arrList=新的ArrayList();
}

您的
arrList
可能在类构造时初始化后被设置回null

尝试检查空值并设置它,如下所示

   private void loadData(){
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString("task list", null);
        Type type = new TypeToken<ArrayList<String>>() {}.getType();
        arrList = gson.fromJson(json, type);
        if (arrList  == null) {
            arrList = new ArrayList<String>();
        }
    }
private void loadData(){
SharedReferences SharedReferences