我正在做一个使用远程服务并且工作正常的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;
}
}
在我做.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
答案 0 :(得分:1)
您需要先将布局内容设置为活动,然后初始化视图
@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
。
答案 1 :(得分:0)
试试这个..
在TextView
setContentView(R.layout.activity_main);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_main);
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.viewland);
}
tx1 = (TextView) findViewById(R.id.textView7);
tx2 = (TextView) findViewById(R.id.textView8);
tx3 = (TextView) findViewById(R.id.textView9);
}
答案 2 :(得分:0)
将它写在super.onCreate(savedInstanceState)下面的onCreate方法()中;
setContentView(R.layout.activity_main);
然后初始化你的文字
像这样: -
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_main);
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.viewland);
}
tx1 = (TextView) findViewById(R.id.textView7);
tx2 = (TextView) findViewById(R.id.textView8);
tx3 = (TextView) findViewById(R.id.textView9);