Sad.Boy
Sad.Boy

Reputation: 1

AppCompatActivity & ActionBarActivity

I have been trying to follow a tutorial on Android Chat Application.

I have done all the configuration needed to use AppCompatActivity and ActionBarActivity, but when i try to run my application, it will stop working. I've been looking through different ways of solving and tried all sort of solutions but it still seems can't be done, please help me

java file :

       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 FILE :

    <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>

I have done the access permission Manifest

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

I also included in my gradle

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

my 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)

Upvotes: 0

Views: 814

Answers (1)

Anggrayudi H
Anggrayudi H

Reputation: 15165

From the LogCat:

You need to use a Theme.AppCompat theme (or descendant) with this activity.

We can know that your AppCompatActivity needs Theme.AppCompat in manifest. Open styles.xml and change your theme to:

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

In AndroidManifest.xml:

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

Upvotes: 1

Related Questions