App cannot pass values between activities - java

I am working on an app in which the user inputs a number into one activity, and the number shows up on the next activity. This is what I have so far.
First activity: EnterText.java
public class EnterText extends Activity {
//This is the string that will be passed to the next activity
public static String mynumber = "com.example.ListviewExplorer.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_text);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.enter_text, menu);
return true;
}
//This is where onClick, the button will invoke sendMessage
//sendMessage is supposed to start the next activity
public void sendMessage(View view){
Intent intent = new Intent (this, DisplayText.class);
EditText editText = (EditText) findViewById(R.id.editText1);
mynumber = editText.getText().toString();
intent.putExtra("MyNumber",mynumber);
startActivity(intent);
}
}
Second activity: DisplayText.java
public class DisplayText extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras=getIntent().getExtras();
String mynumber = extras.getString("MyNumber");
TextView textview = (TextView)findViewById(R.id.textView1);
textview.setText(EnterText.mynumber);
setContentView(R.layout.activity_display_text);
// Show the Up button in the action bar.
setupActionBar();
}
}
The problem is that this app crashes when I press the button on the first activity and it calls sendMessage; the second activity isn't even displayed.
Does anyone know why this is occurring?
LogCat is posted below.
Get to it!
05-29 11:57:59.385: W/dalvikvm(3046): threadid=1: thread exiting with uncaught exception (group=0x42d5d140)
05-29 11:57:59.425: E/AndroidRuntime(3046): FATAL EXCEPTION: main
05-29 11:57:59.425: E/AndroidRuntime(3046): Process: com.example.listviewexplorer, PID: 3046
05-29 11:57:59.425: E/AndroidRuntime(3046): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listviewexplorer/com.example.listviewexplorer.DisplayText}: java.lang.NullPointerException
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.os.Handler.dispatchMessage(Handler.java:102)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.os.Looper.loop(Looper.java:136)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-29 11:57:59.425: E/AndroidRuntime(3046): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 11:57:59.425: E/AndroidRuntime(3046): at java.lang.reflect.Method.invoke(Method.java:515)
05-29 11:57:59.425: E/AndroidRuntime(3046): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-29 11:57:59.425: E/AndroidRuntime(3046): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-29 11:57:59.425: E/AndroidRuntime(3046): at dalvik.system.NativeStart.main(Native Method)
05-29 11:57:59.425: E/AndroidRuntime(3046): Caused by: java.lang.NullPointerException
05-29 11:57:59.425: E/AndroidRuntime(3046): at com.example.listviewexplorer.DisplayText.onCreate(DisplayText.java:21)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.Activity.performCreate(Activity.java:5231)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-29 11:57:59.425: E/AndroidRuntime(3046): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-29 11:57:59.425: E/AndroidRuntime(3046): ... 11 more

You need to assign the layout before using it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// assign layout
setContentView(R.layout.activity_display_text);
// then you can use it
TextView textview = (TextView)findViewById(R.id.textView1);
textview.setText(EnterText.mynumber);
...
}
Since the layout has not been defined it is at that stage null and when you try to access Views inside it you get that NullPointerException thrown.

TextView textview = (TextView)findViewById(R.id.textView1);
textview.setText(EnterText.mynumber);
setContentView(R.layout.activity_display_text);
Before setContentView() findViewById() will return null no matter what, and invoking a method on null reference causes the NPE as seen in your stacktrace.
Move the setContentView() above the findViewById(), assuming that textView1 is actually in activity_display_text layout.
This fixes the NPE crash. To actually pass a value, have a look at some of the other answers.

Related

Android NumberPicker NullPointer why?

