我在远程服务中修改textview时失败(fail when i modify a textview in remote service)

我正在做一个使用远程服务并且工作正常的Android应用程序,但是当我想在textview中显示此服务的结果崩溃而我不知道为什么。 这是我的代码:

package com.example.grafica; import com.example.pruebacelia.IRemoteService; import android.annotation.TargetApi; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.content.res.Configuration; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Point; import android.graphics.RectF; import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.view.Display; import android.view.KeyEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.TextureView; import android.widget.FrameLayout; import android.widget.TableLayout; import android.widget.TextView; @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) public class MainActivity extends Activity { float ancho = (float) 5.20; float largo = (float) 3.50; private SurfaceView surface; private TableLayout table, table2; private FrameLayout.LayoutParams params, params2; IRemoteService mRemoteService; private int i = 0; private Thread serviceThread = null; private Paint pincel = new Paint(); private Canvas canvas; private TextView tx1, tx2, tx3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tx1 = (TextView) findViewById(R.id.textView7); tx2 = (TextView) findViewById(R.id.textView8); tx3 = (TextView) findViewById(R.id.textView9); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { setContentView(R.layout.activity_main); } if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { setContentView(R.layout.viewland); } surface = (SurfaceView) findViewById(R.id.surface); surface.getHolder().addCallback(new Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { float xini = 0; float yini = 0; float xend = 0; float yend = 0; canvas = holder.lockCanvas(); canvas.drawColor(Color.WHITE); pincel.setColor(Color.BLACK); pincel.setStrokeWidth(8); pincel.setStyle(Style.STROKE); RectF rect = new RectF(); Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { table = (TableLayout) findViewById(R.id.table); params = (FrameLayout.LayoutParams) table.getLayoutParams(); float ratio = ancho / largo; float aux = width / ratio; params.topMargin = (int) aux + 20; xini = 20; yini = 20; xend = width - 20; yend = aux; } if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { table2 = (TableLayout) findViewById(R.id.table2); params2 = (FrameLayout.LayoutParams) table2 .getLayoutParams(); float ratio = largo / ancho; float aux = width / ratio; params2.leftMargin = (int) (width * ratio) + 20; xini = 20; yini = 20; xend = width * ratio; yend = height - 220; } rect.set(xini, yini, xend, yend); canvas.drawRect(rect, pincel); pincel.setColor(Color.RED); pincel.setStrokeWidth(25); canvas.drawPoint(xini, yend, pincel); pincel.setColor(Color.BLUE); pincel.setStrokeWidth(25); canvas.drawPoint(xend, yend, pincel); pincel.setColor(Color.GREEN); pincel.setStrokeWidth(25); canvas.drawPoint(xini, yini, pincel); pincel.setColor(Color.YELLOW); pincel.setStrokeWidth(25); canvas.drawPoint(xend, yini, pincel); holder.unlockCanvasAndPost(canvas); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } }); Intent serviceIntent = new Intent(); serviceIntent.setClassName("com.example.pruebacelia", "com.example.pruebacelia.DemoService"); boolean ok = bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE); Log.v("ok", String.valueOf(ok)); } private ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // get instance of the aidl binder mRemoteService = IRemoteService.Stub.asInterface(service); i = 0; serviceThread = new Thread(new Runnable() { @Override public void run() { while (i == 0) { try { float message = mRemoteService.getx(); float message2 = mRemoteService.gety(); float message3 = mRemoteService.getz(); } catch (RemoteException e) { Log.e("RemoteException", e.toString()); } runOnUiThread(new Runnable() { public void run() { tx1.setText(String.valueOf("ei")); } }); } } }, "serviceThread"); serviceThread.start(); } }; @Override public boolean onKeyDown(int keyCode, KeyEvent evento) { if (keyCode == KeyEvent.KEYCODE_BACK) { i = 1; Log.i("telo", "pulsado"); unbindService(mServiceConnection); finish(); } return true; } }

在concret中我做.settext();

这是logcat:

