在我的应用程序中,第一个屏幕是一个列表,当您单击一次它就可以了但如果,我的错误或其他什么,您在加载应用程序崩溃之前单击两次或更多次。我一直在做一些研究,但我没有找到任何东西。我发布了我的代码:
@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);
}
有谁知道为什么?
我的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)
ShowGraph活动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;
}
});
}
我尝试使用AsyncTask,因为我在这里阅读但是没有工作:
答案 0 :(得分:1)
在ShowGraph中:
if (extras != null) {
object = extras.getString("object name");
app = extras.getString("app name");
list = extras.getString("app list");
}
如果您的extras
为空,那么您的app
也为空。那么问题就在那里
downloadInfo(app, object); //NullPointerException
同时强>
1)请注意onCreate()
调用一次(仅限第一次点击)。最好将“获取意图”部分移动到onStart()
方法。
2)使用debbuging工具或尝试逐块隐藏注释代码,以找到NullPointerException的确切位置。
/*Like this
downloadInfo(app, object);
*/