Android 试图在Mainactivity中启动线程会使我的程序崩溃

Android 试图在Mainactivity中启动线程会使我的程序崩溃,android,multithreading,android-activity,crash,Android,Multithreading,Android Activity,Crash,我正在尝试做一个应用程序,可以读取无线强度不断,并做一些处理。为了使应用程序更好地运行,我考虑使用线程,因为我的应用程序需要连续监控wifi信号的变化。首先,我没有使用线程,当我触发单击事件开始处理wifi信号时,它崩溃了(处理代码在while循环中运行) 我的主要活动如下所示: public class MainActivity extends Activity { SampleThread s; @Override protected void onCreate(Bundle savedI

我正在尝试做一个应用程序,可以读取无线强度不断,并做一些处理。为了使应用程序更好地运行,我考虑使用线程,因为我的应用程序需要连续监控wifi信号的变化。首先,我没有使用线程,当我触发单击事件开始处理wifi信号时,它崩溃了(处理代码在while循环中运行)

我的主要活动如下所示:

public class MainActivity extends Activity {
SampleThread s;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    s = new SampleThread(this);
    s.start();

}

public void changeState(View v) {
Switch sw =(Switch) findViewById(R.id.switchx);
    final boolean state = sw.isChecked();
    if (state) {
        s.on();
    } 
    else {
        s.off();
    }
}
09-14 17:46:29.902: W/dalvikvm(3632): threadid=11: thread exiting with uncaught exception (group=0x41c9fd40)
09-14 17:46:29.904: E/AndroidRuntime(3632): FATAL EXCEPTION: Thread-6876
09-14 17:46:29.904: E/AndroidRuntime(3632): Process: com.example.tet, PID: 3632
09-14 17:46:29.904: E/AndroidRuntime(3632): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-14 17:46:29.904: E/AndroidRuntime(3632):     at android.os.Handler.<init>(Handler.java:200)
09-14 17:46:29.904: E/AndroidRuntime(3632):     at android.os.Handler.<init>(Handler.java:114)
09-14 17:46:29.904: E/AndroidRuntime(3632):     at com.example.tet.SampleThread.work(SampleThread.java:29)
09-14 17:46:29.904: E/AndroidRuntime(3632):     at com.example.tet.SampleThread.run(SampleThread.java:23)
线程实现:

public class SampleThread extends Thread {

boolean flag;
Context context;

public SampleThread(Activity c) {
    context =c;
}

@Override
public void run() {
    while(true){
        while(flag){
            work();             
        }
    }
}

void work(){
    //lines of code which process signal strngth
}

void on(){
    flag = true;
}
void off(){
    flag = false;
}

}
如果开关未打开,我的线程将不工作,并且当开关打开时,它将在执行实际工作的while循环中启用一个标志

我用带有swing UI的Java尝试了一个类似的代码,它工作起来没有任何问题。我对安卓不是很在行,我希望弄清楚我在这里做错了什么

我的Logcat看起来像这样:

public class MainActivity extends Activity {
SampleThread s;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    s = new SampleThread(this);
    s.start();

}

public void changeState(View v) {
Switch sw =(Switch) findViewById(R.id.switchx);
    final boolean state = sw.isChecked();
    if (state) {
        s.on();
    } 
    else {
        s.off();
    }
}
09-14 17:46:29.902: W/dalvikvm(3632): threadid=11: thread exiting with uncaught exception (group=0x41c9fd40)
09-14 17:46:29.904: E/AndroidRuntime(3632): FATAL EXCEPTION: Thread-6876
09-14 17:46:29.904: E/AndroidRuntime(3632): Process: com.example.tet, PID: 3632
09-14 17:46:29.904: E/AndroidRuntime(3632): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-14 17:46:29.904: E/AndroidRuntime(3632):     at android.os.Handler.<init>(Handler.java:200)
09-14 17:46:29.904: E/AndroidRuntime(3632):     at android.os.Handler.<init>(Handler.java:114)
09-14 17:46:29.904: E/AndroidRuntime(3632):     at com.example.tet.SampleThread.work(SampleThread.java:29)
09-14 17:46:29.904: E/AndroidRuntime(3632):     at com.example.tet.SampleThread.run(SampleThread.java:23)
09-14 17:46:29.902:W/dalvikvm(3632):threadid=11:线程以未捕获异常退出(组=0x41c9fd40)
09-14 17:46:29.904:E/AndroidRuntime(3632):致命异常:线程6876
09-1417:46:29.904:E/AndroidRuntime(3632):进程:com.example.tet,PID:3632
09-14 17:46:29.904:E/AndroidRuntime(3632):java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序
09-1417:46:29.904:E/AndroidRuntime(3632):在android.os.Handler.(Handler.java:200)
09-1417:46:29.904:E/AndroidRuntime(3632):在android.os.Handler.(Handler.java:114)
09-14 17:46:29.904:E/AndroidRuntime(3632):在com.example.tet.SampleThread.work(SampleThread.java:29)上
09-14 17:46:29.904:E/AndroidRuntime(3632):在com.example.tet.SampleThread.run(SampleThread.java:23)上

试试:runOnUiThread;而不是s.start()

Try:runOnUiThread(s);而不是s.start()

Try:runOnUiThread(s);而不是s.start()

Try:runOnUiThread(s);而不是s.start()

从onCreate方法以这种方式运行它

runOnUiThread(new Runnable() {
  public void run() {
   s.start();
  }
});

从onCreate方法以这种方式运行它

runOnUiThread(new Runnable() {
  public void run() {
   s.start();
  }
});

从onCreate方法以这种方式运行它

runOnUiThread(new Runnable() {
  public void run() {
   s.start();
  }
});

从onCreate方法以这种方式运行它

runOnUiThread(new Runnable() {
  public void run() {
   s.start();
  }
});

为了保持Android应用程序的最大效率,您应该使用Android Serivce或Android Intent服务。 如果你想继续使用Wi_-fi,我建议你使用Android IntentService类

有一个链接:

在这里,我将描述它的工作原理,并展示一些代码:

1)您应该创建新类(例如WiFiCheckerService)并扩展IntentService:

public class WiFiCheckerService extends IntentService
{

   //In this methos serivce receive from other class that it should start operations
   @Override
   protected void onHandleIntent(Intent intent) 
   {
    Thread thread = new Thread(wifiOperations_Runnable);
    thread.start();
   }

  class wifiOperations_Runnable() implements Runnable
  {
     @Override
     public void run()
     {
      while(true)
       {
         //checking wifi state or something...true/false

         broadcastState(state);
       }

     }
  }

   //broadcast wifi state to your MainActivity (where you started service):
   public broadcastState(boolean wifiState)
   {
    Intent intent = new Intent();
    intent.putExtra("WIFI_STATE", wifiState);
    sendBroadcast(intent);
   }

}
public MainActivity extends Activity{

..attributes...


private BroadcastReceiver receiver = new BroadcastReceiver() {


    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle bundle = intent.getExtras();
      if (bundle != null) 
        {
        boolean State = bundle.getBoolean(WIFI_STATE);
        if(State==true
       {
         ...wifi works...
       }
       else
      {
         ..doesnt work...
       }
      }
    }
  };


     ...onCreate(...)
    {
      Intent intent = new Intent(this, WiFiCheckerService.class);
      startSerivce(intent);
    }

 @Override
  protected void onResume()
 {
    super.onResume();
    registerReceiver(receiver);
  }

  @Override
  protected void onPause()
 {
    super.onPause();
    unregisterReceiver(receiver);
  }
}
2)活动类:在那里您将启动IntentService并从中接收WiFi状态:

