我的应用程序在Android 2.3.4(模拟器/手机)上运行完美。但我尝试在Android 4.0.3(仿真器/手机)上安装它,当我运行它时无法正常工作。它向我显示了这条消息
"Unfortunately AppName has stopped"
当我看到错误时,应用程序正在执行这些类:
public class EmpleadosAct extends Activity {
private ArrayList<String> datos;
private ListView list;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActualizarVehiculos();
}
public void ActualizarVehiculos() {
try {
NotesCenter messageCenter = new NotesCenterImpl();
List<Vehiculo> vehiculo = messageCenter.getVehiculo();
for (Vehiculo v : vehiculo) {
AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(
this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj
.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(AndroidOpenDbHelper.matricula, v.matricula);
long affectedColumnId = sqliteDatabase.insert("Vehiculos",
null, contentValues);
sqliteDatabase.close();
}
} catch (Exception ex) {
Log.v("blah", ex.getMessage());
}
}
}
第二节课:
public class SeleccionarEmpleado extends Activity implements OnClickListener,
OnItemClickListener {
private ListView listaEmpleados;
private EditText ECont;
String text;
Bundle dataBundle;
public EmpleadosAct actemple;
protected AppApplication app;
private ListAdapter uGraduateListAdapter;
private ArrayList<UndergraduateDetailsPojo> pojoArrayList;
private String contraseña;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seleccionarempleado);
***Intent actServicios = new Intent(this, EmpleadosAct.class);
startActivity(actServicios);***
listaEmpleados = (ListView) findViewById(R.id.ListaEmpleados);
listaEmpleados.setOnItemClickListener(this);
pojoArrayList = new ArrayList<UndergraduateDetailsPojo>();
uGraduateListAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, populateList());
listaEmpleados.setAdapter(uGraduateListAdapter);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actualizar:
//Intent actualizarEmpleados = new Intent(this,EmpleadosAct.class);
//startActivity(actualizarEmpleados);
//return true;
case R.id.opciones:
Toast.makeText(SeleccionarEmpleado.this, "Save is Selected",
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onClick(View v) {
Intent addNewUndergraduateIntent = new Intent(this,
InsertarPedido.class);
startActivity(addNewUndergraduateIntent);
}
public List<String> populateList() {
List<String> uGraduateNamesList = new ArrayList<String>();
AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
Cursor cursor = sqliteDatabase.query("Empleados", null, null, null,
null, null, null);
startManagingCursor(cursor);
while (cursor.moveToNext()) {
String idempleado = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.idempleado));
String nombre = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.nombre));
String primerapellido = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.primerapellido));
String segundoapellido = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.segundoapellido));
String contra = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.contra));
UndergraduateDetailsPojo ugPojoClass = new UndergraduateDetailsPojo();
ugPojoClass.setIdEmpleado(idempleado);
ugPojoClass.setNombreEmpleado(nombre);
ugPojoClass.setPrimerApellido(primerapellido);
ugPojoClass.setSegundoApellido(segundoapellido);
ugPojoClass.setContra(contra);
pojoArrayList.add(ugPojoClass);
uGraduateNamesList.add(nombre + " " + primerapellido + " "
+ segundoapellido);
}
sqliteDatabase.close();
return uGraduateNamesList;
}
@Override
protected void onResume() {
super.onResume();
uGraduateListAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, populateList());
listaEmpleados.setAdapter(uGraduateListAdapter);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
UndergraduateDetailsPojo clickedObject = pojoArrayList.get(arg2);
String Empleado = clickedObject.getNombreEmpleado() + " "
+ clickedObject.getPrimerApellido() + " "
+ clickedObject.getSegundoApellido();
Empleado empleadoseleccionado = new Empleado();
empleadoseleccionado.id = clickedObject.getIdEmpleado();
empleadoseleccionado.nombre = Empleado;
app = (AppApplication)getApplicationContext();
app.setempleadoActual(empleadoseleccionado);
app.setempleadoId(empleadoseleccionado);
}
}
首先它执行第二个类,在第二个类中有一个打开第一个类的Intent。第一类从一些远程服务器向android SQLite数据库添加数据。然后第二个类从android SQLite数据库显示这个结果。
这是日志:
06-04 21:08:13.034: W/dalvikvm(517): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-04 21:08:13.054: E/AndroidRuntime(517): FATAL EXCEPTION: main
06-04 21:08:13.054: E/AndroidRuntime(517): java.lang.RuntimeException: Unable to start activity ComponentInfo{dokesim.net/myapp.net.EmpleadosAct}: java.lang.NullPointerException: println needs a message
06-04 21:08:13.054: E/AndroidRuntime(517): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-04 21:08:13.054: E/AndroidRuntime(517): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-04 21:08:13.054: E/AndroidRuntime(517): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-04 21:08:13.054: E/AndroidRuntime(517): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-04 21:08:13.054: E/AndroidRuntime(517): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 21:08:13.054: E/AndroidRuntime(517): at android.os.Looper.loop(Looper.java:137)
06-04 21:08:13.054: E/AndroidRuntime(517): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-04 21:08:13.054: E/AndroidRuntime(517): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 21:08:13.054: E/AndroidRuntime(517): at java.lang.reflect.Method.invoke(Method.java:511)
06-04 21:08:13.054: E/AndroidRuntime(517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-04 21:08:13.054: E/AndroidRuntime(517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-04 21:08:13.054: E/AndroidRuntime(517): at dalvik.system.NativeStart.main(Native Method)
06-04 21:08:13.054: E/AndroidRuntime(517): Caused by: java.lang.NullPointerException: println needs a message
06-04 21:08:13.054: E/AndroidRuntime(517): at android.util.Log.println_native(Native Method)
06-04 21:08:13.054: E/AndroidRuntime(517): at android.util.Log.v(Log.java:117)
06-04 21:08:13.054: E/AndroidRuntime(517): at myapp.net.EmpleadosAct.ActualizarVehiculos(EmpleadosAct.java:133)
06-04 21:08:13.054: E/AndroidRuntime(517): at myapp.net.EmpleadosAct.onCreate(EmpleadosAct.java:24)
06-04 21:08:13.054: E/AndroidRuntime(517): at android.app.Activity.performCreate(Activity.java:4465)
06-04 21:08:13.054: E/AndroidRuntime(517): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-04 21:08:13.054: E/AndroidRuntime(517): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-04 21:08:13.054: E/AndroidRuntime(517): ... 11 more
第二个日志:
06-04 21:40:03.225: V/blah(720): Error happened:
06-04 21:40:03.225: V/blah(720): android.os.NetworkOnMainThreadException
06-04 21:40:03.225: V/blah(720): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-04 21:40:03.225: V/blah(720): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-04 21:40:03.225: V/blah(720): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-04 21:40:03.225: V/blah(720): at libcore.io.IoBridge.connect(IoBridge.java:112)
06-04 21:40:03.225: V/blah(720): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-04 21:40:03.225: V/blah(720): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-04 21:40:03.225: V/blah(720): at java.net.Socket.connect(Socket.java:842)
06-04 21:40:03.225: V/blah(720): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-04 21:40:03.225: V/blah(720): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-04 21:40:03.225: V/blah(720): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-04 21:40:03.225: V/blah(720): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119 )
06-04 21:40:03.225: V/blah(720): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-04 21:40:03.225: V/blah(720): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-04 21:40:03.225: V/blah(720): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-04 21:40:03.225: V/blah(720): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-04 21:40:03.225: V/blah(720): at myapp.net.HttpNote.doGet(HttpNote.java:32)
06-04 21:40:03.225: V/blah(720): at myapp.net.NotesCenterImpl.getVehiculo(NotesCenterImpl.java:23)
06-04 21:40:03.225: V/blah(720): at myapp.net.EmpleadosAct.ActualizarVehiculo(EmpleadosAct.java:91)
06-04 21:40:03.225: V/blah(720): at myapp.net.EmpleadosAct.onCreate(EmpleadosAct.java:24)
06-04 21:40:03.225: V/blah(720): at android.app.Activity.performCreate(Activity.java:4465)
06-04 21:40:03.225: V/blah(720): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-04 21:40:03.225: V/blah(720): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-04 21:40:03.225: V/blah(720): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-04 21:40:03.225: V/blah(720): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-04 21:40:03.225: V/blah(720): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-04 21:40:03.225: V/blah(720): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 21:40:03.225: V/blah(720): at android.os.Looper.loop(Looper.java:137)
06-04 21:40:03.225: V/blah(720): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-04 21:40:03.225: V/blah(720): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 21:40:03.225: V/blah(720): at java.lang.reflect.Method.invoke(Method.java:511)
06-04 21:40:03.225: V/blah(720): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-04 21:40:03.225: V/blah(720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-04 21:40:03.225: V/blah(720): at dalvik.system.NativeStart.main(Native Method)
那么究竟是什么导致2.3.4 android工作正常,而4.0.3则不然?我的代码有些问题不支持4.0.3版本?
感谢您的任何回答或建议。
答案 0 :(得分:6)
Log.v("blah", ex.getMessage());
问题是ex.getMessage()
正在返回null
。您可以检查null
,但更好的方法是将异常作为第三个参数显式传递给log方法:
Log.v("blah", "Error happened: ", ex);