java.lang NULL POINT Exception [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
It say there's error in line 71 but i could figure what error was it any kind souls willing to help me ? THANKS
my problem is that when i click on the foodbutton on the mainactivity , the system crashes. BUT the logcat shows the error in food.java which is another page.
Here's the coding !
package com.yiqiexample.cabincrew;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
//import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Food extends ListActivity
implements OnClickListener {
// Progress Dialog
private ProgressDialog pDialog;
//testing on Emulator:
private static final String READ_COMMENTS_URL = "http://10.0.2.2/pbda2/foodordered.php";
// private CheckBox chkFood, chkDrinks, chkServices;
//private Button btnDisplay, chkClear, deliever, chkClearFood, fooddeliever, drinksdeliever, servicesdeliever, chkClearDrinks, chkClearServices;
//private TextView clearThis,orderdisplay, clearThisFood, foodorderdisplay, drinksorderdisplay, servicesorderdisplay, clearThisDrinks, clearThisServices;
private static final String TAG_SUCCESS = "success";
private static final String TAG_POSTS = "posts";
private static final String TAG_SEATNUMBER = "seatnumber";
private static final String TAG_FOODORDERED = "foodordered";
//it's important to note that the message is both in the parent branch of
//our JSON tree that displays a "Post Available" or a "No Post Available" message,
//and there is also a message for each individual post, listed under the "posts"
//category, that displays what the user typed as their message.
//An array of all of our comments
private JSONArray mComments = null;
//manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.food);
View v = findViewById(R.id.backmain);
//set event listener
v.setOnClickListener(this);
View z = findViewById(R.id.drinksbtn);
//set event listener
z.setOnClickListener(this);
View x = findViewById(R.id.servicebtn);
//set event listener
x.setOnClickListener(this);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//loading the comments via AsyncTask
new LoadComments().execute();
}
/**
* Retrieves recent post data from the server.
*/
public void updateJSONdata() {
// Instantiate the arraylist to contain all the JSON data.
// we are going to use a bunch of key-value pairs, referring
// to the json element name, and the content, for example,
// message it the tag, and "I'm awesome" as the content..
mCommentList = new ArrayList<HashMap<String, String>>();
// Bro, it's time to power up the J parser
JSONParser jParser = new JSONParser();
// Feed the beast our comments url, and it spits us
//back a JSON object. Boo-yeah Jerome.
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
//when parsing JSON stuff, we should probably
//try to catch any exceptions:
try {
//I know I said we would check if "Posts were Avail." (success==1)
//before we tried to read the individual posts, but I lied...
//mComments will tell us how many "posts" or comments are
//available
mComments = json.getJSONArray(TAG_POSTS);
// looping through all posts according to the json object returned
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
//gets the content of each tag
String seatnumber = c.getString(TAG_SEATNUMBER);
String foodordered = c.getString(TAG_FOODORDERED);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_SEATNUMBER, seatnumber);
map.put(TAG_FOODORDERED, foodordered);
// adding HashList to ArrayList
mCommentList.add(map);
//annndddd, our JSON data is up to date same with our array list
}
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.backmain){
//define a new Intent for the second Activity
Intent intent = new Intent(this,MainActivity.class);
//start the second Activity
this.startActivity(intent);
}
if(arg0.getId() == R.id.drinksbtn){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Drinks.class);
//start the second Activity
this.startActivity(intent);
}
if(arg0.getId() == R.id.servicebtn){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Services.class);
//start the second Activity
this.startActivity(intent);
}
}
/**
* Inserts the parsed data into the listview.
*/
private void updateList() {
// For a ListActivity we need to set the List Adapter, and in order to do
//that, we need to create a ListAdapter. This SimpleAdapter,
//will utilize our updated Hashmapped ArrayList,
//use our single_post xml template for each item in our list,
//and place the appropriate info from the list to the
//correct GUI id. Order is important here.
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_SEATNUMBER, TAG_FOODORDERED
//TAG_DRINKSORDERED, TAG_SERVICES
}, new int[] { R.id.seatnumber, R.id.orders
//R.id.drinkstv, R.id.servicestv,
});
// I shouldn't have to comment on this one:
setListAdapter(adapter);
// Optional: when the user clicks a list item we
//could do something. However, we will choose
//to do nothing...
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// This method is triggered if an item is click within our
// list. For our example we won't be using this, but
// it is useful to know in real life applications.
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Food.this);
pDialog.setMessage("Loading orders...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected Boolean doInBackground(Void... arg0) {
//we will develop this method in version 2
updateJSONdata();
return null;
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
//we will develop this method in version 2
updateList();
}
}
}
Here's the coding for main page
package com.yiqiexample.cabincrew;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity
implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View v = findViewById(R.id.foodbutton);
//set event listener
v.setOnClickListener(this);
View x= findViewById(R.id.drinks);
//set event listener
x.setOnClickListener(this);
View y = findViewById(R.id.services);
//set event listener
y.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.foodbutton){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Food.class);
//start the second Activity
this.startActivity(intent);
}
if(arg0.getId() == R.id.drinks){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Drinks.class);
//start the second Activity
this.startActivity(intent);
}
if(arg0.getId() == R.id.services){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Services.class);
//start the second Activity
this.startActivity(intent);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
food.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android1:id="#+id/bg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android1:background="#E0FFFF" >
<Button
android1:id="#+id/backmain"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_alignParentBottom="true"
android1:layout_alignParentRight="true"
android1:text="#string/backtomain" />
<Button
android1:id="#+id/servicesdelivered"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_above="#+id/foodbtn"
android1:layout_alignLeft="#+id/backmain"
android1:text="#string/servicesdelivered"
android1:visibility="invisible" />
<Button
android:id="#+id/servicebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="358dp"
android:layout_toRightOf="#+id/foodbutton"
android:text="SERVICES" />
<Button
android1:id="#+id/foodbtn"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_alignParentBottom="true"
android1:layout_marginBottom="23dp"
android1:layout_marginRight="24dp"
android1:text="DRINKS ORDERS" />
<LinearLayout
android:id="#+id/top_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android1:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/foodtitle"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top_layover"
android1:layout_alignBottom="#+id/servicesdelivered"
android:background="#fff"
android:divider="#android:color/transparent"
android:scrollbars="none" />
<LinearLayout
android:id="#+id/bottom_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here's the LOGCAT !
11-03 04:23:36.974: W/dalvikvm(756): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-03 04:23:37.093: E/AndroidRuntime(756): FATAL EXCEPTION: main
11-03 04:23:37.093: E/AndroidRuntime(756): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yiqiexample.cabincrew/com.yiqiexample.cabincrew.Food}: java.lang.NullPointerException
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.os.Looper.loop(Looper.java:137)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-03 04:23:37.093: E/AndroidRuntime(756): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 04:23:37.093: E/AndroidRuntime(756): at java.lang.reflect.Method.invoke(Method.java:525)
11-03 04:23:37.093: E/AndroidRuntime(756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-03 04:23:37.093: E/AndroidRuntime(756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 04:23:37.093: E/AndroidRuntime(756): at dalvik.system.NativeStart.main(Native Method)
11-03 04:23:37.093: E/AndroidRuntime(756): Caused by: java.lang.NullPointerException
11-03 04:23:37.093: E/AndroidRuntime(756): at com.yiqiexample.cabincrew.Food.onCreate(Food.java:71)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.Activity.performCreate(Activity.java:5133)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-03 04:23:37.093: E/AndroidRuntime(756): ... 11 more
11-03 04:23:43.713: I/Process(756): Sending signal. PID: 756 SIG: 9
11-03 04:23:46.715: D/dalvikvm(895): GC_FOR_ALLOC freed 44K, 4% free 2600K/2708K, paused 57ms, total 60ms
11-03 04:23:46.753: I/dalvikvm-heap(895): Grow heap (frag case) to 4.941MB for 2457616-byte allocation
11-03 04:23:46.893: D/dalvikvm(895): GC_FOR_ALLOC freed 2K, 3% free 4998K/5112K, paused 130ms, total 130ms
11-03 04:23:47.793: D/gralloc_goldfish(895): Emulator without GPU emulation detected.
11-03 04:31:41.842: I/Choreographer(895): Skipped 125 frames! The application may be doing too much work on its main thread.
11-03 04:31:42.284: I/Choreographer(895): Skipped 62 frames! The application may be doing too much work on its main thread.
11-03 04:31:42.913: I/Choreographer(895): Skipped 71 frames! The application may be doing too much work on its main thread.
11-03 04:32:15.008: D/dalvikvm(895): GC_FOR_ALLOC freed 143K, 4% free 5688K/5892K, paused 138ms, total 186ms
11-03 04:32:16.063: I/Choreographer(895): Skipped 91 frames! The application may be doing too much work on its main thread.
11-03 04:32:16.603: I/Choreographer(895): Skipped 58 frames! The application may be doing too much work on its main thread.
11-03 04:32:42.023: I/Choreographer(895): Skipped 46 frames! The application may be doing too much work on its main thread.
11-03 04:32:44.223: I/Choreographer(895): Skipped 30 frames! The application may be doing too much work on its main thread.
11-03 04:32:45.053: I/Choreographer(895): Skipped 122 frames! The application may be doing too much work on its main thread.
11-03 04:32:45.513: I/Choreographer(895): Skipped 68 frames! The application may be doing too much work on its main thread.
11-03 04:32:46.555: I/Choreographer(895): Skipped 135 frames! The application may be doing too much work on its main thread.
11-03 04:32:49.742: D/dalvikvm(895): GC_FOR_ALLOC freed 152K, 4% free 6327K/6544K, paused 350ms, total 378ms
11-03 04:32:50.245: D/dalvikvm(895): GC_FOR_ALLOC freed 59K, 4% free 7253K/7540K, paused 187ms, total 190ms
11-03 04:32:50.349: I/Choreographer(895): Skipped 192 frames! The application may be doing too much work on its main thread.
11-03 04:32:54.232: I/Choreographer(895): Skipped 35 frames! The application may be doing too much work on its main thread.
11-03 04:32:54.702: I/Choreographer(895): Skipped 73 frames! The application may be doing too much work on its main thread.
11-03 04:32:55.134: I/Choreographer(895): Skipped 71 frames! The application may be doing too much work on its main thread.
11-03 04:32:55.968: I/Choreographer(895): Skipped 112 frames! The application may be doing too much work on its main thread.
11-03 04:32:59.177: D/dalvikvm(895): GC_FOR_ALLOC freed 1715K, 21% free 6795K/8580K, paused 279ms, total 332ms
11-03 04:32:59.322: I/Choreographer(895): Skipped 134 frames! The application may be doing too much work on its main thread.
11-03 04:33:03.003: I/Choreographer(895): Skipped 53 frames! The application may be doing too much work on its main thread.
11-03 04:33:04.183: I/Choreographer(895): Skipped 35 frames! The application may be doing too much work on its main thread.
11-03 04:33:04.643: I/Choreographer(895): Skipped 35 frames! The application may be doing too much work on its main thread.
11-03 04:33:08.103: I/Choreographer(895): Skipped 43 frames! The application may be doing too much work on its main thread.
11-03 04:34:58.453: D/AndroidRuntime(951): Shutting down VM
11-03 04:34:58.453: W/dalvikvm(951): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-03 04:34:58.464: E/AndroidRuntime(951): FATAL EXCEPTION: main
11-03 04:34:58.464: E/AndroidRuntime(951): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.IllegalStateException: Unable to get package info for com.yiqiexample.cabincrew; is package not installed?
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.LoadedApk.makeApplication(LoadedApk.java:509)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4417)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.os.Looper.loop(Looper.java:137)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-03 04:34:58.464: E/AndroidRuntime(951): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 04:34:58.464: E/AndroidRuntime(951): at java.lang.reflect.Method.invoke(Method.java:525)
11-03 04:34:58.464: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-03 04:34:58.464: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 04:34:58.464: E/AndroidRuntime(951): at dalvik.system.NativeStart.main(Native Method)
11-03 04:34:58.464: E/AndroidRuntime(951): Caused by: java.lang.IllegalStateException: Unable to get package info for com.yiqiexample.cabincrew; is package not installed?
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:369)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.LoadedApk.makeApplication(LoadedApk.java:501)
11-03 04:34:58.464: E/AndroidRuntime(951): ... 11 more
11-03 04:35:10.544: D/dalvikvm(994): GC_FOR_ALLOC freed 55K, 5% free 2600K/2720K, paused 30ms, total 33ms
11-03 04:35:10.562: I/dalvikvm-heap(994): Grow heap (frag case) to 4.940MB for 2457616-byte allocation
11-03 04:35:10.673: D/dalvikvm(994): GC_FOR_ALLOC freed 2K, 3% free 4998K/5124K, paused 110ms, total 110ms
11-03 04:35:11.213: D/gralloc_goldfish(994): Emulator without GPU emulation detected.
11-03 04:40:44.032: D/AndroidRuntime(994): Shutting down VM
11-03 04:40:44.042: W/dalvikvm(994): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-03 04:40:44.112: E/AndroidRuntime(994): FATAL EXCEPTION: main
11-03 04:40:44.112: E/AndroidRuntime(994): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yiqiexample.cabincrew/com.yiqiexample.cabincrew.Food}: java.lang.NullPointerException
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.os.Looper.loop(Looper.java:137)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-03 04:40:44.112: E/AndroidRuntime(994): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 04:40:44.112: E/AndroidRuntime(994): at java.lang.reflect.Method.invoke(Method.java:525)
11-03 04:40:44.112: E/AndroidRuntime(994): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-03 04:40:44.112: E/AndroidRuntime(994): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 04:40:44.112: E/AndroidRuntime(994): at dalvik.system.NativeStart.main(Native Method)
11-03 04:40:44.112: E/AndroidRuntime(994): Caused by: java.lang.NullPointerException
11-03 04:40:44.112: E/AndroidRuntime(994): at com.yiqiexample.cabincrew.Food.onCreate(Food.java:71)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.Activity.performCreate(Activity.java:5133)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-03 04:40:44.112: E/AndroidRuntime(994): ... 11 more
if you need more coding to spot the error please tell me ! :)