public class WiFiCheckerService extends IntentService
{

   //In this methos serivce receive from other class that it should start operations
   @Override
   protected void onHandleIntent(Intent intent) 
   {
    Thread thread = new Thread(wifiOperations_Runnable);
    thread.start();
   }

  class wifiOperations_Runnable() implements Runnable
  {
     @Override
     public void run()
     {
      while(true)
       {
         //checking wifi state or something...true/false

         broadcastState(state);
       }

     }
  }

   //broadcast wifi state to your MainActivity (where you started service):
   public broadcastState(boolean wifiState)
   {
    Intent intent = new Intent();
    intent.putExtra("WIFI_STATE", wifiState);
    sendBroadcast(intent);
   }

}
public MainActivity extends Activity{

..attributes...


private BroadcastReceiver receiver = new BroadcastReceiver() {


    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle bundle = intent.getExtras();
      if (bundle != null) 
        {
        boolean State = bundle.getBoolean(WIFI_STATE);
        if(State==true
       {
         ...wifi works...
       }
       else
      {
         ..doesnt work...
       }
      }
    }
  };


     ...onCreate(...)
    {
      Intent intent = new Intent(this, WiFiCheckerService.class);
      startSerivce(intent);
    }

 @Override
  protected void onResume()
 {
    super.onResume();
    registerReceiver(receiver);
  }

  @Override
  protected void onPause()
 {
    super.onPause();
    unregisterReceiver(receiver);
  }
}

