Autore Topic: Extends Activity crash !  (Letto 468 volte)

Offline oktogon

  • Nuovo arrivato
  • *
  • Post: 6
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Samsung S5
  • Sistema operativo:
    Windows 10
Extends Activity crash !
« il: 07 Dicembre 2015, 11:55:56 CET »
0
Salve,
premetto che sono un neofita assoluto ...

Ho sviluppato la mia prima app e sto utilizzando SharedPreferences.

Per utilizzare i valori all'interno di una Class ho utilizzato 'extends activity'. La app crasha.

Ecco il codice:
Codice (Java): [Seleziona]
package it.rockopera.SCSremote;

/**
 * Created by Simone on 04/12/2015.
 */


import android.content.Context;
import android.content.SharedPreferences;

import android.util.Log;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;



public class TcpClient extends Activity{

    public String T_IP = "1";
  //  public int T_PORT = 2;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


            SharedPreferences userDetails = this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
            T_IP = userDetails.getString("IPkey", "192.168.1.100");  // (key, default)
          //  T_PORT = Integer.parseInt(userDetails.getString("PORTkey", "23"));





    }



    public final String SERVER_IP = T_IP ; //computer IP address

    public final int SERVER_PORT = 20;
    // message to send to the server
    private String mServerMessage;
    // sends message received notifications
    private OnMessageReceived mMessageListener = null;
    // while this is true, the server will continue running
    private boolean mRun = false;
    // used to send messages
    private PrintWriter mBufferOut;
    // used to read messages from the server
    private BufferedReader mBufferIn;

    /**
     * Constructor of the class. OnMessagedReceived listens for the messages received from server
     */

    public TcpClient(OnMessageReceived listener) {
        mMessageListener = listener;
    }

    /**
     * Sends the message entered by client to the server
     *
     * @param message text entered by client
     */

    public void sendMessage(String message) {
        if (mBufferOut != null && !mBufferOut.checkError()) {
            mBufferOut.println(message);
            mBufferOut.flush();
        }
    }

    /**
     * Close the connection and release the members
     */

    public void stopClient() {

        // send mesage that we are closing the connection
        sendMessage(Constants.CLOSED_CONNECTION+"Kazy");

        mRun = false;

        if (mBufferOut != null) {
            mBufferOut.flush();
            mBufferOut.close();
        }

        mMessageListener = null;
        mBufferIn = null;
        mBufferOut = null;
        mServerMessage = null;
    }

    public void run() {

        mRun = true;

        try {
            //here you must put your computer's IP address.
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

            Log.e("TCP Client", "C: Connecting..." + SERVER_IP);

            //create a socket to make the connection with the server
            Socket socket = new Socket(serverAddr, SERVER_PORT);

            try {

                //sends the message to the server
                mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

                //receives the message which the server sends back
                mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // send login name
                sendMessage(Constants.LOGIN_NAME+"Kazy");

                //in this while the client listens for the messages sent by the server
                while (mRun) {

                    mServerMessage = mBufferIn.readLine();

                    if (mServerMessage != null && mMessageListener != null) {
                        //call the method messageReceived from MyActivity class
                        mMessageListener.messageReceived(mServerMessage);
                    }

                }

                Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'");

            } catch (Exception e) {

                Log.e("TCP", "S: Error", e);

            } finally {
                //the socket must be closed. It is not possible to reconnect to this socket
                // after it is closed, which means a new socket instance has to be created.
                socket.close();
            }

        } catch (Exception e) {

            Log.e("TCP", "C: Error", e);

        }

    }

    //Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
    //class at on asynckTask doInBackground
    public interface OnMessageReceived {
        public void messageReceived(String message);
    }





}