Assuming line 71 is this one.-
z.setOnClickListener(this);
this line must be setting z to null.
View z = findViewById(R.id.drinksbtn);
Double check that food layout contains a view with id drinksbtn with no typos.
Notice that there's no drinksbtn in your xml, nor foodbutton neither.

Related

AChartEngine - Charts with data from parse.com won't display

I would like to create applications forming charts based on data from parse.com. I have read some examples and tutorials but still have problem with displaying charts. Below is my code:
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import java.util.ArrayList;
public class LineGraph {
public ArrayList<Integer> dataArray;
XYMultipleSeriesDataset dataset;
XYMultipleSeriesRenderer renderer;
public static boolean ClickEnabled = true;
public Intent getIntent(Context context) {
ArrayList<Integer> y = this.dataArray;
XYSeries seriesY = new XYSeries("Y");
for (int i = 0; i < y.size(); i++) {
seriesY.add(i, y.get(i));
}
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesY);
renderer.setPanEnabled(true, false);
renderer.setClickEnabled(ClickEnabled);
renderer.setBackgroundColor(Color.WHITE);
renderer.setApplyBackgroundColor(true);
renderer.setChartTitle("Simple data");
renderer.setAxesColor(Color.BLACK);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.RED);
renderer.setPointStyle(PointStyle.DIAMOND);
mRenderer.addSeriesRenderer(renderer);
Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Line Graph Title");
return intent;
}
public void getData() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Counters_data");
query.getInBackground("lxFzCTeOcl", new GetCallback<ParseObject>() {
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
String object = parseObject.getString("value");
Integer objectValue = Integer.parseInt(object);
if (dataArray == null) {
dataArray = new ArrayList<Integer>();
dataArray.add(objectValue);
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
}
And there is how I invoke charts:
public void lineGraphHandler(View view) {
LineGraph line = new LineGraph();
line.getData();
Intent lineIntent = line.getIntent(this);
startActivity(lineIntent);
}
And XML part:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/counters"
android:onClick="lineGraphHandler"
android:text="Charts"
android:id="#+id/charts"/>
There is my logcat:
03-26 08:42:13.096 1229-1229/com.example.tst D/dalvikvm﹕ Late-enabling
CheckJNI 03-26 08:42:13.487 1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libEGL_genymotion.so 03-26 08:42:13.491
1229-1229/com.example.tst D/﹕ HostConnection::get() New Host
Connection established 0xb94f4270, tid 1229 03-26 08:42:13.551
1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libGLESv1_CM_genymotion.so 03-26 08:42:13.551
1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libGLESv2_genymotion.so 03-26 08:42:14.035
1229-1229/com.example.tst W/EGL_genymotion﹕ eglSurfaceAttrib not
implemented 03-26 08:42:14.039 1229-1229/com.example.tst
E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache 03-26
08:42:14.043 1229-1229/com.example.tst E/OpenGLRenderer﹕
MAX_TEXTURE_SIZE: 4096 03-26 08:42:14.055 1229-1229/com.example.tst
E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from
Caches::initConstraints() 03-26 08:42:14.063 1229-1229/com.example.tst
E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 4096 03-26 08:42:14.063
1229-1229/com.example.tst D/OpenGLRenderer﹕ Enabling debug mode 0
03-26 08:42:50.327 1229-1229/com.example.tst D/dalvikvm﹕ GC_FOR_ALLOC
freed 200K, 8% free 2975K/3228K, paused 10ms, total 13ms 03-26
08:42:51.675 1229-1229/com.example.tst D/dalvikvm﹕ GC_FOR_ALLOC freed
431K, 14% free 3056K/3540K, paused 22ms, total 28ms 03-26 08:42:52.043
1229-1229/com.example.tst W/EGL_genymotion﹕ eglSurfaceAttrib not
implemented 03-26 08:42:53.543 1229-1229/com.example.tst
I/Choreographer﹕ Skipped 89 frames! The application may be doing too
much work on its main thread. 03-26 08:43:01.747
1229-1229/com.example.tst D/AndroidRuntime﹕ Shutting down VM 03-26
08:43:01.747 1229-1229/com.example.tst W/dalvikvm﹕ threadid=1: thread
exiting with uncaught exception (group=0xa4d8fb20) 03-26 08:43:01.767
1229-1229/com.example.tst E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tst, PID: 1229 java.lang.IllegalStateException:
Could not execute method of the activity at
android.view.View$1.onClick(View.java:3823) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5017) 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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at
dalvik.system.NativeStart.main(Native Method) Caused by:
java.lang.reflect.InvocationTargetException at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
android.view.View$1.onClick(View.java:3818) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method) Caused by:
java.lang.NullPointerException at
com.example.tst.LineGraph.getIntent(LineGraph.java:36) at
com.example.tst.MainActivity.lineGraphHandler(MainActivity.java:44)
at java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
android.view.View$1.onClick(View.java:3818) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method) 03-26 08:43:04.507
1229-1229/com.example.tst I/Process﹕ Sending signal. PID: 1229 SIG: 9
I don't understand where the problem is. My app starts but crashes immediately when I push "chart" button. Is it data type of problem or because I misunderstand something?
Thank you in advance.
I tried like this but still got crash:
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
String object = parseObject.getString("value");
Integer objectValue = Integer.parseInt(object);
if (dataArray == null) {
dataArray = new ArrayList<Integer>();
dataArray.add(objectValue);
ArrayList<Integer> y = dataArray;
XYSeries seriesY = new XYSeries("Y");
for (int i = 0; i < y.size(); i++) {
seriesY.add(i, y.get(i));
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesY);
}
}
Your getData() retrieves the data asynchronously. dataArray won't be initialized immediately when you call getIntent().
Wait for the async operation to complete before using the data there. For example, call the code requiring that data from the done() callback.

