-1

My apps crashed after i added the following to my code

String displayingText;
EditText editText = (EditText)findViewById(R.id.editText);

In button startview

displayingText = editText.getText().toString();

Can anyone guide me in this?

Code for MainActivity :

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

String displayingText;// = "abc";
EditText editText = (EditText)findViewById(R.id.editText);

public void StartService(View v) {

    displayingText = editText.getText().toString();

    Intent mIntent = new Intent(this, MyService.class);
    mIntent.putExtra("PassToService",displayingText);
    startService(mIntent);//use to start the services
    Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show();
}
public void StopService(View v){
    stopService(new Intent(this, MyService.class));//use to start the services)
    //stopService(serviceIntent);
    Toast.makeText(this, "Stopped", Toast.LENGTH_SHORT).show();
}}

Error Log :

12-12 07:52:19.674 6893-6893/tk.myessentialoils.toast5sec E/AndroidRuntime: FATAL EXCEPTION: main Process: tk.myessentialoils.toast5sec, PID: 6893 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tk.myessentialoils.toast5sec/tk.myessentialoils.toast5sec.MainActivity}: 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:2718) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 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.(AppCompatDelegateImplBase.java:117) at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:149) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:29) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:54) at android.support.v7.app.AppCompatDelegateImplV23.(AppCompatDelegateImplV23.java:31) at android.support.v7.app.AppCompatDelegateImplN.(AppCompatDelegateImplN.java:31) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190) at tk.myessentialoils.toast5sec.MainActivity.(MainActivity.java:26) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1173) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

Anthony
  • 51
  • 7

5 Answers5

0
EditText editText; // declare global variable 

in onCreate Method do findViewById

editText  = (EditText)findViewById(R.id.editText);
Dharmishtha
  • 1,313
  • 10
  • 21
  • If declare under onCreate, the editText inside the StartService(View v) will be unabled to resolve. – Anthony Dec 12 '17 at 08:08
0
  • set EditText as global variable

  • findViewById in your onCreate method

Try

String displayingText;// = "abc";
EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // find here
    editText = (EditText)findViewById(R.id.editText);
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

Try this code...

public class MainActivity extends AppCompatActivity {
EditText editText;
String displayingText;// = "abc";

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

  editText = (EditText)findViewById(R.id.editText);
}

public void StartService(View v) {

    displayingText = editText.getText().toString();

    Intent mIntent = new Intent(this, MyService.class);
    mIntent.putExtra("PassToService",displayingText);
    startService(mIntent);//use to start the services
    Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show();
}
public void StopService(View v){
    stopService(new Intent(this, MyService.class));//use to start the services)
    //stopService(serviceIntent);
    Toast.makeText(this, "Stopped", Toast.LENGTH_SHORT).show();
}}
Noel
  • 384
  • 1
  • 3
  • 16
0

Most Android components can't be initialised when they are declared. If you want to use them, you have to declare them as a normal field and initialise them in onCreate. In the end it will look like this:

EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText)findViewById(R.id.editText);
}
Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
0

You should do this way-:

    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

             initViews();
    }

    String displayingText;// = "abc";


    public void StartService(View v) {

        displayingText = editText.getText().toString();

        Intent mIntent = new Intent(this, MyService.class);
        mIntent.putExtra("PassToService",displayingText);
        startService(mIntent);//use to start the services
        Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show();
    }
    public void StopService(View v){
        stopService(new Intent(this, MyService.class));//use to start the services)
        //stopService(serviceIntent);
        Toast.makeText(this, "Stopped", Toast.LENGTH_SHORT).show();
    }
   private void initViews()
   {
       EditText editText = (EditText)findViewById(R.id.editText);


   }

}

Declare all your views in initViews() method and specify id should be same as in .xml file.

Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18