希望这会有帮助,不要犹豫问更多的问题

要保持Android应用程序的最大效率,您应该使用Android Serivce或Android Intent服务。 如果你想继续使用Wi_-fi,我建议你使用Android IntentService类

有一个链接:

在这里,我将描述它的工作原理,并展示一些代码:

1)您应该创建新类(例如WiFiCheckerService)并扩展IntentService:

public class WiFiCheckerService extends IntentService
{

   //In this methos serivce receive from other class that it should start operations
   @Override
   protected void onHandleIntent(Intent intent) 
   {
    Thread thread = new Thread(wifiOperations_Runnable);
    thread.start();
   }

  class wifiOperations_Runnable() implements Runnable
  {
     @Override
     public void run()
     {
      while(true)
       {
         //checking wifi state or something...true/false

         broadcastState(state);
       }

     }
  }

   //broadcast wifi state to your MainActivity (where you started service):
   public broadcastState(boolean wifiState)
   {
    Intent intent = new Intent();
    intent.putExtra("WIFI_STATE", wifiState);
    sendBroadcast(intent);
   }

}
public MainActivity extends Activity{

..attributes...


private BroadcastReceiver receiver = new BroadcastReceiver() {


    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle bundle = intent.getExtras();
      if (bundle != null) 
        {
        boolean State = bundle.getBoolean(WIFI_STATE);
        if(State==true
       {
         ...wifi works...
       }
       else
      {
         ..doesnt work...
       }
      }
    }
  };


     ...onCreate(...)
    {
      Intent intent = new Intent(this, WiFiCheckerService.class);
      startSerivce(intent);
    }

 @Override
  protected void onResume()
 {
    super.onResume();
    registerReceiver(receiver);
  }

  @Override
  protected void onPause()
 {
    super.onPause();
    unregisterReceiver(receiver);
  }
}
2)活动类:在那里您将启动IntentService并从中接收WiFi状态:

public class WiFiCheckerService extends IntentService
{

   //In this methos serivce receive from other class that it should start operations
   @Override
   protected void onHandleIntent(Intent intent) 
   {
    Thread thread = new Thread(wifiOperations_Runnable);
    thread.start();
   }

  class wifiOperations_Runnable() implements Runnable
  {
     @Override
     public void run()
     {
      while(true)
       {
         //checking wifi state or something...true/false

         broadcastState(state);
       }

     }
  }

   //broadcast wifi state to your MainActivity (where you started service):
   public broadcastState(boolean wifiState)
   {
    Intent intent = new Intent();
    intent.putExtra("WIFI_STATE", wifiState);
    sendBroadcast(intent);
   }

}
public MainActivity extends Activity{

..attributes...


private BroadcastReceiver receiver = new BroadcastReceiver() {


    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle bundle = intent.getExtras();
      if (bundle != null) 
        {
        boolean State = bundle.getBoolean(WIFI_STATE);
        if(State==true
       {
         ...wifi works...
       }
       else
      {
         ..doesnt work...
       }
      }
    }
  };


     ...onCreate(...)
    {
      Intent intent = new Intent(this, WiFiCheckerService.class);
      startSerivce(intent);
    }

 @Override
  protected void onResume()
 {
    super.onResume();
    registerReceiver(receiver);
  }

  @Override
  protected void onPause()
 {
    super.onPause();
    unregisterReceiver(receiver);
  }
}