Hiding Keyboard on Android crashes app

When I try to run this line in order to hide the keyboard (I get the InputMethodManager):
this.context = context;
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); //this line crashes the app
What should I do to fix it?
(I am running it from a fragment by the way)
Crash Log:
11-03 16:20:26.700: D/AndroidRuntime(2809): Shutting down VM
11-03 16:20:26.700: W/dalvikvm(2809): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-03 16:20:26.710: E/AndroidRuntime(2809): FATAL EXCEPTION: main
11-03 16:20:26.710: E/AndroidRuntime(2809): java.lang.NullPointerException
11-03 16:20:26.710: E/AndroidRuntime(2809): at co.emuze.tabtest1.Tab1$1.onClick(Tab1.java:32)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.view.View.performClick(View.java:4084)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.view.View$PerformClick.run(View.java:16966)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Handler.handleCallback(Handler.java:615)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Handler.dispatchMessage(Handler.java:92)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Looper.loop(Looper.java:137)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-03 16:20:26.710: E/AndroidRuntime(2809): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 16:20:26.710: E/AndroidRuntime(2809): at java.lang.reflect.Method.invoke(Method.java:511)
11-03 16:20:26.710: E/AndroidRuntime(2809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-03 16:20:26.710: E/AndroidRuntime(2809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 16:20:26.710: E/AndroidRuntime(2809): at dalvik.system.NativeStart.main(Native Method)
Full on click method:
public void onClick(View v) {
text1.setText(editText1.getText().toString());
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
}
It would appear context is null - you show the line this.context = context, and if the right hand side context is not a variable local to that method you're doing nothing. I think what you may be looking for is context = getApplicationContext()
Dont forget to use try catch blog because in case when your keyboard not open and if you use key key board hide code app crashed
User below code in Fragment
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}

Android - NullPointerException on EditText field

I've been following the Android tutorials and created MyFirstApp (see http://developer.android.com/training/basics/firstapp/index.html) and I can launch the app ok on the emulator, but upon entering a message and hitting "send" I get an IllegalStateException caused by a NullPointerException at this line in the below code:
String message = editText.getText().toString();
Upon debugging, it is clear that editText is null at this point, but I cannot see why, or anything I've missed in the tutorial.
My MainActivity class:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
/**
* <p>Main Activity class for the App</p>
*
* #author - thebloodguy
*/
public class MainActivity extends Activity {
//~ ----------------------------------------------------------------------------------------------------------------
//~ Static fields/initializers
//~ ----------------------------------------------------------------------------------------------------------------
/** Key to the extra message data in the sendMessage intent */
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
//~ ----------------------------------------------------------------------------------------------------------------
//~ Methods
//~ ----------------------------------------------------------------------------------------------------------------
/**
* {#inheritDoc}
*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/* Inflate the menu; this adds items to the action bar if it is present */
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* <p>Send a message using the data in the {#link View}</p>
*
* #param view - the {#link View} object representing the state of the view when the message is sent
*/
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) view.findViewById(R.id.edit_message);
String message = editText.getText().toString(); // This is the guilty line
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/**
* {#inheritDoc}
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
My activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
and the LogCat output:
12-21 10:02:32.210: E/Trace(621): error opening trace file: No such file or directory (2)
12-21 10:02:32.250: W/ActivityThread(621): Application com.example.myfirstapp is waiting for the debugger on port 8100...
12-21 10:02:32.270: I/System.out(621): Sending WAIT chunk
12-21 10:02:32.280: I/dalvikvm(621): Debugger is active
12-21 10:02:32.480: I/System.out(621): Debugger has connected
12-21 10:02:32.480: I/System.out(621): waiting for debugger to settle...
12-21 10:02:32.680: I/System.out(621): waiting for debugger to settle...
12-21 10:02:32.889: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.091: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.290: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.534: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.796: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.990: I/System.out(621): waiting for debugger to settle...
12-21 10:02:34.204: I/System.out(621): debugger has settled (1353)
12-21 10:02:35.429: D/gralloc_goldfish(621): Emulator without GPU emulation detected.
12-21 10:05:11.510: I/Choreographer(621): Skipped 94 frames! The application may be doing too much work on its main thread.
12-21 10:05:13.850: I/Choreographer(621): Skipped 35 frames! The application may be doing too much work on its main thread.
12-21 10:05:15.571: I/Choreographer(621): Skipped 38 frames! The application may be doing too much work on its main thread.
12-21 10:06:53.724: D/AndroidRuntime(621): Shutting down VM
12-21 10:06:53.724: W/dalvikvm(621): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-21 10:06:53.840: E/AndroidRuntime(621): FATAL EXCEPTION: main
12-21 10:06:53.840: E/AndroidRuntime(621): java.lang.IllegalStateException: Could not execute method of the activity
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$1.onClick(View.java:3591)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View.performClick(View.java:4084)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$PerformClick.run(View.java:16966)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Handler.handleCallback(Handler.java:615)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Looper.loop(Looper.java:137)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:06:53.840: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-21 10:06:53.840: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-21 10:06:53.840: E/AndroidRuntime(621): at dalvik.system.NativeStart.main(Native Method)
12-21 10:06:53.840: E/AndroidRuntime(621): Caused by: java.lang.reflect.InvocationTargetException
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$1.onClick(View.java:3586)
12-21 10:06:53.840: E/AndroidRuntime(621): ... 11 more
12-21 10:06:53.840: E/AndroidRuntime(621): Caused by: java.lang.NullPointerException
12-21 10:06:53.840: E/AndroidRuntime(621): at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:61)
12-21 10:06:53.840: E/AndroidRuntime(621): ... 14 more
Change this line
EditText editText = (EditText) view.findViewById(R.id.edit_message);
to
EditText editText = (EditText)findViewById(R.id.edit_message);
here in sendMessage method, view parameter is the Button being clicked, and EditText is not child of this view, so you need to call findViewById from the activity content view, which you call by
EditText editText = (EditText) findViewById(R.id.edit_message);

FATAL EXCEPTION: main on Android 4.x but not Android 2.X

I am getting some errors when trying to run my android app on an emulator that is running any version of Android 4.x but it works fine on any version of Android 2.X.
package com.EXCLUDED_FOR_PRIVACY.EXCLUDED_FOR_PRIVACY;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.ImageView;
import android.graphics.Color;
public class EXCLUDED_FOR_PRIVACY extends Activity {
public int WebView1Bool=0;
public int WebView2Bool=0;
public int WebView3Bool=0;
public int WebView4Bool=0;
private TabHost tabs;
private void setupTabHost() {
tabs = (TabHost) findViewById(R.id.my_tabhost);
tabs.setup();
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupTabHost();
tabs.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
setupTab(new ImageView(this), 1);
setupTab(new ImageView(this), 2);
setupTab(new ImageView(this), 3);
setupTab(new ImageView(this), 4);
WebView mWebView1;
mWebView1 = (WebView) findViewById(R.id.webview1);
mWebView1.setBackgroundColor(Color.parseColor("#a3a3a3"));
mWebView1.setBackgroundColor(0);
mWebView1.getSettings().setJavaScriptEnabled(true);
mWebView1.setWebViewClient(new MobileClient());
mWebView1.loadUrl("EXCLUDED_FOR_PRIVACY");
WebView1Bool=1;
tabs.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
tabs.setCurrentTab(0);
if(WebView1Bool==0){
WebView mWebView1;
mWebView1 = (WebView) findViewById(R.id.webview1);
mWebView1.setBackgroundColor(Color.parseColor("#a3a3a3"));
mWebView1.setBackgroundColor(0);
mWebView1.getSettings().setJavaScriptEnabled(true);
mWebView1.setWebViewClient(new MobileClient());
mWebView1.loadUrl("EXCLUDED_FOR_PRIVACY");
WebView1Bool=1;
}
}
});
tabs.getTabWidget().getChildAt(2).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
tabs.setCurrentTab(1);
if(WebView2Bool==0){
WebView mWebView2;
mWebView2 = (WebView) findViewById(R.id.webview2);
mWebView2.setBackgroundColor(Color.parseColor("#a3a3a3"));
mWebView2.setBackgroundColor(0);
mWebView2.getSettings().setJavaScriptEnabled(true);
mWebView2.setWebViewClient(new MobileClient());
mWebView2.loadUrl("EXCLUDED_FOR_PRIVACY");
WebView2Bool=1;
}
}
});
tabs.getTabWidget().getChildAt(4).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
tabs.setCurrentTab(2);
if(WebView3Bool==0){
WebView mWebView3 = (WebView) findViewById(R.id.webview3);
mWebView3.setBackgroundColor(Color.parseColor("#a3a3a3"));
mWebView3.setBackgroundColor(0);
mWebView3.getSettings().setJavaScriptEnabled(true);
mWebView3.setWebViewClient(new MobileClient());
mWebView3.loadUrl("EXCLUDED_FOR_PRIVACY");
WebView3Bool=1;
}
}
});
tabs.getTabWidget().getChildAt(6).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
tabs.setCurrentTab(3);
if(WebView4Bool==0){
WebView mWebView4;
mWebView4 = (WebView) findViewById(R.id.webview4);
mWebView4.setBackgroundColor(Color.parseColor("#a3a3a3"));
mWebView4.setBackgroundColor(0);
mWebView4.getSettings().setJavaScriptEnabled(true);
mWebView4.setWebViewClient(new MobileClient());
mWebView4.loadUrl("EXCLUDED_FOR_PRIVACY");
WebView4Bool=1;
}
}
});
}
private class MobileClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
private void setupTab(final View view, final int i) {
View createTabView_view = LayoutInflater.from(tabs.getContext()).inflate(R.layout.tabs_bg, null);
ImageView createTabView_tv = (ImageView) createTabView_view.findViewById(R.id.tabsImage);
if(i==1){
TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
createTabView_tv.setImageResource(R.drawable.ic_discounts);
tabSpec.setIndicator(createTabView_view);
tabSpec.setContent(R.id.webview1);
tabs.addTab(tabSpec);
} else if(i==2){
TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
createTabView_tv.setImageResource(R.drawable.ic_specials);
tabSpec.setIndicator(createTabView_view);
tabSpec.setContent(R.id.webview2);
tabs.addTab(tabSpec);
} else if(i==3){
TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
createTabView_tv.setImageResource(R.drawable.ic_directory);
tabSpec.setIndicator(createTabView_view);
tabSpec.setContent(R.id.webview3);
tabs.addTab(tabSpec);
} else if(i==4){
TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
createTabView_tv.setImageResource(R.drawable.ic_membership);
tabSpec.setIndicator(createTabView_view);
tabSpec.setContent(R.id.webview4);
tabs.addTab(tabSpec);
}
}
}
Here are the errors I am seeing
09-24 13:11:25.329: E/Trace(1151): error opening trace file: No such file or directory (2)
09-24 13:11:25.539: D/dalvikvm(1151): GC_FOR_ALLOC freed 43K, 4% free 8316K/8583K, paused 42ms, total 45ms
09-24 13:11:25.549: I/dalvikvm-heap(1151): Grow heap (frag case) to 8.829MB for 691216-byte allocation
09-24 13:11:25.669: D/dalvikvm(1151): GC_CONCURRENT freed <1K, 4% free 8990K/9287K, paused 75ms+6ms, total 124ms
09-24 13:11:25.829: D/gralloc_goldfish(1151): Emulator without GPU emulation detected.
09-24 13:11:26.209: I/Choreographer(1151): Skipped 42 frames! The application may be doing too much work on its main thread.
09-24 13:11:26.379: I/Choreographer(1151): Skipped 44 frames! The application may be doing too much work on its main thread.
09-24 13:11:27.509: I/Choreographer(1151): Skipped 54 frames! The application may be doing too much work on its main thread.
09-24 13:11:28.319: D/AndroidRuntime(1151): Shutting down VM
09-24 13:11:28.319: W/dalvikvm(1151): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-24 13:11:28.379: E/AndroidRuntime(1151): FATAL EXCEPTION: main
09-24 13:11:28.379: E/AndroidRuntime(1151): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.EXCLUDED.EXCLUDED/com.EXCLUDED.EXCLUDED.EXCLUDED}: java.lang.NullPointerException
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.os.Looper.loop(Looper.java:137)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-24 13:11:28.379: E/AndroidRuntime(1151): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 13:11:28.379: E/AndroidRuntime(1151): at java.lang.reflect.Method.invoke(Method.java:511)
09-24 13:11:28.379: E/AndroidRuntime(1151): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-24 13:11:28.379: E/AndroidRuntime(1151): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-24 13:11:28.379: E/AndroidRuntime(1151): at dalvik.system.NativeStart.main(Native Method)
09-24 13:11:28.379: E/AndroidRuntime(1151): Caused by: java.lang.NullPointerException
09-24 13:11:28.379: E/AndroidRuntime(1151): at com.EXCLUDED.EXCLUDED.EXCLUDED.onCreate(EXCLUDED.java:85)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.Activity.performCreate(Activity.java:5008)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-24 13:11:28.379: E/AndroidRuntime(1151): ... 11 more
09-24 13:11:30.289: I/Process(1151): Sending signal. PID: 1151 SIG: 9
I apologize if this is something really simple that any java developer would know, I have to admit I barely know what I am doing in Java but am under the gun to get this built. Thank you very much for any help.
Get rid of all the tabs.getTabWidget().getChildAt(...).setOnClickListener() crap, replacing it with a single TabHost.OnTabChangeListener that you register with your TabHost, to find out when the user changes tabs. Your existing code assumes a particular internal structure of a TabWidget, and that assumption is unreliable, as you are discovering.

check if checkbox is checked return null pointer

I have this code for an expandable list, I want to have a checkbox in the childgroups of the list view, and check if one of the checkboxes is checked.
The problem is that when I check if the checkbox is checked I get a NULL Pointer Exception.
Can you please tell me whats wrong?
here's my code - I've edited the code to inflate a view of the child_row.xml that holds that checkbox but I still get that null pointer, what am I doing wrong?!?!
package send.Shift;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class Shifts extends ExpandableListActivity implements
OnCheckedChangeListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list);
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), R.layout.group_row,
new String[] { "Group Item" }, new int[] { R.id.row_name },
createChildList(), R.layout.child_row,
new String[] { "Sub Item" }, new int[] { R.id.grp_child });
getExpandableListView().setGroupIndicator(
getResources().getDrawable(R.drawable.expander_group));
ExpandableListView EX = (ExpandableListView) findViewById(android.R.id.list);
EX.setAdapter(expListAdapter);
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
final TextView choosenGroup = (TextView) findViewById(R.id.choosen);
LayoutInflater inflater = LayoutInflater.from(Shifts.this);
View view2 = inflater.inflate(R.layout.child_row, null);
childBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(childBox.isChecked() == true){
choosenGroup.setText("Shift Set");
}
}
});
}
Here is some of the Logcat log:
12-23 07:38:00.644: W/dalvikvm(880): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-23 07:38:00.654: E/AndroidRuntime(880): FATAL EXCEPTION: main
12-23 07:38:00.654: E/AndroidRuntime(880): java.lang.RuntimeException: Unable to start activity ComponentInfo{send.Shift/send.Shift.Shifts}: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Looper.loop(Looper.java:123)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invoke(Method.java:507)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-23 07:38:00.654: E/AndroidRuntime(880): at dalvik.system.NativeStart.main(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): Caused by: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at send.Shift.Shifts.onCreate(Shifts.java:41)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-23 07:38:00.654: E/AndroidRuntime(880): ... 11 more
If you get a nullpointer on a certain line, something on that line is null. If it is the if(childBox.isChecked() line, probably childBox is null. Hard to say without the stack, but most probable cause is the line where you retrieve that checkbox.
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
Might be returning null, and this could be for several reasons. It could be your id is childBox instead of childBOX. Or that it is not in R.layout.list.
The best thing you can do is start debugging. What line is the error. Find the object that is null. Find out why it is null and if that is expected or not.
Instead of checking the childBox.isChecked() you can use the isChecked value. So your code would look like this:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
choosenGroup.setText("Shift Set");
}
}
Check your layout list.xml I think this layout may is missing android:id="#+id/childBOX" for Check Box or android:id="#+id/choosen" for TextView
Check the folowin sample
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/childBOX"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/choosen"/>

Categories