i am try to implement a numberPicker to select minute values.
But i am getting a NullPointer Exception at this line:
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
Following Code:
public class MainActivity extends Activity {
NumberPicker minutePicker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Auswahl Minuten zum starten / Stoppen aller
minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
minutePicker.setMaxValue(30);
minutePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
abschaltzeit = minutePicker.getValue();
}
});
minutePicker.setValue(0);
minutePicker.setWrapSelectorWheel(false);
}
}
XML:
<NumberPicker
android:id="#+id/minuten_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="6"
android:layout_column="0"
android:paddingLeft="20dp" />
Log:
09-25 11:00:09.749 10687-10687/de.carsten.awesome.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.carsten.awesome.app, PID: 10687
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.carsten.awesome.app/de.carsten.awesome.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at de.carsten.awesome.app.MainActivity.onCreate(MainActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
You're creating a NumberPicker programmatically and then overwriting the reference with whatever findViewById() returns. It returns null if your activity_main layout does not contain a minuten_picker.
Choose only the other: either create it prorgrammatically or find it from a view hierarchy you inflated.
If you choose the programmatic way new NumberPicker(), remember to add it to some layout in your activity view hierarchy, e.g. with setContentView()
If you choose the inflation way, make sure you have the view in your XML layout file.
I'm guessing the NPE you're seeing is actually on the following line where you're trying to invoke a method on the minutePicker and it's null.
I apologize.
I moved the Code from the MainActivity in the creating Fragment and it works with rootView.findView.
Sorry but i am new with the Fragement Konzept.
Thanks a lot for your help !

Programmatically adding different buttons to RelativeLayout using if()

So I am programmatically adding buttons to an already existing RelativeLayout.
I get a fatal error where running this code without any error message.
My android app doesn't even open.
Though when I take out the if/else below it works fine.
Am I not allowed to use an if statement to populate a layout or am I implementing this the wrong way?
*Note that below code is only pseudo code. Everything works as expected when the if/else is commented out
**edit adding full code and logcat
protected void onCreate(Bundle savedInstanceState)
{
//load main screen
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayoutTop = (RelativeLayout)findViewById(R.id.RelativeLayout1);
boolean bool = getBool();
if(bool)
{
//some parameters for the sign in button
RelativeLayout.LayoutParams button1Param = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//make the new button and set attributes
Button button1 = new Button(this);
button1.setText("b1");
button1.setLayoutParams(button1Param);
button1.setId(1);
//create an on click listener to start the activity
button1.setOnClickListener(new OnClickListener() {
//on click execute activity
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), FirstActivity.class));
}
});
}
else
{
//some parameters for the status button
RelativeLayout.LayoutParams statusParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ToggleButton buttonStatus = new ToggleButton(this);
buttonStatus.setText("Status");
buttonStatus.setLayoutParams(statusParam);
buttonStatus.setId(2);
relativeLayoutTop.addView(buttonStatus);
}
}
Log is here:
07-16 10:33:52.900: E/AndroidRuntime(1648): FATAL EXCEPTION: main
07-16 10:33:52.900: E/AndroidRuntime(1648): Process: shout.locate, PID: 1648
07-16 10:33:52.900: E/AndroidRuntime(1648): java.lang.RuntimeException: Unable to start activity ComponentInfo{shout.locate/shout.locate.MainActivity}: java.lang.NullPointerException
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.os.Looper.loop(Looper.java:136)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 10:33:52.900: E/AndroidRuntime(1648): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 10:33:52.900: E/AndroidRuntime(1648): at java.lang.reflect.Method.invoke(Method.java:515)
07-16 10:33:52.900: E/AndroidRuntime(1648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 10:33:52.900: E/AndroidRuntime(1648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 10:33:52.900: E/AndroidRuntime(1648): at dalvik.system.NativeStart.main(Native Method)
07-16 10:33:52.900: E/AndroidRuntime(1648): Caused by: java.lang.NullPointerException
07-16 10:33:52.900: E/AndroidRuntime(1648): at shout.locate.MainActivity.initLayout(MainActivity.java:79)
07-16 10:33:52.900: E/AndroidRuntime(1648): at shout.locate.MainActivity.onCreate(MainActivity.java:57)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.Activity.performCreate(Activity.java:5231)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-16 10:33:52.900: E/AndroidRuntime(1648): ... 11 more
Try this:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayoutTop = (RelativeLayout)findViewById(R.id.RelativeLayout1);
if(someboolean)
{
//set layouparams
//new button1
//button1.setonclicklistener etc etc
//set new layoutparams
//new button2
//button2.setonclicklistener etc etc
}
else
{
//set layouparams
//new button3
//button.setonclicklistener etc etc
}
}
You should get your layout only after setContentView(R.layout.activity_main);
EDIT
You are not adding the button to layout in if statement
v.getContext() might be null.Use YourCurrentActivity.this instead.

how to set listadapter for fragment

I have two framelayout in my main.xml file. I add framelayouts to the class that extends Fragment. my main class extends FragmentActivity and this is Oncreate method of it:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm =getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment f=new Freg1();
Fragment f2=new Freg1();
ft.add(R.id.frame1, f);
ft.add(R.id.frame2, f2);
ft.commit();
tf=Typeface.createFromAsset(this.getAssets(),"font/Byekan.ttf" );
tv1=(TextView) findViewById(R.id.textView1);
tv1.setTypeface(tf);
Log.i(TAG,"1");
lv1=(ListView) findViewById(R.id.listView1);
lv2=(ListView) findViewById(R.id.listView2);
Log.i(TAG,"2");
List<String> stringList = new ArrayList<String>(Arrays.asList(s1));
Log.i(TAG,"3");
ListAdapter listAdapter = new CustomListAdapter(MainActivity.this , R.layout.custom_list ,stringList);
Log.i(TAG,"4");
lv1.setAdapter(listAdapter);
Log.i(TAG,"5");
lv2.setAdapter(listAdapter);
Log.i(TAG,"6");
}
when i run the codes, it crashed after LOG no4. that mean setAdapter() method do not work. how can i resolve this problem?
this is my logcat resource:
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40a13300)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taxitabriz/com.example.taxitabriz.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more
thank for you that help me to resolve problem.
Your ListView lv1 is null as you can see here:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException
....
com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
The line 55 of MainActivity should be this call: lv1.setAdapter(listAdapter);
Make sure that your listView1 is included within the layout and initialized correctly prior to trying to set an Adapter to it.