希望这会有帮助,不要犹豫问更多的问题

要保持Android应用程序的最大效率,您应该使用Android Serivce或Android Intent服务。 如果你想继续使用Wi_-fi,我建议你使用Android IntentService类

有一个链接:

在这里,我将描述它的工作原理,并展示一些代码:

1)您应该创建新类(例如WiFiCheckerService)并扩展IntentService:

public class WiFiCheckerService extends IntentService
{

   //In this methos serivce receive from other class that it should start operations
   @Override
   protected void onHandleIntent(Intent intent) 
   {
    Thread thread = new Thread(wifiOperations_Runnable);
    thread.start();
   }

  class wifiOperations_Runnable() implements Runnable
  {
     @Override
     public void run()
     {
      while(true)
       {
         //checking wifi state or something...true/false

         broadcastState(state);
       }

     }
  }

   //broadcast wifi state to your MainActivity (where you started service):
   public broadcastState(boolean wifiState)
   {
    Intent intent = new Intent();
    intent.putExtra("WIFI_STATE", wifiState);
    sendBroadcast(intent);
   }

}
public MainActivity extends Activity{

..attributes...


private BroadcastReceiver receiver = new BroadcastReceiver() {


    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle bundle = intent.getExtras();
      if (bundle != null) 
        {
        boolean State = bundle.getBoolean(WIFI_STATE);
        if(State==true
       {
         ...wifi works...
       }
       else
      {
         ..doesnt work...
       }
      }
    }
  };


     ...onCreate(...)
    {
      Intent intent = new Intent(this, WiFiCheckerService.class);
      startSerivce(intent);
    }

 @Override
  protected void onResume()
 {
    super.onResume();
    registerReceiver(receiver);
  }

  @Override
  protected void onPause()
 {
    super.onPause();
    unregisterReceiver(receiver);
  }
}
2)活动类:在那里您将启动IntentService并从中接收WiFi状态:

public class WiFiCheckerService extends IntentService
{

   //In this methos serivce receive from other class that it should start operations
   @Override
   protected void onHandleIntent(Intent intent) 
   {
    Thread thread = new Thread(wifiOperations_Runnable);
    thread.start();
   }

  class wifiOperations_Runnable() implements Runnable
  {
     @Override
     public void run()
     {
      while(true)
       {
         //checking wifi state or something...true/false

         broadcastState(state);
       }

     }
  }

   //broadcast wifi state to your MainActivity (where you started service):
   public broadcastState(boolean wifiState)
   {
    Intent intent = new Intent();
    intent.putExtra("WIFI_STATE", wifiState);
    sendBroadcast(intent);
   }

}
public MainActivity extends Activity{

..attributes...


private BroadcastReceiver receiver = new BroadcastReceiver() {


    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle bundle = intent.getExtras();
      if (bundle != null) 
        {
        boolean State = bundle.getBoolean(WIFI_STATE);
        if(State==true
       {
         ...wifi works...
       }
       else
      {
         ..doesnt work...
       }
      }
    }
  };


     ...onCreate(...)
    {
      Intent intent = new Intent(this, WiFiCheckerService.class);
      startSerivce(intent);
    }

 @Override
  protected void onResume()
 {
    super.onResume();
    registerReceiver(receiver);
  }

  @Override
  protected void onPause()
 {
    super.onPause();
    unregisterReceiver(receiver);
  }
}

希望这会有帮助,不要犹豫问更多的问题

要保持Android应用程序的最大效率,您应该使用Android Serivce或Android Intent服务。 如果你想继续使用Wi_-fi,我建议你使用Android IntentService类

有一个链接:

