|
楼主 |
发表于 2012-8-16 19:04:35
|
显示全部楼层
Android编程遇到问题,请教各位大侠,问题出在哪里?
请教各位大侠,我是一个学Android的新手,而我们基地又没有精通Android的学长,写程序的过程中就遇到很多问题。最近在做学习Android视频传输。写了一个很简单的程序,视频传输外加有一个按钮,按下后发送字符给wifi芯片。但是程序不能运行,在模拟器上运行时总说意外终止。程序如下:
Main.java部分程序:
public class Main extends Activity
{
//private static final String TAG1 = "Main";
URL videoUrl;
Bitmap bmp;
static PrintWriter mPrintWriterClient = null;
static BufferedReader mBufferedReaderClient = null;
private boolean isConnecting = false;
private Thread mThreadClient = null;
private Socket mSocketClient = null;
public static String CameraIp;
MySurfaceView r;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//隐去标题(应用的名字必须要写在setContentView之前,否则会有异常)
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
Button button2 = (Button) findViewById(R.id.button2);
r=(MySurfaceView)findViewById(R.id.mySurfaceView1);
button2.setTextSize(20);
CameraIp ="http://192.168.1.1:8080/?action=snapshot";
r.GetCameraIP(CameraIp);
mThreadClient = new Thread(mRunnable);
mThreadClient.start();
button2.setOnTouchListener(new Button.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
mPrintWriterClient.print("f");//前进
mPrintWriterClient.flush();
break;
case MotionEvent.ACTION_UP:
mPrintWriterClient.print("0");
mPrintWriterClient.flush();
}
return false;
}
});
}
private Runnable mRunnable = new Runnable()
{
public void run()
{
String msgText ="192.168.1.1:2001";
int start = msgText.indexOf(":");
String sIP = msgText.substring(0, start);
String sPort = msgText.substring(start+1);
int port = Integer.parseInt(sPort);
Log.d("gjz", "IP:"+ sIP + ":" + port);
try
{
//连接服务器
mSocketClient = new Socket(sIP, port);
//取得输入、输出流
mBufferedReaderClient = new BufferedReader(new InputStreamReader(mSocketClient.getInputStream()));
mPrintWriterClient = new PrintWriter(mSocketClient.getOutputStream(), true);
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
//break;
}
catch (Exception e)
{
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
return;
}
char[] buffer = new char[256];
while (isConnecting)
{
try
{
//if ( (recvMessageClient = mBufferedReaderClient.readLine()) != null )
if((mBufferedReaderClient.read(buffer))>0)
{
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
catch (Exception e)
{
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
}
};
Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
}
};
public void onDestroy() {
super.onDestroy();
if (isConnecting)
{
isConnecting = false;
try {
if(mSocketClient!=null)
{
mSocketClient.close();
mSocketClient = null;
mPrintWriterClient.close();
mPrintWriterClient = null;
}
}
catch (IOException e)
{
e.printStackTrace();
}
mThreadClient.interrupt();
}
}
}
MySurfaceView.java代码应该没有什么问题,因为之前写的一个视频传输程序运行成功了,代码没有变。
main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<here.java.MySurfaceView
android:id="@+id/mySurfaceView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="前进"
/>
</LinearLayout >
AndroidManifest代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.java"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:screenOrientation="landscape"
android:name=".Main" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE">
</uses-permission>
</manifest>
请教各位Android高手,问题出在哪?
|
|