Intent is not passing

I having a problem in passing the intent. The code is written in proper manner still it is showing errors in the logcat. Any help will be greatfull. Thank you
Have i placed the intent code in the write area???
Here is my main file.
MainActivity.java
public class MainActivity extends Activity {
ProgressBar progressBar;
int progressStatus = 0;
TextView textView1, textView2;
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
textView2 = (TextView) findViewById(R.id.load_per);
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView2.setText(progressStatus + "%");
if (progressStatus == 100) {
Intent i = new Intent(MainActivity.this,
EventActivity.class);
startActivity(i);
}
}
});
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
The logcat is showing errors like this
05-30 13:39:51.296: E/AndroidRuntime(1352): FATAL EXCEPTION: main
05-30 13:39:51.296: E/AndroidRuntime(1352): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.temp9/com.example.temp9.EventActivity}: java.lang.NullPointerException
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.os.Looper.loop(Looper.java:137)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-30 13:39:51.296: E/AndroidRuntime(1352): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 13:39:51.296: E/AndroidRuntime(1352): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 13:39:51.296: E/AndroidRuntime(1352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-30 13:39:51.296: E/AndroidRuntime(1352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-30 13:39:51.296: E/AndroidRuntime(1352): at dalvik.system.NativeStart.main(Native Method)
05-30 13:39:51.296: E/AndroidRuntime(1352): Caused by: java.lang.NullPointerException
05-30 13:39:51.296: E/AndroidRuntime(1352): at com.example.temp9.EventActivity.onCreate(EventActivity.java:22)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.Activity.performCreate(Activity.java:4465)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-30 13:39:51.296: E/AndroidRuntime(1352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
05-30 13:39:51.296: E/AndroidRuntime(1352): ... 11 more
Problem is Caused by: java.lang.NullPointerException
Here com.example.temp9.EventActivity.onCreate(EventActivity.java:22) in EventActivity class' onCreate function at line 22 location you are accessing some null pointer.
Seems like you haven't added EventActivity in AndroidManifest.xml(failed to start activity message in logcat). Make sure you have added the activity in Manifest file.

java.lang.RuntimeException: Unable to start activity ComponentInfo Android Eclipse

I know a bunch of people have asked this same thing but I just don't know what's going on. I'm trying to make a calculator in Eclipse, but I keep getting a list of errors.
There are no errors in the file that the program notices, although there is an error in the layout.xml but it hasn't caused a problem before so that shouldn't cause a problem.
07-30 08:19:50.470: D/AndroidRuntime(2071): Shutting down VM
07-30 08:19:50.470: W/dalvikvm(2071): threadid=1: thread exiting with uncaught
exception (group=0x40a421f8)
07-30 08:19:50.480: E/AndroidRuntime(2071): FATAL EXCEPTION: main
07-30 08:19:50.480: E/AndroidRuntime(2071): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.se.miun.chris.calculator/com.example.se.miun.chris.
calculator.MainActivity}: java.lang.NullPointerException
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.access$600(ActivityThread.java:123)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.os.Handler.dispatchMessage(Handler.java:99)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.os.Looper.loop(Looper.java:137)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.main(ActivityThread.java:4424)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
java.lang.reflect.Method.invokeNative(Native Method)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
java.lang.reflect.Method.invoke(Method.java:511)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
dalvik.system.NativeStart.main(Native Method)
07-30 08:19:50.480: E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
07-30 08:19:50.480: E/AndroidRuntime(2071): at
com.example.se.miun.chris.calculator.MainActivity.onCreate(MainActivity.java:60)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.Activity.performCreate(Activity.java:4465)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-30 08:19:50.480: E/AndroidRuntime(2071): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-30 08:19:50.480: E/AndroidRuntime(2071): ... 11 more
This is my coding. It doesn't really do anything yet, but I wanted to just run it to see if it encountered any errors. This is the mainActivity.java file.
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button Seven;
Button Eight;
Button Nine;
Button Four;
Button Five;
Button Six;
Button One;
Button Two;
Button Three;
Button Zero;
Button Point;
Button Negative;
TextView TextBox;
int x;
int y;
String z;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Seven = (Button)findViewById(R.id.NumberSeven);
Seven.setOnClickListener(this);
Eight = (Button)findViewById(R.id.NumberEight);
Eight.setOnClickListener(this);
Nine = (Button)findViewById(R.id.NumberNine);
Nine.setOnClickListener(this);
Four = (Button)findViewById(R.id.NumberFour);
Four.setOnClickListener(this);
Five = (Button)findViewById(R.id.NumberFive);
Five.setOnClickListener(this);
Six = (Button)findViewById(R.id.NumberSix);
Six.setOnClickListener(this);
One = (Button)findViewById(R.id.NumberOne);
One.setOnClickListener(this);
Two = (Button)findViewById(R.id.NumberTwo);
Two.setOnClickListener(this);
Three = (Button)findViewById(R.id.NumberThree);
Three.setOnClickListener(this);
Zero = (Button)findViewById(R.id.NumberZero);
Zero.setOnClickListener(this);
Point = (Button)findViewById(R.id.Point);
Point.setOnClickListener(this);
Negative = (Button)findViewById(R.id.NNegative);
Negative.setOnClickListener(this);
TextBox = (TextView)findViewById(R.id.Screen);
x = (Integer) null;
y = (Integer) null;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View One) {
if(z == null){
x = 1;
TextBox.setText(x);
TextBox.setText("diggity");
}
else if(z != null) {
y = 1;
TextBox.setText(x);
TextBox.setText(z);
TextBox.setText(y);
}
}
}
line 60: x = (Integer) null;
This line will compile to this bytecode (disassembled by javap):
aconst_null
checkcast #2; //class java/lang/Integer
invokevirtual #3; //Method java/lang/Integer.intValue:()I
Third line will cause a NullPointerException becouse Integer object is actually your null constant :)
Primitive data types (int, long etc.) is the only non-object types in Java. null is used to show that the current variable (Object variable) is not backed by the actual object (no memory was allocated). For primitive types memory allocates immediately so they cant have this null state.
So you should check for "if(x == 0)" or define it as Integer.
P.S. And don't cast null to anything :)
It's like this
you're not getting much errors because the application can't launch. it cannot launch because it's onCreate() cannot finish.
onCreate() cannot finish because of a nullPointerException.
you cast null into integer twice, instead of simply instantiating a new integer which will default to 0. once you get rid of that, it should work.
see?
E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
E/AndroidRuntime(2071): at
MainActivity.onCreate(MainActivity.java:60)
and i bet that this is line 60
x = (Integer) null;
so change it to
x = new Integer();

Categories