尝试通过我的主要方法
中的方法开始新活动时,我收到此异常 java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.Context android.content.Context.getApplicationContext()
' on a null object reference
我在主活动中没有调用用于启动新活动的方法。我认为这是导致问题的原因,因为我试图在主要活动的OnCreate方法中调用该方法,并且它有效。
然而我无法找到解决这个问题的方法,一位朋友建议我使用单身,但这不起作用 - 或者我对如何使用它的理解太有限了。
这是我的主要课程:
package gruppe16.sdupay;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import java.util.ArrayList;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*/
public class Main extends Activity {
static EditText et1, et2, et3, et4;
private String password = "1234";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
et3 = (EditText) findViewById(R.id.editText3);
et4 = (EditText) findViewById(R.id.editText4);
et1.addTextChangedListener(new CustomTextWatcher(et1));
et2.addTextChangedListener(new CustomTextWatcher(et2));
et3.addTextChangedListener(new CustomTextWatcher(et3));
et4.addTextChangedListener(new CustomTextWatcher(et4));
}
public void login (String passInput){
if(passInput.equals(password)){
Log.d("asd", "PASS IS CORRECT");
Intent appIntent = new Intent(getApplicationContext(), AppActivity.class);
startActivity(appIntent);
} else {
Log.d("asd2", "PASS IS NOT CORRECT");
}
}
}
这是我的CustomTextWatcher类
package gruppe16.sdupay;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
/**
* Created by jonaspedersen on 15/12/15.
*/
public class CustomTextWatcher implements TextWatcher {
static String et1String, et2String, et3String, et4String;
private EditText mEditText;
private Main MainObject = new Main();
private String passString;
public CustomTextWatcher(EditText e) {
mEditText = e;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (mEditText == Main.et1){
et1String = Main.et1.getText().toString();
Main.et2.requestFocus();
} else if (mEditText == Main.et2){
et2String = Main.et2.getText().toString();
Main.et3.requestFocus();
} else if (mEditText == Main.et3){
et3String = Main.et3.getText().toString();
Main.et4.requestFocus();
} else if (mEditText == Main.et4){
et4String = Main.et4.getText().toString();
passString = et1String + et2String + et3String + et4String;
MainObject.login(passString);
}
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gruppe16.sdupay">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.Light.NoTitleBar">
<activity
android:name=".Main"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/SDUTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AppActivity"
android:label="@string/title_activity_app"></activity>
</application>
</manifest>
答案 0 :(得分:1)
使用
Intent appIntent = new Intent(Main.this, AppActivity.class);
也会更改CustomTextWatcher构造函数
private Main MainObject;
private String passString;
public CustomTextWatcher(EditText e,Main MainObject) {
mEditText = e;
this.MainObject = MainObject;
}
并将TextWatcher设置为
et1.addTextChangedListener(new CustomTextWatcher(et1),Main.this);