ecco il LogCat
Codice (HTML): [Seleziona]
12-07 10:52:31.942 1287-4996/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=it.rockopera.SCSremote/.SplashActivity (has extras)} from uid 10008 on display 0
12-07 10:52:32.238 1287-1736/? I/ActivityManager: Start proc 10104:it.rockopera.SCSremote/u0a59 for activity it.rockopera.SCSremote/.SplashActivity
12-07 10:52:32.255 10104-10104/? I/art: Not late-enabling -Xcheck:jni (already on)
12-07 10:52:32.256 10104-10104/? I/art: Late-enabling JIT
12-07 10:52:32.341 10104-10104/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
12-07 10:52:32.537 10104-10104/? W/System: ClassLoader referenced unknown path: /data/app/it.rockopera.SCSremote-1/lib/x86
12-07 10:52:33.322 10104-10115/? I/art: Background sticky concurrent mark sweep GC freed 12860(551KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 5MB/5MB, paused 78.761ms total 437.276ms
12-07 10:52:33.656 941-1005/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
12-07 10:52:33.661 10104-10118/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-07 10:52:33.775 10104-10115/? I/art: Background partial concurrent mark sweep GC freed 37(1776B) AllocSpace objects, 0(0B) LOS objects, 39% free, 5MB/9MB, paused 126.134ms total 379.222ms
12-07 10:52:33.903 10104-10118/? I/OpenGLRenderer: Initialized EGL, version 1.4
12-07 10:52:34.082 10104-10118/? W/EGL_emulation: eglSurfaceAttrib not implemented
12-07 10:52:34.082 10104-10118/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7a00a0, error=EGL_SUCCESS
12-07 10:52:34.535 2355-2385/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab81b270
12-07 10:52:35.532 1287-1324/? I/ActivityManager: Displayed it.rockopera.SCSremote/.SplashActivity: +3s320ms (total +34s936ms)
12-07 10:52:38.452 1287-1734/? I/ActivityManager: START u0 {cmp=it.rockopera.SCSremote/.ClientActivity} from uid 10059 on display 0
12-07 10:52:38.678 10104-10118/? W/EGL_emulation: eglSurfaceAttrib not implemented
12-07 10:52:38.679 10104-10118/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7a5d00, error=EGL_SUCCESS
12-07 10:52:39.361 10104-10104/? I/Choreographer: Skipped 31 frames!  The application may be doing too much work on its main thread.
12-07 10:52:39.395 10104-10126/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                   Process: it.rockopera.SCSremote, PID: 10104
                                                   java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                       at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                       at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                       at java.lang.Thread.run(Thread.java:818)
                                                    Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                                                       at android.os.Handler.<init>(Handler.java:200)
                                                       at android.os.Handler.<init>(Handler.java:114)
                                                       at android.app.Activity.<init>(Activity.java:754)
                                                       at it.rockopera.SCSremote.TcpClient.<init>(TcpClient.java:60)
                                                       at it.rockopera.SCSremote.ClientActivity$ConnectTask.doInBackground(ClientActivity.java:237)
                                                       at it.rockopera.SCSremote.ClientActivity$ConnectTask.doInBackground(ClientActivity.java:231)
                                                       at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                       at java.lang.Thread.run(Thread.java:818)
12-07 10:52:39.442 1287-1749/? W/ActivityManager:   Force finishing activity it.rockopera.SCSremote/.ClientActivity
12-07 10:52:39.564 941-941/? E/EGL_emulation: tid 941: eglCreateSyncKHR(1243): error 0x3004 (EGL_BAD_ATTRIBUTE)
12-07 10:52:39.722 1287-1733/? W/art: Long monitor contention event with owner method=android.graphics.Bitmap com.android.server.wm.WindowManagerService.screenshotApplicationsInner(android.os.IBinder, int, int, int, boolean) from WindowManagerService.java:6223 waiters=0 for 178ms
12-07 10:52:39.765 10104-10118/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab818720
12-07 10:52:39.787 10104-10104/? D/AndroidRuntime: Shutting down VM
12-07 10:52:39.788 10104-10104/? I/Process: Sending signal. PID: 10104 SIG: 9
12-07 10:52:39.798 1287-1287/? W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService.crashApplication(com.android.server.am.ProcessRecord, android.app.ApplicationErrorReport$CrashInfo) from ActivityManagerService.java:12502 waiters=0 for 370ms
12-07 10:52:39.807 1287-1307/? W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService.crashApplication(com.android.server.am.ProcessRecord, android.app.ApplicationErrorReport$CrashInfo) from ActivityManagerService.java:12502 waiters=2 for 295ms
12-07 10:52:40.161 1287-1845/? I/OpenGLRenderer: Initialized EGL, version 1.4
12-07 10:52:40.311 1287-1307/? W/ActivityManager: Activity pause timeout for ActivityRecord{9419f6 u0 it.rockopera.SCSremote/.ClientActivity t159 f}
12-07 10:52:40.312 1287-1845/? W/EGL_emulation: eglSurfaceAttrib not implemented
12-07 10:52:40.313 1287-1845/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9b9fe060, error=EGL_SUCCESS
12-07 10:52:40.316 1287-1293/? I/art: Background partial concurrent mark sweep GC freed 10893(817KB) AllocSpace objects, 3(60KB) LOS objects, 33% free, 7MB/11MB, paused 18.552ms total 566.721ms
12-07 10:52:40.453 939-939/? E/lowmemorykiller: Error writing /proc/10104/oom_score_adj; errno=22
12-07 10:52:40.611 2355-2385/? W/EGL_emulation: eglSurfaceAttrib not implemented
12-07 10:52:40.611 2355-2385/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa1562c60, error=EGL_SUCCESS
12-07 10:52:40.638 1287-1734/? D/GraphicsStats: Buffer count: 4
12-07 10:52:40.647 1287-1341/? W/InputDispatcher: channel '41c4e15 it.rockopera.SCSremote/it.rockopera.SCSremote.SplashActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
12-07 10:52:40.647 1287-1341/? E/InputDispatcher: channel '41c4e15 it.rockopera.SCSremote/it.rockopera.SCSremote.SplashActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
12-07 10:52:40.652 1287-1341/? W/InputDispatcher: channel '8dfdf82 it.rockopera.SCSremote/it.rockopera.SCSremote.ClientActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
12-07 10:52:40.653 1287-1341/? E/InputDispatcher: channel '8dfdf82 it.rockopera.SCSremote/it.rockopera.SCSremote.ClientActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
12-07 10:52:40.675 1287-1642/? I/ActivityManager: Process it.rockopera.SCSremote (pid 10104) has died
12-07 10:52:40.751 1287-1734/? I/WindowState: WIN DEATH: Window{41c4e15 u0 it.rockopera.SCSremote/it.rockopera.SCSremote.SplashActivity}
12-07 10:52:40.752 1287-1734/? W/InputDispatcher: Attempted to unregister already unregistered input channel '41c4e15 it.rockopera.SCSremote/it.rockopera.SCSremote.SplashActivity (server)'
12-07 10:52:40.766 1287-4996/? W/art: Long monitor contention event with owner method=void com.android.server.wm.WindowAnimator$1.doFrame(long) from WindowAnimator.java:121 waiters=2 for 127ms
12-07 10:52:40.769 1287-4996/? I/WindowState: WIN DEATH: Window{8dfdf82 u0 it.rockopera.SCSremote/it.rockopera.SCSremote.ClientActivity}
12-07 10:52:40.769 1287-4996/? W/InputDispatcher: Attempted to unregister already unregistered input channel '8dfdf82 it.rockopera.SCSremote/it.rockopera.SCSremote.ClientActivity (server)'
12-07 10:52:40.808 1287-1642/? W/art: Long monitor contention event with owner method=void com.android.server.wm.WindowAnimator$1.doFrame(long) from WindowAnimator.java:121 waiters=3 for 111ms
12-07 10:52:40.823 1287-1749/? W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService$AppDeathRecipient.binderDied() from ActivityManagerService.java:1305 waiters=0 for 136ms
12-07 10:52:41.005 1287-1308/? I/Choreographer: Skipped 36 frames!  The application may be doing too much work on its main thread.
12-07 10:52:41.055 1287-1845/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa15fc870
12-07 10:52:41.237 1287-4996/? W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 10104 uid 10059
12-07 10:52:41.416 1287-1324/? W/AppOps: Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
12-07 10:52:41.609 1287-1324/? W/WindowAnimator: Failed to dispatch window animation state change.
                                                 android.os.DeadObjectException
                                                     at android.os.BinderProxy.transactNative(Native Method)
                                                     at android.os.BinderProxy.transact(Binder.java:503)
                                                     at android.view.IWindow$Stub$Proxy.onAnimationStopped(IWindow.java:534)
                                                     at com.android.server.wm.WindowAnimator.updateWindowsLocked(WindowAnimator.java:286)
                                                     at com.android.server.wm.WindowAnimator.animateLocked(WindowAnimator.java:678)
                                                     at com.android.server.wm.WindowAnimator.-wrap0(WindowAnimator.java)
                                                     at com.android.server.wm.WindowAnimator$1.doFrame(WindowAnimator.java:123)
                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)
                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                     at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:148)
                                                     at android.os.HandlerThread.run(HandlerThread.java:61)
                                                     at com.android.server.ServiceThread.run(ServiceThread.java:46)

