为什么Android Studio中存在运行时错误?

时间:2018-01-03 07:00:32

标签: java android android-studio runtime-error

我的hp预兆(16gb RAM,Linux Ubuntu,I7-6700HQ)上的Android Studio中存在运行时错误。我使用Java 8和Android Studio。有人可以帮帮我吗?

PS:对不起,我的英语不太好,我不是母语人士。

以下是来自Java的错误:

01-02 17:30:25.841 5975-5975/? E/libprocessgroup: failed to make and chown /acct/uid_10061: Read-only file system
01-02 17:30:25.841 5975-5975/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
01-02 17:30:25.841 5975-5975/? I/art: Not late-enabling -Xcheck:jni (already on)
01-02 17:30:25.927 5975-5975/com.example.user..androidappforerg I/InstantRun: starting instant run server: is main process
01-02 17:30:25.957 5975-5975/com.example.user.androidappforerg W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-02 17:30:26.015 5975-5975/com.example.user.androidappforerg D/AndroidRuntime: Shutting down VM
01-02 17:30:26.015 5975-5975/com.example.user.androidappforerg E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                          Process: com.example.user.androidappforerg, PID: 5975
                                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.androidappforerg/com.example.user.androidappforerg.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                              at android.os.Looper.loop(Looper.java:135)
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                              at com.example.user.androidappforerg.MainActivity.onCreate(MainActivity.java:26)
                                                                                              at android.app.Activity.performCreate(Activity.java:5990)
                                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                              at android.os.Looper.loop(Looper.java:135) 
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                                              at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
01-02 17:35:26.064 5975-5975/? I/Process: Sending signal. PID: 5975 SIG: 9

这是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lukas_muenzel.androidappforerg.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">

<Button
    android:id="@+id/CaesarNormal"
    android:layout_width="199dp"
    android:layout_height="62dp"
    android:text="Cäsar Ver- und Entschlüsselung"
    tools:layout_constraintTop_creator="1"
    android:layout_marginStart="16dp"
    android:layout_marginTop="25dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="16dp" />

<Button
    android:id="@+id/CaesarNormalInfo"
    android:layout_width="88dp"
    android:layout_height="48dp"
    android:text="Button"
    tools:text="Info"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    android:layout_marginEnd="37dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="37dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginRight="37dp" />

<Button
    android:id="@+id/CaesarAutomatic"
    android:layout_width="0dp"
    android:layout_height="74dp"
    android:text="Cäsar Entschlüsselung (automatisch)"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toBottomOf="@+id/RSA"
    app:layout_constraintRight_toRightOf="@+id/RSA"
    android:layout_marginTop="134dp"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="135dp"
    app:layout_constraintLeft_toLeftOf="@+id/CaesarNormal"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/CaesarAutomaticInfo"
    android:layout_width="88dp"
    android:layout_height="48dp"
    android:text="Button"
    tools:text="Info"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    android:layout_marginEnd="37dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="12dp"
    app:layout_constraintTop_toTopOf="@+id/CaesarAutomatic"
    android:layout_marginRight="37dp" />

<Button
    android:id="@+id/RSA"
    android:layout_width="198dp"
    android:layout_height="74dp"
    android:text="RSA"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toTopOf="@+id/AES"
    android:layout_marginStart="16dp"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="57dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="16dp" />

<Button
    android:id="@+id/RSAInfo"
    android:layout_width="88dp"
    android:layout_height="48dp"
    android:text="Button"
    tools:text="Info"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toBottomOf="@+id/RSA"
    android:layout_marginEnd="37dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginBottom="7dp"
    android:layout_marginRight="37dp" />

<Button
    android:id="@+id/AES"
    android:layout_width="0dp"
    android:layout_height="74dp"
    android:text="AES (Advanced Encryption Standard)"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="@+id/RSA"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="44dp"
    app:layout_constraintLeft_toLeftOf="@+id/RSA"
    app:layout_constraintHorizontal_bias="0.0" />

<Button
    android:id="@+id/AesInfo"
    android:layout_width="88dp"
    android:layout_height="48dp"
    android:text="Button"
    tools:text="Info"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="37dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginBottom="70dp"
    android:layout_marginRight="37dp" />

</android.support.constraint.ConstraintLayout>

这是Java类     包com.example.lukas_muenzel.androidappforerg;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
 final Button RsaButton = (Button) findViewById(R.id.RSA);

 final Button RsaInfoButton = (Button) this.findViewById(R.id.RSAInfo);
 final Button AesButton = (Button) this.findViewById(R.id.AES);

 final Button CaesarNormalButton =                                                                          (Button)this.findViewById(R.id.CaesarNormal);
 final Button CaesarNormalInfoButton = (Button) this.findViewById(R.id.CaesarNormalInfo);
 final Button CaesarAutomaticButton = (Button) this.findViewById(R.id.CaesarAutomatic);
 final Button CaesarAutomaticInfoButton = (Button) this.findViewById(R.id.CaesarAutomaticInfo);

 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 RsaButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           RsaButton.setText("df");
        }
    });


}


}

我怎么能解决这个问题,我不知道。 感谢您的帮助 - WDJPNG

4 个答案:

答案 0 :(得分:1)

 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

它应该是onCreate方法的开始。然后,您可以从布局中找到ID,

答案 1 :(得分:0)

像这样更改所有按钮。

<?php
      $dir = dirname(__FILE__);
      echo $dir;
?>

答案 2 :(得分:0)

试试这个

Button RsaButton ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RsaInfoButton = (Button) this.findViewById(R.id.RSAInfo); 
RsaButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       RsaButton.setText("df");
    }
});
}

答案 3 :(得分:0)

改变这一点 - :

final Button RsaButton = (Button) findViewById(R.id.RSA);

 final Button RsaInfoButton = (Button) this.findViewById(R.id.RSAInfo);
 final Button AesButton = (Button) this.findViewById(R.id.AES);

 final Button CaesarNormalButton =                                                                          (Button)this.findViewById(R.id.CaesarNormal);
 final Button CaesarNormalInfoButton = (Button) this.findViewById(R.id.CaesarNormalInfo);
 final Button CaesarAutomaticButton = (Button) this.findViewById(R.id.CaesarAutomatic);
 final Button CaesarAutomaticInfoButton = (Button) this.findViewById(R.id.CaesarAutomaticInfo);

 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

这样做 - :

First you need to call 
super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

Then do your intilization of buttons

final Button RsaButton = (Button) findViewById(R.id.RSA);

     final Button RsaInfoButton = (Button) this.findViewById(R.id.RSAInfo);
     final Button AesButton = (Button) this.findViewById(R.id.AES);

     final Button CaesarNormalButton =                                                                          (Button)this.findViewById(R.id.CaesarNormal);
     final Button CaesarNormalInfoButton = (Button) this.findViewById(R.id.CaesarNormalInfo);
     final Button CaesarAutomaticButton = (Button) this.findViewById(R.id.CaesarAutomatic);
     final Button CaesarAutomaticInfoButton = (Button) this.findViewById(R.id.CaesarAutomaticInfo);