In my app the first screen is a list and when you click once it goes ok but if,my mistake or something, you click two or more times before it loads the application crashes. I've been doing a little research but I didn't found anything. I post my code:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.firstscreen);
// Set title
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.mainheader);
// Get Arguments
Bundle extras = getIntent().getExtras();
if (extras != null) {
app = extras.getString("app");
}
//Set simulation date
TextView date = (TextView) findViewById(R.id.date);
date.setText(convertDateToString());
// set the list adapter: ListView myList =
ListView lv = (ListView) findViewById(R.id.list);
DataBaseAccess ddbb = new DataBaseAccess();
//Download the data from a database
lv.setAdapter(new MyAdapter(this, R.layout.listitem, myLines,
flowAlarm, precAlarm));
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(Main.this, ShowGraph.class);
String[] obj = list.split(";");
i.putExtra("object name", obj[position]);
i.putExtra("app name", app);
i.putExtra("app list", list);
startActivity(i);
}
});
// Set buttons clickable
Button exit = (Button) findViewById(R.id.exit);
exit.setOnClickListener(this);
Button calendar = (Button) findViewById(R.id.calendar);
calendar.setOnClickListener(this);
}
Does anyone knows why?
My LogCat:
02-29 13:51:27.975: E/AndroidRuntime(9082): FATAL EXCEPTION: main
02-29 13:51:27.975: E/AndroidRuntime(9082): java.lang.NullPointerException
02-29 13:51:27.975: E/AndroidRuntime(9082):at org.achartengine.chart.XYChart.toRealPoint(XYChart.java:699)
02-29 13:51:27.975: E/AndroidRuntime(9082):at org.achartengine.tools.Pan.apply(Pan.java:64)
02-29 13:51:27.975: E/AndroidRuntime(9082):at org.achartengine.TouchHandler.handleTouch(TouchHandler.java:106)
02-29 13:51:27.975: E/AndroidRuntime(9082):at org.achartengine.GraphicalView.onTouchEvent(GraphicalView.java:284)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.View.dispatchTouchEvent(View.java:3952)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1034)
02-29 13:51:27.975: E/AndroidRuntime(9082): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1711)
02-29 13:51:27.975: E/AndroidRuntime(9082): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1145)
02-29 13:51:27.975: E/AndroidRuntime(9082): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
02-29 13:51:27.975: E/AndroidRuntime(9082):at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1695)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2217)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.view.ViewRoot.handleMessage(ViewRoot.java:1901)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.os.Looper.loop(Looper.java:130)
02-29 13:51:27.975: E/AndroidRuntime(9082):at android.app.ActivityThread.main(ActivityThread.java:3701)
02-29 13:51:27.975: E/AndroidRuntime(9082):at java.lang.reflect.Method.invokeNative(Native Method)
02-29 13:51:27.975: E/AndroidRuntime(9082):at java.lang.reflect.Method.invoke(Method.java:507)
02-29 13:51:27.975: E/AndroidRuntime(9082):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
02-29 13:51:27.975: E/AndroidRuntime(9082):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
02-29 13:51:27.975: E/AndroidRuntime(9082):at dalvik.system.NativeStart.main(Native Method)
The ShowGraph activity oncreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
Bundle extras = getIntent().getExtras();
if (extras != null) {
object = extras.getString("object name");
app = extras.getString("app name");
list = extras.getString("app list");
}
// Set title
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.headermenu);
// Download the info from the database
downloadInfo(app, object);
// set the button listeners
listeners();
//Set checkbox listeners
setCheckListeners();
// Drag Meteo
vg = (RelativeLayout) findViewById(R.id.graph);
vg.setOnTouchListener(new View.OnTouchListener() {
RelativeLayout vg1 = (RelativeLayout) findViewById(R.id.graph1);
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
// int x = (int) event.getX() - offset_x;
int y = (int) event.getY() - offset_y;
int w = getWindowManager().getDefaultDisplay().getWidth();
int h = vg.getHeight() - 40;
if (y < 0)
y = 0;
if (y > h)
y = h;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
lp.setMargins(0, y, 0, 0);
selected_item.setLayoutParams(lp);
int height = vg1.getHeight();
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
lp2.setMargins(0, 0, 0, Math.abs(vg.getHeight() - y));
// lp2.height = y-height;
vg1.setLayoutParams(lp2);
break;
default:
break;
}
return true;
}
});
RelativeLayout img = (RelativeLayout) findViewById(R.id.vg);
img.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
offset_x = (int) event.getX();
offset_y = (int) event.getY();
selected_item = v;
break;
default:
break;
}
return false;
}
});
}
I tried with the AsyncTask as I read here but is not working:
Android: Disable ListView once clicked?
In ShowGraph:
ReplyDeleteif (extras != null) {
object = extras.getString("object name");
app = extras.getString("app name");
list = extras.getString("app list");
}
If your extras is null, then your app is null too. So problem can be there
downloadInfo(app, object); //NullPointerException
ALSO
1) Note that onCreate() called once (only for first click). It's better to move your "get Intent" part to onStart() method.
2) Use debbuging tools or try to hide your code with comments block by block, to find where is NullPointerException exactly.
/*Like this
downloadInfo(app, object);
*/