This question already has answers here:
How do I fix a compilation error for unhandled exception on call to Thread.sleep()?

(2个答案)


3年前关闭。




我在为我的Android项目使用Java中的InetAddress时遇到困难。我包括了InetAddress库,但是它永远无法正常工作。
代码如下:
InetAddress giriAddress = InetAddress.getByName("www.girionjava.com");

但是,所有的时间告诉我:
Description Resource    Path    Location    Type
Default constructor cannot handle exception type UnknownHostException thrown by implicit super constructor. Must define an explicit constructor LauncherActivity.java   /src/my/app/client  line 25 Java Problem

我包括了图书馆:
import java.net.InetAddress;

在Android项目中使用InetAddress我必须怎么做?

我的项目的类(class)是:
public class LauncherActivity extends Activity
    {
        /** Called when the activity is first created. */

        Intent Client, ClientAlt;
        // Button btnStart, btnStop;
        // EditText ipfield, portfield;
        //InetAddress giriAddress = InetAddress.getByName("www.girionjava.com");
        //private InetAddress giriAddress;
        private InetAddress giriAddress;

        public LauncherActivity()
        {
            this.giriAddress=InetAddress.getByName("www.girionjava.com");
        }
        private String myIp = "MYIP"; // Put your IP in these quotes.
        private int myPort = PORT; // Put your port there, notice that there are no quotes here.

        @Override
        public void onStart()
            {
                super.onStart();
                onResume();
            }

        @Override
        public void onResume()
            {
                super.onResume();
                Client = new Intent(this, Client.class);
                Client.setAction(LauncherActivity.class.getName());
                getConfig();
                Client.putExtra("IP", myIp);
                Client.putExtra("PORT", myPort);

                startService(Client);
                moveTaskToBack(true);
            }

        @Override
        public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
//              setContentView(R.layout.main);
                Client = new Intent(this, Client.class);
                Client.setAction(LauncherActivity.class.getName());
                getConfig();
                Client.putExtra("IP", myIp);
                Client.putExtra("PORT", myPort);

                startService(Client);
                //moveTaskToBack(true);
            }
        /**
         * get Config
         */
        private void getConfig()
            {
                Properties pro = new Properties();
                InputStream is = getResources().openRawResource(R.raw.config);
                try
                    {
                        pro.load(is);
                    } catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                myIp = pro.getProperty("host");
                myPort = Integer.valueOf(pro.getProperty("prot"));
                System.out.println(myIp);
                System.out.println(myPort);
            }
    }

错误是我得到的。
Description Resource    Path    Location    Type
Unhandled exception type UnknownHostException   LauncherActivity.java   /Androrat/src/my/app/client line 31 Java Problem

图片:
java - 如何在Android中使用InetAddress获取IPAddress-LMLPHP
MY VERSION OF JAVA IS JAVA SE 1.6

最佳答案

我提出了两种选择:

  • 如果给定主机的IP是应用程序正常运行所必需的,则可以将其放入构造函数中,并将异常作为配置错误重新引发:

  • 公共(public)类MyClass
    {
    私有(private)InetAddress giriAddress;

    公共(public)MyClass(...)
    {
    尝试{
    this.giriAddress = InetAddress.getByName(“www.girionjava.com”);
    }
    捕获(UnknownHostException e)
    {
    抛出新的ServiceConfigurationError(e.toString(),e);
    }
    }
    }
  • 但是,如果不是强制性的,并且可能以某种方式恢复该错误,只需在构造函数的UnknownHostException子句中声明throws(这将迫使您在类的构造函数的所有调用层次结构中捕获/抛出该异常):

  • 公共(public)类MyClass
    {
    私有(private)InetAddress giriAddress;

    公共(public)MyClass(...)
    抛出UnknownHostException
    {
    this.giriAddress = InetAddress.getByName(“www.girionjava.com”);
    }
    }

    关于java - 如何在Android中使用InetAddress获取IPAddress ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44182463/

    10-12 04:18