Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android重构URL onClick按钮_Java_Android_Url_Button_Android Activity - Fatal编程技术网

Java Android重构URL onClick按钮

Java Android重构URL onClick按钮,java,android,url,button,android-activity,Java,Android,Url,Button,Android Activity,我想制作一个android应用程序,当应用程序的用户点击一个按钮时,它应该转到树莓PI的IP地址。此Pi位于网络本身上,具有静态IP,但应由用户指定一次 我有一个文本字段,用户可以在其中输入Pi的IP地址。这应该存储在SharedReferences中。以下是此部分的代码: public void save(View view) { editor.putString("homeIP", textHomeIP.getText().toString()); editor.commit

我想制作一个android应用程序,当应用程序的用户点击一个按钮时,它应该转到树莓PI的IP地址。此Pi位于网络本身上,具有静态IP,但应由用户指定一次

我有一个文本字段,用户可以在其中输入Pi的IP地址。这应该存储在SharedReferences中。以下是此部分的代码:

public void save(View view) {
    editor.putString("homeIP", textHomeIP.getText().toString());
    editor.commit();

    editor.putString("PiIP", textPiIP.getText().toString());
    editor.commit();

    piIP = textPiIP.getText().toString();
    homeIP = textHomeIP.getText().toString();

    makeURL();

    Toast.makeText(getApplication(), "Saved", Toast.LENGTH_SHORT);

    finish();
}
单击“保存”按钮时,此方法将运行。该类扩展MainActivity,并在单击设置按钮时使用设置xml打开。由于某些原因,在MainActivity中无法更改URL。下面是更改URL的方法

protected void makeURL()
{
    if(mSocket != null)
    {
        mSocket.disconnect();
    }

    try
    {
        url = new URL("http://" + piIP);
        Log.d("URL", url.toString());
    }
    catch (java.net.MalformedURLException e) {}

    try
    {
        mSocket = IO.socket(url.toString(), opts);
    }
    catch (java.net.URISyntaxException e)
    {
        Log.d("ERROR", "Cant initialize socket: " + e.toString());
    }

    mSocket.connect();
    mSocket.on("detection-start", detectionStart);
    mSocket.on("detection-end", detectionEnd);
}
套接字实际上是连接的,但由于某种原因URL保持不变。我在onCreate中运行这个方法,并在这之前启动piIP

以下是onCreate方法:

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

    ipActivity = new IPActivity();
    ipActivity.execute();

    setContentView(R.layout.content_main);
    notificationSettings();

    //connectionChanger = new ConnectionChangeReceiver();

    sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    //sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

    editor = sharedPref.edit();

    homeIP = sharedPref.getString("homeIP", "localhost");
    piIP = sharedPref.getString("PiIP", "localhost");

    Log.d("Pi IP", piIP);

    makeURL();
}
最后是openBrowser()方法


有人能告诉我为什么保存设置时URL没有更改吗?

原来我使用的是getPreferences而不是GetSharedReferences。它修复了创建URL的问题。我自己创建了子类,并通过GetSharedReferences加载de ip地址

您可以详细说明一下:“套接字确实连接了,但URL出于某种原因保持不变”?用户套接字连接到用户的给定IP地址,并可以从服务器接收事件。但是URL仍然链接到LocalHost您在logcat中看到任何错误吗?我看到您将Log.d放在Makeurl中,没有任何错误。当我保存设置时,piIP是给定ip中的设置,但url仍然是localhostcheck your editor.commit()通过使用布尔isSuccess=editor.commit()成功;
public void openBrowser(View view)
{
    makeURL();
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url.toString()));
    startActivity(browserIntent);
}