请求权限/Android Studio 时应用程序崩溃
Posted
技术标签:
【中文标题】请求权限/Android Studio 时应用程序崩溃【英文标题】:App Crashes when asking for Permission / Android Studio 【发布时间】:2018-09-15 04:40:11 【问题描述】:我目前正在开发一个用于将 .txt 写入存储的 android 应用程序。 但每次我想请求权限(在运行时)时,应用程序崩溃并显示错误代码:“应用程序不幸停止......”。
权限是在清单中定义的,我会检查它是否已经被授予。 但是当它要求它时,应用程序立即崩溃。
希望有人可以帮助我。
这是我的代码:
public class MainActivity extends AppCompatActivity
//Deklarationen
Button btn;
EditText editText;
//Ordner anlegen
File ordner;
//Mit beliebiger Zahl
final int REQ_CODE_EXTERNAL_STORAGE_PERMISSION = 22;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Zuweisungen Speicherort
//ordner = new File(Environment.getExternalStorageDirectory(), "SD_Konfiguration");
//Ordner erstellen wenn noch nicht da
if(!ordner.exists())
//ordner.mkdirs(); //Abfragen ob bereits Permission
if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
createFolder();
else
//Dialog über Permission anzeigen
ActivityCompat.requestPermissions(MainActivity.this, new String[] Manifest.permission.WRITE_EXTERNAL_STORAGE, REQ_CODE_EXTERNAL_STORAGE_PERMISSION);
else
Toast.makeText(getApplicationContext(), "Ordner bereits angelegt", Toast.LENGTH_SHORT).show();
//Listener
editText = (EditText)findViewById(R.id.editText);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener()
//Aktiviert beim klicken
@Override
public void onClick(View v)
if (editText.getText().length() >0)
//Datei anlegen
File Konfig = new File(ordner, "Konfig_Datei.txt");
Toast.makeText(getApplicationContext(), "Datei angelegt", Toast.LENGTH_SHORT).show();
//Schreiben
try
OutputStream outputStream = new FileOutputStream(Konfig);
outputStream.write(editText.getText().toString().getBytes());
outputStream.close();
//Wieder zurücksetzen
editText.setText(null);
Toast.makeText(getApplicationContext(), "Ergebnis gespeichert", Toast.LENGTH_SHORT).show();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
else
Toast.makeText(getApplicationContext(), "Kein Text", Toast.LENGTH_SHORT).show();
);
//Ordner mit Permission anlegen
private void createFolder()
File ordner = new File(Environment.getExternalStorageDirectory(), "SD_Konfiguration");
ordner.mkdirs();
Toast.makeText(getApplicationContext(), "Ordner angelegt", Toast.LENGTH_SHORT).show();
//Hier wird übergeben ob Permission bestätigt wurde (läuf alles zusammen)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == REQ_CODE_EXTERNAL_STORAGE_PERMISSION && grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
createFolder();
Logcat:
04-05 11:22:44.959 4986-4986/? D/AndroidRuntime: Shutting down VM
04-05 11:22:44.959 4986-4986/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.sidacon.sidaconsd, PID: 4986
java.lang.RuntimeException: Unable to start activity ComponentInfode.sidacon.sidaconsd/de.sidacon.sidaconsd.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.exists()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.exists()' on a null object reference
at de.sidacon.sidaconsd.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
【问题讨论】:
请发布您的 logcat。 对 File ordner 进行空检查; 对于错误问题也必须使用 logcat。没有它,我们将如何理解问题? 抱歉,我是 Android 新手,Java 几乎是新手。我已将其添加到主帖中,谢谢您的帮助。 你的订单是空男人 【参考方案1】:好的,你要做的第一件事是,请求权限不要检查你的 File ordner 是否存在请求权限。
if(!ordner.exists()) //Do that later.
如果用户拒绝权限 goBack / closeTheActivity
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == REQ_CODE_EXTERNAL_STORAGE_PERMISSION && grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
createFolder();
else
finish();
【讨论】:
【参考方案2】:您在存储权限之前检查文件是否存在,首先授予权限,然后检查文件是否存在,如下所示。
if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
createFolder();
else
//Dialog über Permission anzeigen
ActivityCompat.requestPermissions(MainActivity.this, new String[] Manifest.permission.WRITE_EXTERNAL_STORAGE, REQ_CODE_EXTERNAL_STORAGE_PERMISSION);
并像这样更改您的 createFolder()
private void createFolder()
File ordner = new File(Environment.getExternalStorageDirectory(), "SD_Konfiguration");
if(!ordner.exists())
ordner.mkdirs();
Toast.makeText(getApplicationContext(), "Ordner angelegt", Toast.LENGTH_SHORT).show();
【讨论】:
以上是关于请求权限/Android Studio 时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章