02-11 09:55:41.579: I/Adreno-EGL(9499): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13 02-11 09:55:41.599: D/OpenGLRenderer(9499): Enabling debug mode 0 02-11 09:55:41.739: D/AndroidRuntime(9499): Shutting down VM 02-11 09:55:41.739: W/dalvikvm(9499): threadid=1: thread exiting with uncaught exception (group=0x41548ba8) 02-11 09:55:41.739: E/AndroidRuntime(9499): FATAL EXCEPTION: main 02-11 09:55:41.739: E/AndroidRuntime(9499): Process: com.example.grafica, PID: 9499 02-11 09:55:41.739: E/AndroidRuntime(9499): java.lang.NullPointerException 02-11 09:55:41.739: E/AndroidRuntime(9499): at com.example.grafica.MainActivity$1$1$1.run(MainActivity.java:217) 02-11 09:55:41.739: E/AndroidRuntime(9499): at android.os.Handler.handleCallback(Handler.java:733) 02-11 09:55:41.739: E/AndroidRuntime(9499): at android.os.Handler.dispatchMessage(Handler.java:95) 02-11 09:55:41.739: E/AndroidRuntime(9499): at android.os.Looper.loop(Looper.java:136) 02-11 09:55:41.739: E/AndroidRuntime(9499): at android.app.ActivityThread.main(ActivityThread.java:5017) 02-11 09:55:41.739: E/AndroidRuntime(9499): at java.lang.reflect.Method.invokeNative(Native Method) 02-11 09:55:41.739: E/AndroidRuntime(9499): at java.lang.reflect.Method.invoke(Method.java:515) 02-11 09:55:41.739: E/AndroidRuntime(9499): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 02-11 09:55:41.739: E/AndroidRuntime(9499): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 02-11 09:55:41.739: E/AndroidRuntime(9499): at dalvik.system.NativeStart.main(Native Method) 02-11 09:55:42.309: D/dalvikvm(9499): GC_FOR_ALLOC freed 191K, 2% free 17078K/17300K, paused 11ms, total 11ms 02-11 09:55:43.909: I/Process(9499): Sending signal. PID: 9499 SIG: 9

I am doing an Android app wich uses a remote service and works fine, but when i want show in a textview results of this service crashes and i don´t know why. This is my code:

package com.example.grafica; import com.example.pruebacelia.IRemoteService; import android.annotation.TargetApi; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.content.res.Configuration; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Point; import android.graphics.RectF; import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.view.Display; import android.view.KeyEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.TextureView; import android.widget.FrameLayout; import android.widget.TableLayout; import android.widget.TextView; @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) public class MainActivity extends Activity { float ancho = (float) 5.20; float largo = (float) 3.50; private SurfaceView surface; private TableLayout table, table2; private FrameLayout.LayoutParams params, params2; IRemoteService mRemoteService; private int i = 0; private Thread serviceThread = null; private Paint pincel = new Paint(); private Canvas canvas; private TextView tx1, tx2, tx3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tx1 = (TextView) findViewById(R.id.textView7); tx2 = (TextView) findViewById(R.id.textView8); tx3 = (TextView) findViewById(R.id.textView9); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { setContentView(R.layout.activity_main); } if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { setContentView(R.layout.viewland); } surface = (SurfaceView) findViewById(R.id.surface); surface.getHolder().addCallback(new Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { float xini = 0; float yini = 0; float xend = 0; float yend = 0; canvas = holder.lockCanvas(); canvas.drawColor(Color.WHITE); pincel.setColor(Color.BLACK); pincel.setStrokeWidth(8); pincel.setStyle(Style.STROKE); RectF rect = new RectF(); Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { table = (TableLayout) findViewById(R.id.table); params = (FrameLayout.LayoutParams) table.getLayoutParams(); float ratio = ancho / largo; float aux = width / ratio; params.topMargin = (int) aux + 20; xini = 20; yini = 20; xend = width - 20; yend = aux; } if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { table2 = (TableLayout) findViewById(R.id.table2); params2 = (FrameLayout.LayoutParams) table2 .getLayoutParams(); float ratio = largo / ancho; float aux = width / ratio; params2.leftMargin = (int) (width * ratio) + 20; xini = 20; yini = 20; xend = width * ratio; yend = height - 220; } rect.set(xini, yini, xend, yend); canvas.drawRect(rect, pincel); pincel.setColor(Color.RED); pincel.setStrokeWidth(25); canvas.drawPoint(xini, yend, pincel); pincel.setColor(Color.BLUE); pincel.setStrokeWidth(25); canvas.drawPoint(xend, yend, pincel); pincel.setColor(Color.GREEN); pincel.setStrokeWidth(25); canvas.drawPoint(xini, yini, pincel); pincel.setColor(Color.YELLOW); pincel.setStrokeWidth(25); canvas.drawPoint(xend, yini, pincel); holder.unlockCanvasAndPost(canvas); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } }); Intent serviceIntent = new Intent(); serviceIntent.setClassName("com.example.pruebacelia", "com.example.pruebacelia.DemoService"); boolean ok = bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE); Log.v("ok", String.valueOf(ok)); } private ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // get instance of the aidl binder mRemoteService = IRemoteService.Stub.asInterface(service); i = 0; serviceThread = new Thread(new Runnable() { @Override public void run() { while (i == 0) { try { float message = mRemoteService.getx(); float message2 = mRemoteService.gety(); float message3 = mRemoteService.getz(); } catch (RemoteException e) { Log.e("RemoteException", e.toString()); } runOnUiThread(new Runnable() { public void run() { tx1.setText(String.valueOf("ei")); } }); } } }, "serviceThread"); serviceThread.start(); } }; @Override public boolean onKeyDown(int keyCode, KeyEvent evento) { if (keyCode == KeyEvent.KEYCODE_BACK) { i = 1; Log.i("telo", "pulsado"); unbindService(mServiceConnection); finish(); } return true; } }