在这里,我将描述它的工作原理,并展示一些代码:

1)您应该创建新类(例如WiFiCheckerService)并扩展IntentService:

public class WiFiCheckerService extends IntentService
{

   //In this methos serivce receive from other class that it should start operations
   @Override
   protected void onHandleIntent(Intent intent) 
   {
    Thread thread = new Thread(wifiOperations_Runnable);
    thread.start();
   }

  class wifiOperations_Runnable() implements Runnable
  {
     @Override
     public void run()
     {
      while(true)
       {
         //checking wifi state or something...true/false

         broadcastState(state);
       }

     }
  }

   //broadcast wifi state to your MainActivity (where you started service):
   public broadcastState(boolean wifiState)
   {
    Intent intent = new Intent();
    intent.putExtra("WIFI_STATE", wifiState);
    sendBroadcast(intent);
   }

}
public MainActivity extends Activity{

..attributes...


private BroadcastReceiver receiver = new BroadcastReceiver() {


    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle bundle = intent.getExtras();
      if (bundle != null) 
        {
        boolean State = bundle.getBoolean(WIFI_STATE);
        if(State==true
       {
         ...wifi works...
       }
       else
      {
         ..doesnt work...
       }
      }
    }
  };


     ...onCreate(...)
    {
      Intent intent = new Intent(this, WiFiCheckerService.class);
      startSerivce(intent);
    }

 @Override
  protected void onResume()
 {
    super.onResume();
    registerReceiver(receiver);
  }

  @Override
  protected void onPause()
 {
    super.onPause();
    unregisterReceiver(receiver);
  }
}
2)活动类:在那里您将启动IntentService并从中接收WiFi状态:

public class WiFiCheckerService extends IntentService
{

   //In this methos serivce receive from other class that it should start operations
   @Override
   protected void onHandleIntent(Intent intent) 
   {
    Thread thread = new Thread(wifiOperations_Runnable);
    thread.start();
   }

  class wifiOperations_Runnable() implements Runnable
  {
     @Override
     public void run()
     {
      while(true)
       {
         //checking wifi state or something...true/false

         broadcastState(state);
       }

     }
  }

   //broadcast wifi state to your MainActivity (where you started service):
   public broadcastState(boolean wifiState)
   {
    Intent intent = new Intent();
    intent.putExtra("WIFI_STATE", wifiState);
    sendBroadcast(intent);
   }

}
public MainActivity extends Activity{

..attributes...


private BroadcastReceiver receiver = new BroadcastReceiver() {


    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle bundle = intent.getExtras();
      if (bundle != null) 
        {
        boolean State = bundle.getBoolean(WIFI_STATE);
        if(State==true
       {
         ...wifi works...
       }
       else
      {
         ..doesnt work...
       }
      }
    }
  };


     ...onCreate(...)
    {
      Intent intent = new Intent(this, WiFiCheckerService.class);
      startSerivce(intent);
    }

 @Override
  protected void onResume()
 {
    super.onResume();
    registerReceiver(receiver);
  }

  @Override
  protected void onPause()
 {
    super.onPause();
    unregisterReceiver(receiver);
  }
}

希望这会有帮助,不要犹豫问更多的问题

这听起来像是你试图在线程中创建一个吐司。如果是这样的话,试着把它注释掉。您的代码有一些问题,它不是线程安全的,我认为您在工作函数中使用了handler对象。这听起来像是您试图在线程中创建Toast。如果是这样的话,试着把它注释掉。您的代码有一些问题,它不是线程安全的,我认为您在工作函数中使用了handler对象。这听起来像是您试图在线程中创建Toast。如果是这样的话,试着把它注释掉。您的代码有一些问题,它不是线程安全的,我认为您在工作函数中使用了handler对象。这听起来像是您试图在线程中创建Toast。如果是这样的话,试着把它注释掉。你的代码有一些问题,它不是线程安全的,我认为你在工作函数中使用了handler对象。谢谢你简洁的回答!谢谢你简洁的回答!谢谢你简洁的回答!谢谢你简洁的回答!