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.
Related
I have popupMenu and CheckBox. I need make write status CheckBox to boolean.
This code not working:
MenuItem fast_result;
boolean fast=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.FastResult:
fast_result = item.getSubMenu().getItem(R.id.FastResult);//This is 182 line
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
}
}
It is errors:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alexvsalex.HelpforMath.RootsActivity.onOptionsItemSelected(RootsActivity.java:182)
at android.app.Activity.onMenuItemSelected(Activity.java:2502)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
at android.widget.AbsListView$1.run(AbsListView.java:3168)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
What to do?
The problem is solved:
case R.id.FastResult:
fast_result = item; //There was an error
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
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.
I have an ArrayList of a custom object with lat and long properties.
I have managed to put markers on the map. What I want is to be able to zoom in to those markers when the map loads.
What I have tried below causes the app to crash.
Here is the code for loading the map:
public void getBridgeData() {
db = new DbHelperClass(this);
bridges = db.getAllBridges();
DecimalFormat dc = new DecimalFormat("#.000");
Builder builder = new LatLngBounds.Builder();
for (int i= 0;i<bridges.size();i++) {
Bridge b= (Bridge)bridges.get(i);
double lati= b.getLatitude();
double longo= b.getLongitude();
double reflat= b.getReflat();
double reflong= b.getReflong();
LatLng start = new LatLng(reflat,reflong);
LatLng end = new LatLng(lati,longo);
GeneralUtils uts = new GeneralUtils();
double ch= uts.calculateDistance(start, end);
String chainage = dc.format(ch);
String mysnipet = "Location:" + b.getRoadname() + " " + chainage;
Marker mm=map.addMarker(new
MarkerOptions().position(new LatLng(lati, longo))
.title(b.getBridgename())
.snippet(mysnipet));
builder.include(mm.getPosition());
}
LatLngBounds bounds= builder.build();
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
Here also part of the code
public class MainActivity extends FragmentActivity {
private GoogleMap map;
private DbHelperClass db;
private List<Bridge> bridges = new ArrayList<Bridge>();
CameraUpdate cu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map);
//AddSomeData();
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if(map!=null) {
getBridgeData();
}
}
}
Here is the error log:
05-30 22:19:28.894: E/AndroidRuntime(5593): FATAL EXCEPTION: main
05-30 22:19:28.894: E/AndroidRuntime(5593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bridge.bridgeinventory/com.bridge.bridgeinventory.MainActivity}: java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1755)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1774)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.access$1500(ActivityThread.java:157)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1001)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Handler.dispatchMessage(Handler.java:130)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Looper.loop(SourceFile:351)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.main(ActivityThread.java:3841)
05-30 22:19:28.894: E/AndroidRuntime(5593): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 22:19:28.894: E/AndroidRuntime(5593): at java.lang.reflect.Method.invoke(Method.java:538)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
05-30 22:19:28.894: E/AndroidRuntime(5593): at dalvik.system.NativeStart.main(Native Method)
05-30 22:19:28.894: E/AndroidRuntime(5593): Caused by: java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.
05-30 22:19:28.894: E/AndroidRuntime(5593): at kbh.b(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at mas.a(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at mal.a(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at mbi.a(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at fms.onTransact(SourceFile:83)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.os.Binder.transact(Binder.java:310)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.bridge.bridgeinventory.MainActivity.getBridgeData(MainActivity.java:148)
05-30 22:19:28.894: E/AndroidRuntime(5593): at com.bridge.bridgeinventory.MainActivity.onCreate(MainActivity.java:44)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
05-30 22:19:28.894: E/AndroidRuntime(5593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1719)
05-30 22:19:28.894: E/AndroidRuntime(5593): ... 11 more
How can I make it work?
The problem is that the map is not loaded yet to the layout that is why you got that error
Solution:
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
});
It will call that method when the map is already loaded to the layout.
I am trying to upload image to Facebook using facebook api.
But getting error please find the code below.
I am unable to find out the error. I tried debugging it but am unable to understand the error looking at the log cat. Please help experts. Thanks in advance.
public void postImageonWall(Bitmap bmpImg) {
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmpImg.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
try {
facebook.request(params);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log Cat:
05-30 05:55:52.489: E/AndroidRuntime(1220): FATAL EXCEPTION: main
05-30 05:55:52.489: E/AndroidRuntime(1220): java.lang.NullPointerException
05-30 05:55:52.489: E/AndroidRuntime(1220): at libcore.net.UriCodec.encode(UriCodec.java:132)
05-30 05:55:52.489: E/AndroidRuntime(1220): at java.net.URLEncoder.encode(URLEncoder.java:50)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.facebook.android.Util.encodeUrl(Util.java:85)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.facebook.android.Util.openUrl(Util.java:145)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.facebook.android.Facebook.request(Facebook.java:717)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.facebook.android.Facebook.request(Facebook.java:633)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.example.dragview.DragActivity.postImageonWall(DragActivity.java:458)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.example.dragview.DragActivity.onOptionsItemSelected(DragActivity.java:428)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.app.Activity.onMenuItemSelected(Activity.java:2548)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.support.v7.app.ActionBarActivity.superOnMenuItemSelected(ActionBarActivity.java:244)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.support.v7.app.ActionBarActivityDelegateICS.onMenuItemSelected(ActionBarActivityDelegateICS.java:165)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:130)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onMenuItemSelected(ActionBarActivityDelegateICS.java:300)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.view.View.performClick(View.java:4204)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.view.View$PerformClick.run(View.java:17355)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.os.Handler.handleCallback(Handler.java:725)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.os.Looper.loop(Looper.java:137)
05-30 05:55:52.489: E/AndroidRuntime(1220): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 05:55:52.489: E/AndroidRuntime(1220): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 05:55:52.489: E/AndroidRuntime(1220): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 05:55:52.489: E/AndroidRuntime(1220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 05:55:52.489: E/AndroidRuntime(1220): at dalvik.system.NativeStart.main(Native Method)
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();