AppCompatActivity& ActionBarActivity

时间:2015-10-02 10:24:35

标签: java android eclipse android-studio

我一直在尝试关注Android聊天应用程序的教程。

我已经完成了使用AppCompatActivityActionBarActivity所需的所有配置,但是当我尝试运行我的应用程序时,它将停止工作。 我一直在寻找不同的解决方法并尝试各种解决方案,但似乎仍然无法完成,请帮助我

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>

我已完成访问权限 清单

     <uses-permission android:name="android.permission.INTERNET" />

我也参与了我的gradle

compile 'com.android.support:appcompat-v7:23.0.1'

我的logcat

  

E / AndroidRuntime:FATAL EXCEPTION:main进程:com.example.l335a04.walao,PID:11814 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.l335a04.walao / com.example.l335a04。 walao.MainActivity}:java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)。在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)android.app.ActivityThread.access $ 900(ActivityThread.java:172)android.app .ActivityThread $ H.handleMessage(ActivityThread.java:1422)位于android.app.A.运行时,android.O.Roper.Mopage(Handler.java:102)在android.app.Looper.loop(Looper.java:145)的android.app.ActivityThread。 main(ActivityThread.java:5832)位于java.lang.reflect.Method.invoke(Native Method)的java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit $在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)中的MethodAndArgsCaller.run(ZygoteInit.java:1399)引发者:java.lang.IllegalStateException:您需要使用Theme.AppCompat主题(或后代) )这项活动。在Android.support.v7.app.App.DeCompalDelegateImplV7.setContentView的android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:309)android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:278) (AppCompatDelegateImplV7.java:252)位于android.app的com.example.l335a04.walao.MainActivity.onCreate(MainActivity.java:35)的android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)。 Activity.performCreate(Activity.java:6221)位于android.app.AnsTmentation.handleLaunchActivity的android.ap.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)android.ap p.ActivityThread.performLaunchActivity(ActivityThread.java:2611)。 ActivityThread.java:2723)在Android.app.Handler.dispatchMessage(处理程序。)的android.app.ActivityThread.access $ 900(ActivityThread.java:172)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1422)。 java:102)在android.ap.Looper.loop(Looper.java:145)在android.ap p.ActivityThread.main(ActivityThread.java:5832)位于com.android.internal的java.lang.reflect.Method.invoke(Method.java:372)的java.lang.reflect.Method.invoke(Native Method)中。 os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)at com.android.internal.os.ZygoteInit.main(Zygoenter code hereteInit.java:1194)

1 个答案:

答案 0 :(得分:1)

来自LogCat:

  

您需要在此活动中使用Theme.AppCompat主题(或后代)。

我们可以知道您的AppCompatActivity需要Theme.AppCompat在清单中。打开styles.xml并将您的主题更改为:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" />

AndroidManifest.xml

 <activity android:name="MainActivity"
    android:theme="@style/AppTheme"
    ... />