In concret when i do the .settext();

and this is logcat:

02-11 09:55:41.579: I/Adreno-EGL(9499): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13 02-11 09:55:41.599: D/OpenGLRenderer(9499): Enabling debug mode 0 02-11 09:55:41.739: D/AndroidRuntime(9499): Shutting down VM 02-11 09:55:41.739: W/dalvikvm(9499): threadid=1: thread exiting with uncaught exception (group=0x41548ba8) 02-11 09:55:41.739: E/AndroidRuntime(9499): FATAL EXCEPTION: main 02-11 09:55:41.739: E/AndroidRuntime(9499): Process: com.example.grafica, PID: 9499 02-11 09:55:41.739: E/AndroidRuntime(9499): java.lang.NullPointerException 02-11 09:55:41.739: E/AndroidRuntime(9499): at com.example.grafica.MainActivity$1$1$1.run(MainActivity.java:217) 02-11 09:55:41.739: E/AndroidRuntime(9499): at android.os.Handler.handleCallback(Handler.java:733) 02-11 09:55:41.739: E/AndroidRuntime(9499): at android.os.Handler.dispatchMessage(Handler.java:95) 02-11 09:55:41.739: E/AndroidRuntime(9499): at android.os.Looper.loop(Looper.java:136) 02-11 09:55:41.739: E/AndroidRuntime(9499): at android.app.ActivityThread.main(ActivityThread.java:5017) 02-11 09:55:41.739: E/AndroidRuntime(9499): at java.lang.reflect.Method.invokeNative(Native Method) 02-11 09:55:41.739: E/AndroidRuntime(9499): at java.lang.reflect.Method.invoke(Method.java:515) 02-11 09:55:41.739: E/AndroidRuntime(9499): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 02-11 09:55:41.739: E/AndroidRuntime(9499): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 02-11 09:55:41.739: E/AndroidRuntime(9499): at dalvik.system.NativeStart.main(Native Method) 02-11 09:55:42.309: D/dalvikvm(9499): GC_FOR_ALLOC freed 191K, 2% free 17078K/17300K, paused 11ms, total 11ms 02-11 09:55:43.909: I/Process(9499): Sending signal. PID: 9499 SIG: 9

最满意答案

您需要先将布局的内容设置为活动,然后初始化视图

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // this should be first tx1 = (TextView) findViewById(R.id.textView7); // then initialize views

findViewById查找具有当前膨胀布局中提到的id的视图。 如果不是你最终我NullPointerException 。

You need to set the content of the layout to the activity first then initialize views

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // this should be first tx1 = (TextView) findViewById(R.id.textView7); // then initialize views

findViewById looks for a view with the id mentioned in the current inflated layout. If not you end up i NullPointerException.

更多推荐