- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试遵循 Android 聊天应用程序的教程。
我已经完成了使用 AppCompatActivity
和 ActionBarActivity
所需的所有配置,但是当我尝试运行我的应用程序时,它将停止工作。我一直在寻找不同的解决方法并尝试了各种解决方案,但似乎仍然无法完成,请帮助我
java文件:
public class MainActivity extends AppCompatActivity {
static final int SocketServerPORT = 8080;
TextView infoIp, infoPort, chatMsg;
String msgLog = "";
List<ChatClient> userList;
ServerSocket serverSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infoIp = (TextView) findViewById(R.id.infoip);
infoPort = (TextView) findViewById(R.id.infoport);
chatMsg = (TextView) findViewById(R.id.chatmsg);
infoIp.setText(getIpAddress());
userList = new ArrayList<ChatClient>();
ChatServerThread chatServerThread = new ChatServerThread();
chatServerThread.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class ChatServerThread extends Thread {
@Override
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SocketServerPORT);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
infoPort.setText("I'm waiting here: "
+ serverSocket.getLocalPort());
}
});
while (true) {
socket = serverSocket.accept();
ChatClient client = new ChatClient();
userList.add(client);
ConnectThread connectThread = new ConnectThread(client, socket);
connectThread.start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
private class ConnectThread extends Thread {
Socket socket;
ChatClient connectClient;
String msgToSend = "";
ConnectThread(ChatClient client, Socket socket){
connectClient = client;
this.socket= socket;
client.socket = socket;
client.chatThread = this;
}
@Override
public void run() {
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
String n = dataInputStream.readUTF();
connectClient.name = n;
msgLog += connectClient.name + " connected@" +
connectClient.socket.getInetAddress() +
":" + connectClient.socket.getPort() + "\n";
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
chatMsg.setText(msgLog);
}
});
dataOutputStream.writeUTF("Welcome " + n + "\n");
dataOutputStream.flush();
broadcastMsg(n + " join our chat.\n");
while (true) {
if (dataInputStream.available() > 0) {
String newMsg = dataInputStream.readUTF();
msgLog += n + ": " + newMsg;
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
chatMsg.setText(msgLog);
}
});
broadcastMsg(n + ": " + newMsg);
}
if(!msgToSend.equals("")){
dataOutputStream.writeUTF(msgToSend);
dataOutputStream.flush();
msgToSend = "";
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
userList.remove(connectClient);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,
connectClient.name + " removed.", Toast.LENGTH_LONG).show();
msgLog += "-- " + connectClient.name + " leaved\n";
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
chatMsg.setText(msgLog);
}
});
broadcastMsg("-- " + connectClient.name + " leaved\n");
}
});
}
}
private void sendMsg(String msg){
msgToSend = msg;
}
}
private void broadcastMsg(String msg){
for(int i=0; i<userList.size(); i++){
userList.get(i).chatThread.sendMsg(msg);
msgLog += "- send to " + userList.get(i).name + "\n";
}
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
chatMsg.setText(msgLog);
}
});
}
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "
+ inetAddress.getHostAddress() + "\n";
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
class ChatClient {
String name;
Socket socket;
ConnectThread chatThread;
}
XML 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Char Server"
android:textStyle="bold" />
<TextView
android:id="@+id/infoport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic" />
<TextView
android:id="@+id/infoip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/chatmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
我已完成访问权限 list
<uses-permission android:name="android.permission.INTERNET" />
我也包含在我的gradle中
compile 'com.android.support:appcompat-v7:23.0.1'
我的logcat
E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.l335a04.walao, PID: 11814 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.l335a04.walao/com.example.l335a04.walao.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:309) at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:278) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:252) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) at com.example.l335a04.walao.MainActivity.onCreate(MainActivity.java:35) at android.app.Activity.performCreate(Activity.java:6221) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.ap p.ActivityThread.performLaunchActivity(ActivityThread.java:2611) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(Zygoenter code hereteInit.java:1194)
最佳答案
来自 LogCat:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
我们可以知道您的 AppCompatActivity
需要 list 中的 Theme.AppCompat
。打开 styles.xml
并将主题更改为:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" />
在AndroidManifest.xml
中:
<activity android:name="MainActivity"
android:theme="@style/AppTheme"
... />
关于java - AppCompatActivity 和 ActionBarActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32905190/
我正在升级我之前制作的一款游戏的用户界面,并且正朝着 Google 的 Material Design 方向发展。作为代码升级的一部分,我将向后兼容性支持库 android-support-v7-ap
更改applicationId后,Android Studio无法解析AppCompatActivity,DialogFragment,支持V4 Fragment和支持库中的类似项。 我努力了: 清洁
我尝试使用下面的代码在 android studio 中导入 AppCompatActivity import android.support.v7.app.AppCompatActivity; “a
我想将工具栏添加到我的 Android 应用程序,但是我得到了“致命异常:主要 java.lang.OutOfMemoryError"当我将 Activity 更改为 AppCompatActivit
您好,我想为此创建 float Activity ,我知道我可以将此样式设置为我的 Activity 。 android:theme="@android:style/Theme.Translucent
我使用的是Android Studio 1.5.1版本 如果我创建新项目并且 MainActivity 扩展 AppCompatActivity 那么它不起作用它会给出如下错误: 12-02
虽然之前有人问过这个问题,但没有一个解决方案对我有用。 问题是突然莫名其妙的 Android Studio 开始抛出 Cannot resolve symbol AppCompatActivity 的
我正在尝试添加搜索功能以从 Activity 列表中搜索项目。我正在为 android 5.1 尝试它,我想支持向后兼容性。我正在使用 android-support-v7-appcompat。 主要
我正在尝试为以编程方式扩展 AppCompatActivity 的类设置工具栏,但该 Activity 在运行时没有工具栏。我能够找到的关于使用工具栏的每个教程都以 XML 创建和添加工具栏,但我正在
我是从 Eclipse 开始接触 Android Studio 的新手,正在尝试调整设置。当我添加 Activity(空白 Activity )时,它会生成 java 文件,但会出现 extends
我正在开发一个使用 AppCompatActivity 的应用程序。这么多天以来,我一直在努力解决这个错误,但没有运气,也没有在 StackOverflow 上回答这样的答案。 gradle构建成功,
这个问题在这里已经有了答案: Out of Memory : android.support.v7.app.AppCompatActivity.onCreate (2 个答案) 关闭 6 年前。 再
我需要在我的应用程序中添加一个抽屉导航。这些 Activity 扩展了 AppCompatActivity。我希望在某些 Activity 中使用抽屉导航。抽屉导航应该看起来像 play store
我试图在全屏模式下隐藏标题栏。当我尝试使用 Activity 时,标题栏消失了。但是当我使用 AppCompatActivity 时,它仍然存在。知道为什么会这样吗?我应该更改 list 等其他内容吗
我正在将我的应用程序从 GCM 转移到 Firebase 以获取推送通知,这涉及更改某些依赖项。我将 gcm 依赖项更改为 firebase 并尝试同步项目,但在同步后我发现我所有实现了 AppCom
在我的应用程序的 build.gradle 中,依赖项是: compile 'com.android.support:support-v4:22.0.0' compile 'com.android.s
将 Activity 的父级从 Activity 更改为 AppCompatActivity 后,按钮上的字体发生了变化。例如,Remove-REMOVE。对于其他 Vews,字体没有改变。 为什么按
我的状态栏总是显示为黑色。我尝试了多种修复方法,但没有任何效果...我不敢相信这么简单的事情竟然如此复杂 这是我的 Style.xml: @color/cobalt @color/
重命名应用包名后,Android Studio 显示此错误can't resolve symbol AppCompatActivity 我尝试过的事情: 清理项目 清理 Android Studio
我有一个继承自 AppCompactActivity 的 Activity。在 Activity 集主题的 list 中: true @color/primary @colo
我是一名优秀的程序员,十分优秀!