package com.example.tcpsocketclient;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
//import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Locale;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
private TextView textReceive = null;
private EditText textSend = null;
private EditText TextUserName=null;
private EditText TextPhone=null;
private Button btnConnect = null;
private Button btnSend = null;
private Button ClearBtn=null;
private static String ServerIP ="192.168.0.109";/* "58.30.16.206";*/
private static int ServerPort = 6002;
private Socket socket = null;
private String strMessage;
private boolean isConnect = false;
private OutputStream outStream;
private static Handler myHandler = null;
private ReceiveThread receiveThread = null;
private boolean isReceive = false;
private CheckThread checkThread=null;
private TextToSpeech mTts;
private int MY_DATA_CHECK_CODE=0;
public static final String DISPLAYTYPE = "UserName";
private String UserName="";
private String PhoneNumber="";
private boolean ExitApp=false;
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//程序运行到这里就报错,单步运行出现: Source not found
try
{
dbHelper = new DatabaseHelper(this);
GetUserInfo();
}
catch(Exception ex)
{
}
textReceive = (TextView)findViewById(R.id.textReceive);
textSend = (EditText)findViewById(R.id.textSend);
TextUserName =(EditText)findViewById(R.id.UserName);
TextPhone = (EditText)findViewById(R.id.phone);
btnConnect = (Button)findViewById(R.id.btnConnect);
btnSend = (Button)findViewById(R.id.btnSend);
ClearBtn= (Button)findViewById(R.id.ClearBtn);
//连接按钮的监听器
btnConnect.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (isConnect)
{
strMessage=TextUserName.getText().toString()+"|"+TextPhone.getText().toString();
new Thread(sendThread).start();
}
else
{
ReConnectServer();
}
}
});
//发送按钮的监听器
btnSend.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
strMessage = textSend.getText().toString();
new Thread(sendThread).start();
}
});
//清空按钮的监听器
ClearBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
textReceive.setText("");
}
});
myHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
textReceive.append((msg.obj).toString());
}
};
boolean networkState = NetworkDetector.detect(MainActivity.this);
if (!networkState)
{
new AlertDialog.Builder(this).setTitle("设置网络").setMessage("网络不可用,是否现在设置网络?").setPositiveButton("是", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
startActivityForResult(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS), 0);
}
}).setNegativeButton("否", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
}).show();
}
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
ReConnectServer();
checkThread = new CheckThread();
checkThread.start();
}
private TextToSpeech.OnInitListener mttsInitListener =new TextToSpeech.OnInitListener()
{
public void onInit(int version)
{
mTts.speak("你好!语音朗读可以使用了!", 0, null);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == MY_DATA_CHECK_CODE)
{
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
{
// success, create the TTS instance
//mTts = new TextToSpeech(this, this);
mTts = new TextToSpeech(this,mttsInitListener);
mTts.setLanguage(Locale.CHINESE);
if (mTts.isLanguageAvailable(Locale.CHINESE)!=TextToSpeech.LANG_AVAILABLE )
{
Toast toast =Toast.makeText(this, "你的手机系统没有安装中文语音包,不支持语音播报!", Toast.LENGTH_SHORT);
toast.show();
}
}
else
{
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
private void GetUserInfo()
{
try
{
Cursor cursor= dbHelper.GetUserInfo();
//startManagingCursor(cursor);
UserName=cursor.getString(0);
PhoneNumber=cursor.getString(1);
}
catch(Exception ex)
{
}
}
public void ReConnectServer()
{
if (isConnect)
{
isConnect=false;
if(receiveThread != null)
{
isReceive = false;
receiveThread.interrupt();
}
}
new Thread(connectThread).start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//连接到服务器的接口
Runnable connectThread = new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
try
{
//初始化Scoket,连接到服务器
socket = new Socket(ServerIP, ServerPort);
isConnect = true;
//启动接收线程
isReceive = true;
receiveThread = new ReceiveThread(socket);
receiveThread.start();
btnConnect.setText("连接");
System.out.println("----connected success----");
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("UnknownHostException-->" + e.toString());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("IOException" + e.toString());
}
}
};
//发送消息的接口
Runnable sendThread = new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
byte[] sendBuffer = null;
try
{
sendBuffer = strMessage.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
outStream = socket.getOutputStream();
}
catch (IOException e)
{
ReConnectServer();
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
outStream.write(sendBuffer);
}
catch (IOException e)
{
ReConnectServer();
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
//接收线程
private class ReceiveThread extends Thread
{
private InputStream inStream = null;
private byte[] buffer;
private String str = null;
ReceiveThread(Socket socket)
{
try
{
inStream = socket.getInputStream();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run()
{
while(isReceive)
{
Message msg = new Message();
buffer = new byte[1024];
try
{
inStream.read(buffer);
str = new String(buffer,"UTF-8").trim();
if (mTts.isLanguageAvailable(Locale.CHINESE)==TextToSpeech.LANG_AVAILABLE )
mTts.setLanguage(Locale.CHINESE);
mTts.speak(str, TextToSpeech.QUEUE_ADD, null);
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
msg = new Message();
msg.obj = str;
myHandler.sendMessage(msg);
}
}
}
//检查线程
private class CheckThread extends Thread
{
public void run()
{
boolean networkState = NetworkDetector.detect(MainActivity.this);
while(networkState)
{
if (ExitApp) break;
try
{
Thread.sleep(20000);
}
catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (socket!=null)
{
if (isReceive)
{
try
{
if (UserName!="")
strMessage = UserName+","+PhoneNumber;
else
strMessage = "未登陆";
new Thread(sendThread).start();
Thread.sleep(60000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
btnConnect.setText("登陆");
ReConnectServer();
}
}
else
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//写配制
public void savePreferences(int listID)
{
@SuppressWarnings("deprecation")
int mode = Activity.MODE_WORLD_WRITEABLE;
SharedPreferences gamePreferences = getSharedPreferences(DISPLAYTYPE, mode);
SharedPreferences.Editor editor = gamePreferences.edit();
editor.putInt("score", listID);
editor.commit();
}
//读配制
public int loadPreferences()
{
// Get the stored preferences
int listID;
@SuppressWarnings("deprecation")
int mode = Activity.MODE_WORLD_READABLE;
SharedPreferences gamePreferences = getSharedPreferences(DISPLAYTYPE, mode);
listID =gamePreferences.getInt("score", 0);
return listID;
}
@Override
protected void onDestroy()
{
ExitApp=true;
// TODO Auto-generated method stub
super.onDestroy();
if(receiveThread != null)
{
isReceive = false;
receiveThread.interrupt();
}
if (checkThread!=null)
{
checkThread.interrupt();
}
if (mTts!=null)
{
mTts.stop();
mTts.shutdown();
}
}
public static class NetworkDetector
{
public static boolean detect(Activity act)
{
ConnectivityManager manager = (ConnectivityManager) act.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager == null)
{
return false;
}
NetworkInfo networkinfo = manager.getActiveNetworkInfo();
if (networkinfo == null || !networkinfo.isAvailable())
{
return false;
}
return true;
}
}
}
错日志
03-04 05:35:46.655: E/AndroidRuntime(1210): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-04 05:35:46.655: E/AndroidRuntime(1210): at dalvik.system.NativeStart.main(Native Method)
03-04 05:35:46.655: E/AndroidRuntime(1210): Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class EditView
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-04 05:35:46.655: E/AndroidRuntime(1210): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.app.Activity.setContentView(Activity.java:1929)
03-04 05:35:46.655: E/AndroidRuntime(1210): at com.example.tcpsocketclient.MainActivity.onCreate(MainActivity.java:66)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.app.Activity.performCreate(Activity.java:5231)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-04 05:35:46.655: E/AndroidRuntime(1210): ... 11 more
03-04 05:35:46.655: E/AndroidRuntime(1210): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.EditView" on path: DexPathList[[zip file "/data/app/com.example.tcpsocketclient-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.tcpsocketclient-2, /system/lib]]
03-04 05:35:46.655: E/AndroidRuntime(1210): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-04 05:35:46.655: E/AndroidRuntime(1210): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-04 05:35:46.655: E/AndroidRuntime(1210): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.view.LayoutInflater.createView(LayoutInflater.java:559)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
03-04 05:35:46.655: E/AndroidRuntime(1210): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
03-04 05:35:46.655: E/AndroidRuntime(1210): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
03-04 05:35:46.655: E/AndroidRuntime(1210): ... 22 more
03-04 05:50:14.535: E/AndroidRuntime(1259): FATAL EXCEPTION: main
03-04 05:50:14.535: E/AndroidRuntime(1259): Process: com.example.tcpsocketclient, PID: 1259
03-04 05:50:14.535: E/AndroidRuntime(1259): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tcpsocketclient/com.example.tcpsocketclient.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class EditView
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.os.Looper.loop(Looper.java:136)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-04 05:50:14.535: E/AndroidRuntime(1259): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 05:50:14.535: E/AndroidRuntime(1259): at java.lang.reflect.Method.invoke(Method.java:515)
03-04 05:50:14.535: E/AndroidRuntime(1259): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-04 05:50:14.535: E/AndroidRuntime(1259): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-04 05:50:14.535: E/AndroidRuntime(1259): at dalvik.system.NativeStart.main(Native Method)
03-04 05:50:14.535: E/AndroidRuntime(1259): Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class EditView
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-04 05:50:14.535: E/AndroidRuntime(1259): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.app.Activity.setContentView(Activity.java:1929)
03-04 05:50:14.535: E/AndroidRuntime(1259): at com.example.tcpsocketclient.MainActivity.onCreate(MainActivity.java:66)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.app.Activity.performCreate(Activity.java:5231)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-04 05:50:14.535: E/AndroidRuntime(1259): ... 11 more
03-04 05:50:14.535: E/AndroidRuntime(1259): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.EditView" on path: DexPathList[[zip file "/data/app/com.example.tcpsocketclient-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.tcpsocketclient-2, /system/lib]]
03-04 05:50:14.535: E/AndroidRuntime(1259): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-04 05:50:14.535: E/AndroidRuntime(1259): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-04 05:50:14.535: E/AndroidRuntime(1259): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.view.LayoutInflater.createView(LayoutInflater.java:559)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
03-04 05:50:14.535: E/AndroidRuntime(1259): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
03-04 05:50:14.535: E/AndroidRuntime(1259): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
03-04 05:50:14.535: E/AndroidRuntime(1259): ... 22 more
楼主贴这个代码不如把你的错误日志贴出来显然是布局xml中出错了请问是xml布局那个地方错了,我后来发现使用线性布局就报错
<spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14px;line-height:22px;background-color:#FFFFFF;">Causedby:android.view.InflateException:BinaryXMLfileline#19:ErrorinflatingclassEditView
<spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14px;line-height:22px;background-color:#FFFFFF;">
然后将你上面的xml文件用osc的布局文件按格式发出来,不然看啥啊
xml啊布局文件错误R.layout.activity_main 你导包了吗? 我怎么没有看到代码里面的包引用?<spanstyle="font-family:Verdana,sans-serif,宋体;font-size:14px;line-height:22px;background-color:#FFFFFF;">EditView是何物,没用过。
我只认识EditText,孤陋了啊。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。