当我开始活动时,我的Android活动会崩溃我的应用程序,而我的其他2个活动也没有任何问题

时间:2017-06-19 01:32:42

标签: java android android-intent nullpointerexception

它只是一个凯撒密码,我在网上找到了它。

当我运行加密方法时应用程序不会崩溃,当我开始此活动时应用程序崩溃。

我的代码:

package com.example.brend.securityreach;

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

public class EncryptActivity extends AppCompatActivity {
    EditText thingToEncrypt = (EditText) findViewById(R.id.editText3);
    EditText offsets = (EditText) findViewById(R.id.editText4);
    String offsetting = thingToEncrypt.toString();
    int offset = Integer.valueOf(offsetting);
    EditText encryptedText = (EditText) findViewById(R.id.editText2);

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

    public void encrypt(View view) {
        char[] words = thingToEncrypt.toString().toCharArray();
        for (int i = 0; i < words.length; i++) {
            char letter = words[i];
            letter = (char) (letter + offset);
            if (letter > 'z') {
                letter = (char) (letter - 26);
            } else if (letter < 'a') {
                letter = (char) (letter + 26);
            }
            words[i] = letter;
        }
    }
}

Logcat错误:

06-19 12:49:55.587 3766-3766/com.example.brend.securityreach E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.brend.securityreach, PID: 3766
                                                                           java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.brend.securityreach/com.example.brend.securityreach.EncryptActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                               at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
                                                                               at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
                                                                               at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
                                                                               at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
                                                                               at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
                                                                               at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:29)
                                                                               at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:197)
                                                                               at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
                                                                               at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
                                                                               at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                                                                               at com.example.brend.securityreach.EncryptActivity.<init>(EncryptActivity.java:9)
                                                                               at java.lang.Class.newInstance(Native Method)
                                                                               at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

修改

我想通过尝试捕获并使用变量更改内容。感谢所有为此问题付出时间和精力的人,并向我展示了如何声明EditText变量等。

2 个答案:

答案 0 :(得分:0)

请更密切关注Android示例:

public class EncryptActivity extends AppCompatActivity {
    EditText thingToEncrypt = (EditText) findViewById(R.id.editText3); //no! don't do this

不要像上面那样使用findViewById(int id)字段初始化。

您需要将其放入onCreate(Bundle savedInstanceState)

EditText thingToEncrypt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_encrypt);
    thingToEncrypt = (EditText) findViewById(R.id.editText3);
}

为什么呢? EditText之类的观点在调用setContentView(int layoutId)之前无法使用。请阅读official guide以了解活动生命周期。

答案 1 :(得分:-1)

方案可能是,您尚未在AndroidManifest.xml文件中声明您的活动。请检查一下。

提供有关错误的完整详细信息,以便我们指导您正确的路径。