Skip to content Skip to sidebar Skip to footer

Application Crashes When Pressing The Button

I have struggled to find the solution for couple of days and it has drive me nuts. I am currently following the tutorial in http://developer.android.com/guide/topics/ui/actionbar.h

Solution 1:

Move your EditText inside of your onCreate() method in MyActivity.java

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText editText=(EditText)findViewById(R.id.edit_message);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    // If your minSdkVersion is 11 or higher, instead use://getActionBar().setDisplayHomeAsUpEnabled(true);
}

And inside your sendMessage() method, just remove that line.

Edit: It seems like you're extending from the new AppCompatActivity. Try changing it to ActionBarActivity in both the activities. For example:

publicclassMyActivityextendsActionBarActivity {...}
publicclassDisplayMessageActivityextendsActionBarActivity {...}

Edit: If you're planning on not using the ActionBarActivity, then you need to define a custom Toolbar and initialze it like this..

Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Solution 2:

Finally I manage to get the solution after struggling for almost 3 days. the process to resolve this issue is really a journey for me as a newbie in android. This is list of errors and warning tracked and solved during the process:

  1. The class name ActionBarActivity was strikeout in public class MyActivity extends ActionBarActivity The explanation is: Since the version 22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity

once I clear all the deprecated issue , the next error:

  1. java.lang.NullPointerException I solved this issue ( or at least I thought so) by checking the null value

    assert getSupportActionBar() != null; getSupportActionBar().setDisplayHomeAsUpEnabled(true);

  2. Next error was always pointing to the sendMessage(View view) I found this post : java.lang.IllegalStateExeception: could not find method in activity class

And it solved my problem. My actual mistake was I have set the theme in AndroidManifest.xml and again i set the them in layout file. Causing the application to crash. I removed the one in layout.xml file.

Post a Comment for "Application Crashes When Pressing The Button"