Android.Intent[Another activity] - android

My task: after button pressed, - second activity opening.
Problem: after button pressed, - "application closed unexpectedly".
LogCat said (short version):
04-10 21:25:24.968: E/AndroidRuntime(13032):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{cat.dog.szosta/cat.dog.szosta.ListaOcenActivity}:
java.lang.NullPointerException
LogCat said (full version):
https://drive.google.com/file/d/0B1jfkoUAwYVhYmFvSzBmS2ZIaU0/edit?usp=sharing
First activity code (partial):
private Button mOcenyPrzycisk;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
mOcenyPrzycisk = (Button)findViewById(R.id.ocenyPrzycisk);
mOcenyPrzycisk.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intencja = new Intent(MainActivity.this, ListaOcenActivity.class);
startActivity(intencja);
}
}
);
}
Second activity(partial):
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_ocen);
mprzyciskWroc = (Button) findViewById(R.id.przyciskWroc);
/*line 28*/ mprzyciskWroc.setOnClickListener(
new View.OnClickListener()
{
public void onClick (View v)
{
finish();
}
}
);
}
P.S: second activity declared in AndroidManifest.xml
I was looking in (didn't help):
Using Intent in an Android application to show another activity
android intents
Thanks in advance!

There is an exception in your second activity. (in ListaOcenActivity)
NullpointerException is thrown at line 28 of your ListaOcenActivity class, in the onCreate method. Search the method generating nullpointer exception, and handle it.
The way you are starting the activity is fine.
04-10 21:25:24.968: E/AndroidRuntime(13032): Caused by: java.lang.NullPointerException
04-10 21:25:24.968: E/AndroidRuntime(13032): at
cat.dog.szosta.ListaOcenActivity.onCreate(ListaOcenActivity.java:28) //here you can see its in your 2nd activity

Problem is here: cat.dog.szosta.ListaOcenActivity.onCreate(ListaOcenActivity.java:28)

Looking at your LogCat (long version) it shows that the NPE is in class ListaOceanActivity line 28. Which means this code is correct. Look into that class instead.

Related

.init(activity); is showing error [android]

i am trying to implement ShineButton in my project . I have successfully synced the library to the gradle and added shine button in the xml.
now when i am trying to write the java code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Kill bill", Toast.LENGTH_SHORT).show();
}
});
ShineButton shineButton = (ShineButton) findViewById(R.id.po_image2);
shineButton.init(context);
}
}
.init(activity); is showing cannot resolve symbol activity.
You don't literally copy the code verbatim, you read the documentation and object types supported by the method.
public void init(Activity activity) {
For example, I assume you are running that from an activity based on the usage of findViewById? Then you need "this instance of the Activity"
shineButton.init(this);
or an instance of an Activity if you were in a Fragment
shineButton.init(getActivity());
If the code is in Activity use shineButton.init(Activityname.this).
If it is in
fragment use shineButton.init(getActivity()).
Read this: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/ and this https://developer.android.com/training/index.html
Change:
shineButton.init(context);
To:
shineButton.init(MainActivity.this);
MainActivity.this holds the instance of MainActivity class and can be used to initialise the view.

java.lang.NullPointerException at android.content.ContextWrapper.getPackageName ANDROID

Hey I want to start an Activity from my MainActivity but not in the oncreate method.
public void awe()
{
Intent myIntent = new Intent(MainActivity.this, Awesome.class);
MainActivity.this.startActivity(myIntent);
}
Another class calls the method awe() and what I get is a crash and
05-25 04:06:51.034: E/AndroidRuntime(7161): FATAL EXCEPTION: main
05-25 04:06:51.034: E/AndroidRuntime(7161): java.lang.NullPointerException
05-25 04:06:51.034: E/AndroidRuntime(7161): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:151)
05-25 04:06:51.034: E/AndroidRuntime(7161): at android.content.ComponentName.<init>(ComponentName.java:106)
05-25 04:06:51.034: E/AndroidRuntime(7161): at android.content.Intent.<init>(Intent.java:2895)
05-25 04:06:51.034: E/AndroidRuntime(7161): at package name.MainActivity.awe(MainActivity.java:215)
Someone knows what I can do?
MainActivity
public class MainActivity extends Activity implements OnClickListener {
// (variable stuff)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonE = (Button) findViewById(R.id.buttonEASY);
buttonM = (Button) findViewById(R.id.buttonMED);
// here I do all that button stuff for the layout
}
public void onClick(View arg0) {
System.out.println("click");
if (arg0==buttonE) {
int checkedRadioButton = radioGroup1.getCheckedRadioButtonId();
String radioButtonSelected = "";
switch (checkedRadioButton) {
case R.id.radio0 : radioButtonSelected = "radiobutton1";
Toast.makeText(getApplicationContext(), "Easy, 10 selected", Toast.LENGTH_SHORT).show();
setContentView(R.layout.raten);
// Button stuff, again.
}
public void awe()
{ Intent tutorial = new Intent(MainActivity.this, Awesome.class);
if (tutorial != null) { startActivity(tutorial); }
}
Easy.java
Nothing important here, the place where I refer to awe():
if (s==max+1){System.out.println("AWESOME!"); MainActivity mA = new MainActivity(); mA.awe();}
Awesome.java
public class Awesome extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.awesome);
}
I hope I now posted everything that is important
The problem probably is that MainActivity has not been fully initialized yet when you are calling the awe() method, and the internal Context of the Activity is null.
Things to consider with Android Activities:
Do you have your classes that extend Activity defined in the AndroidManifest.xml?
Are you aware of your Context when using Intents?
For calling intents, always check for null, if you are calling via packagename:
Intent mTutorial = new Intent(MainActivity.this, TutorialActivity.class);
this.startActivity(mTutorial);
Your problem was simply trying to call your "awe()" method was in another Activity that did not have the correct Context for your MainActivity: http://developer.android.com/reference/android/content/Intent.html.
Android Intent requires a "Context" and a "Class".
Update: Here is another post that will help:
Launch an application from another application on Android
Regards,
I got the same error, which took me a while to figure it out.
Like #csgero mentioned, my problem was that the activity I tried to start was not initialized.
It means errors happen before onCreate is called. And it turned out that there was a error in the codes part where I defined the variables in the to-be-called activity. Good luck!
The problem probably is that MainActivity is null. in my case, the activity destroyed when run abvoe code. so the internal Context of the Activity is null.

