I have run error at on Create on Android studio - java

I get a massage says "app is keep stopping" when I run the project on my mobile.
the error at on Create(MainActivity.java:16)
at the line 16 set Content View method
help me

The method setContentView() sets the View that your MainActivity should display.
The View is usually declared in layout/activity_main.xml.
Some error seems to appear when trying to call this method in your application.
A common reason why android applications crash there is calling some methods in the overridden method onCreate(Bundle savedInstanceState) in a wrong order.
Check if your code follows this pattern:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView call
// other things
}
I cannot help you further now, please copy and paste the stacktrace (crash information) into your question. When your application just crashed on an emulator, you can see it in the Run-section (usually red text).

Related

Getting NullPointerException first time when Activity loads but working fine second time

I am starting EditCardActivity from MainActivity.
The app crashes first time and Android system shows an alert to "Open App Again".
When I reopen the app, it works as expected.
I have seen many answers on this site and I have done following according to those answers but it didn't work.
I have called the setContentView() inside onCreate() method before calling findViewByID().
I have verified that the ID I am passing in findViewByID() is spelled correctly.
I also tried to make the EditText a class member too and initialize it in onCreate() method.
I also tried using onStart(), onPostCreate() methods too.
I feel that the View has not been loaded when I am trying to call it and thus findViewByID() returns null. Hence I tried using Thread.sleep(1000) to give it 1 second to load but still same issue.
Here's the part of code that is having problem.
// MainActivity.java
public void editCard(View v) {
Intent i = new Intent(this, EditCardActivity.class);
startActivity(i);
}
<!--activity_edit_card.xml-->
...
<EditText
android:id="#+id/add_card_category_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin"
android:ems="10"
android:hint="#string/add_card_category_et_hint"
android:inputType="text"
android:textSize="#dimen/font_size" />
...
// EditCardActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_card);
showContents();
}
private void showContents() {
EditText editCardCategoryET = findViewById(R.id.add_card_category_et);
editCardCategoryET.setText(currentCard.getCategory()); // This line is throwing NullPointerException
}
Error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Edit:
I got what was causing the error.
I was referring to the add_card_category_et instead of edit_card_category_et.
The former view belonged to the different layout file of another Activity of my application.
I am sorry I couldn't catch this small error.
Although small, it made me stuck for 3 days.
Anyway thanks for your answers and comments.
Because editCardCategoryET cant find any value. So it throws Exception. It can be solve by add extra character by adding " " . Then app will not crush. editCardCategoryET.setText(" "+currentCard.getCategory()); .
Also you can handle it in a try(), catch() method.

findViewById causes NullPointerException for no apparent reason

I am new to Android development. My app is compiling perfectly fine (no errors, no warnings), but it crashes on launch. I have diagnosed the cause of the crash to be a NullPointerException, and I discovered that if I remove TextView output = (TextView) findViewById(R.id.outputbox); as well as all the lines depending on this declaration, the app launches fine. I did research (both on and off Stack Exchange, and about 5 pages of Google results) and none of the solutions I have found worked, which is why I am asking this question. I have ran setContentView(R.layout.activity_main); before TextView output = (TextView) findViewById(R.id.outputbox); and outputbox is declared in activity_main.xml. This is not a duplicate of the existing questions because the answers to the other ones did not solve my problem.
most probably your declaration and initialization of the variable output is in the class. This is not going to work. The method findViewById will work only after the setContentView method has been invoked. That is why you have to declare your variable in the class, but initialize it in the method onCreate immediately after the setContentView method:
public class MainActivity extends AppCompatActivity {
TextView output;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = (TextView) findViewById(R.id.outputbox)
}
}
The method findViewById searches for your component in the xml. For this to work, you have to point out what xml file will this Activity be using. This is done in the setContentView(R.layout.activity_main); command. It is a common mistake, so no worries :)

Android development noob: WebView usage

I've been following this series of Android Development Tutorials and I've encountered some problem trying to tweak one of the projects I've created.
Basically all I want to know is how to apply the WebView correctly in my project - in the easiest and most simple way possible.
What I've done:
enabled permission to the internet on the manifest
created the webview on the main_activity.xml
imported the WebView webkit to my MainActivity Java class
And all I've done after that is simply loading the url on my onCreated method like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView v = (WebView) findViewById(R.id.weblol);
v.loadUrl("www.google.com");
}
However nothing seems to happen and the block of the WebView remains blank white
Care to explain to a noob what am I doing wrong ?
You should use http://www.google.com instead of www.google.com.
Only you to this,webview can notice this it is a URL.
Otherwise.you can use local file in webview ,like file:///android_asset/xxx in assets folder.
That's all.

onCreate android os bundle is never used

I'm a noob in java android but I get this error when I hover onCreate method onCreate(android.os.Bundle) is never used. Should I worry about this error?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab().setText("Feed").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Browse").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Settings").setTabListener(this));
}
Thanks
thats not an error its a warning. the bundle is used for putting information in when the app changes state ie. orientation so you can continue where the user left off.
there is no harm in not using it however the activity will be shown just as it would if you were to launch it from the launcher.
Got it! I just changed extends FragmentActivity into extends Activity and imported android.app.Activity.

Android activity restart

Hey guys, i am making an android application where i want to show a dialog box about legal agreement everytime the application starts, i have a public method showalert(<>); which shows an alertdialog by building a dialog with alertbuilder. I added a call to showalert() method on the onCreate() method of the main activity to show it, but whenever the user rotates the screen, he gets the dialog everytime. The activity restarts itself when the phone is rotated. I tried adding android:configChanges="keyboardHidden|orientation" to my manifest but that doesnt help on this case. Also can i know how to register a new application class on manifest file. I am trying to create an application class and put the code to show dialog on the new class's oncreate method. But i am not being able to load the class when the app starts.
I also checked Activity restart on rotation Android but i dont seem to get a thing. I am pretty much a newbie to android programming, could someone simplify that for me?
Any help would be appreciated. :)
you could maybe look at the onRetainNonConfigurationInstance() activity method, which is called just before destroying and re-creating the activity on screen orientation change.
it allows you to retain an object that could for instance contain a test variable to know if your legal thing was already shown or not.. example :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String test = (String) getLastNonConfigurationInstance();
if (!("textAlreadyShown").equals(test)) {
//here : show your dialog
}
}
#Override
public String onRetainNonConfigurationInstance() {
return "textAlreadyShown";
}
Set the main activity to an activity that just shows the legal notice, when it is accepted/cleared, show a second activity ( which is currently the main activity )?

Categories

Resources