Offline iClaude

  • Utente normale
  • ***
  • Post: 183
  • Respect: +13
    • Mostra profilo
  • Dispositivo Android:
    Samsung Galaxy S5
  • Sistema operativo:
    Windows 10
Re:Extends Activity crash !
« Risposta #1 il: 08 Dicembre 2015, 19:22:04 CET »
0
Salve,
premetto che sono un neofita assoluto ...

Ho sviluppato la mia prima app e sto utilizzando SharedPreferences.

Per utilizzare i valori all'interno di una Class ho utilizzato 'extends activity'. La app crasha.

E ci credo. Se estendi una Activity vuol dire che la tua classe è una Activity, il che non è il tuo caso.
Se la tua classe ha bisogno di determinati valori semplicemente inserisci un metodo setter con cui gli passi tutti i valori che vuoi.

Offline oktogon

  • Nuovo arrivato
  • *
  • Post: 6
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Samsung S5
  • Sistema operativo:
    Windows 10
Re:Extends Activity crash !
« Risposta #2 il: 08 Dicembre 2015, 20:06:06 CET »
0
Grazie! ...
concretamente, se io dovessi recuperare dei valori dalla classeA e utilizzarli nella classeB come dovrei impostare il codice ??

Offline iClaude

  • Utente normale
  • ***
  • Post: 183
  • Respect: +13
    • Mostra profilo
  • Dispositivo Android:
    Samsung Galaxy S5
  • Sistema operativo:
    Windows 10
Re:Extends Activity crash !
« Risposta #3 il: 08 Dicembre 2015, 20:52:30 CET »
0
Grazie! ...
concretamente, se io dovessi recuperare dei valori dalla classeA e utilizzarli nella classeB come dovrei impostare il codice ??

Beh, ad esempio se sei nella classeA quandi crei un'istanza della classeB puoi passare i valori che ti servono nel costruttore, oppure in un metodo setter pubblico.