Activity instance remains in the memory after onDestroy()

I know that this topic has been already beaten enough, but I still don't understand completely if Android System has fine behavior in following case:
I created small app consists of two classes, here is the code:
Main.java
public class Main extends Activity {
private Button bv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bv = (Button) findViewById(R.id.hello_txt);
bv.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Main.this, Main2.class);
startActivity(i);
}
}
);
}
}
Main2.java
public class Main2 extends Activity {
private TextView countOfActivities;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
countOfActivities = new TextView(this);
setContentView(countOfActivities);
countOfActivities.setText("Count of Activities: " + getInstanceCount());
}
}
When I clicked on the button from first activity several times, I get that even after pressing BACK button that should call second Activity's onDestroy() it's instance remains in the memmory.
Only after creating about 35 instances next click let me know, that GC cleared the memmory.
I just want to completely be sure that it is normal system's behavior.
Following pictures from Emulator and LogCat
Button clicked 10 times
LogCat output after clicked
Yes, the system works fine.
When you press the back button, your activity is removed from the activity stack.
onDestroy() may have been called, this doesn't mean that the instance was actually unallocated from the memory.

Force Close when i run this application

I am new to programming.
I'm creating a simple app which will handle my Button click event.
I have added the button using xml and linked it to the program, but the app force closes immediately after I run it.
Here is the code:
public class Sparkling extends Activity implements OnClickListener
{
Button b;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.Button1);
b.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
//actions....
}
}
Without having any stack trace info, my guess would be Button1 is missing from or is misspelled on main.xml or something is wrong with your manifest file.
I found out what had happened.
I was using:
android:name="#+id/Button1"
instead of:
android:id="#+id/Button1"

Android: Button action not being called

I'm trying to make an app where you start at a menu, click a button and are brought to a list of items (which I later hope to make clickable). But I can't seem to make it call my next activity. Can anyone help?
Your main class / activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Menu Button
Button startNewActivity = (Button)findViewById(R.id.startnew);
startNewActivity.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent newActivityIntent = new Intent(YOUR-CLASS-NAME.this,NewActivity.class);
startActivity(newActivityIntent);
}
});
Your NewActivity Class:
public class NewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new);
}
}
Is the question "How do I call the next activity" ?
If so, it's pretty easy - Assuming the Activity you want to call is "SomeActivity", call this:
Intent someActivity = new Intent(getBaseContext(), SomeActivity.class);
startActivity(someActivity);
There's also a "startActivityForResult" method, if you want data back from the Activity you're calling. For reference, the Activity page of the API Documentation can be found here. Good luck!

Categories

Resources