2016-10-26 2 views
0

Я работаю над своим приложением, когда пользователь вводит IP-адрес и номер порта и будет подключаться к серверу. Как только я заполню IP-адрес и номер порта и . нажмите на связи, а также аварий приложений Эти ошибки я получаю EDIT:. добавлен XML для обоих видов деятельности EDIT2:. Изменена TextView ID и это все еще сбой, новое сообщение об ошибке на всем пути внизСбой приложения для Android-аранжировки при переходе на вторую активность

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.example.jobush50.irobot, PID: 2285 
       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jobush50.irobot/com.example.jobush50.irobot.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x22b8 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
       Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x22b8 
        at android.content.res.Resources.getText(Resources.java:331) 
        at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52) 
        at android.widget.TextView.setText(TextView.java:4550) 
        at com.example.jobush50.irobot.MainActivity.onCreate(MainActivity.java:45) 
        at android.app.Activity.performCreate(Activity.java:6664) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
        at android.app.ActivityThread.-wrap12(ActivityThread.java)  
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
        at android.os.Handler.dispatchMessage(Handler.java:102)  
        at android.os.Looper.loop(Looper.java:154)  
        at android.app.ActivityThread.main(ActivityThread.java:6077)  
        at java.lang.reflect.Method.invoke(Native Method)  
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  

My MainActivity

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

static int port; 
static String ip; 

private Socket socket; 

private static final int SERVERPORT = port; 
private static final String SERVER_IP = ip; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    new Thread(new ClientThread()).start(); 
    Intent intent = getIntent(); 
    String name = intent.getStringExtra("ip"); 
    int number = intent.getIntExtra("port", 0); 
    port = number; 
    ip = name; 


    TextView textView = (TextView) findViewById(R.id.textView1); 
    textView.setText(name); 

    TextView textView2 = (TextView) findViewById(R.id.textView2); 
    textView2.setText(number); 
} 

public void onClick(View view) { 
    try { 
     EditText et = (EditText) findViewById(R.id.EditText01); 
     String str = et.getText().toString(); 
     PrintWriter out = new PrintWriter(new BufferedWriter(
       new OutputStreamWriter(socket.getOutputStream())), 
       true); 
     out.println(str); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

class ClientThread implements Runnable { 

    @Override 
    public void run() { 

     try { 
      InetAddress serverAddr = InetAddress.getByName(SERVER_IP); 

      socket = new Socket(serverAddr, SERVERPORT); 

     } catch (UnknownHostException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 

    } 

} 
} 

SecondActivity

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.view.View; 
import android.widget.EditText; 
import android.content.Intent; 


public class SecondActivity extends AppCompatActivity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_second); 

    final EditText editText = (EditText)findViewById(R.id.editText_port); 
    final EditText editText2 = (EditText)findViewById(R.id.editText_port); 
    Button sendButton = (Button)findViewById(R.id.button_send); 

    sendButton.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 

      String name = editText.getText().toString(); 
      int number = Integer.parseInt(editText2.getText().toString()); 

      Intent intent = new Intent(getApplicationContext(),MainActivity.class); 
      intent.putExtra("ip", name); 
      intent.putExtra("port", number); 

      startActivity(intent); 


     } 



    }); 
} 






} 

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
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="com.example.jobush50.irobot.MainActivity" 
android:background="@android:color/darker_gray"> 

<EditText 
    android:id="@+id/EditText01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="Input" > 
</EditText> 

<Button 
    android:id="@+id/myButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="onClick" 
    android:text="Send" 
    android:layout_below="@+id/EditText01" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="19dp"> 
</Button> 

<TextView 
    android:text="TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_toRightOf="@+id/myButton" 
    android:layout_marginLeft="34dp" 
    android:layout_marginStart="34dp" 
    android:layout_marginBottom="223dp" 
    android:id="@+id/textView1" /> 

<TextView 
    android:text="TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_alignStart="@+id/textView1" 
    android:layout_marginLeft="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginBottom="98dp" 
    android:id="@+id/textView2" /> 

activity_second.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_second" 
android:layout_height="match_parent" 
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="com.example.jobush50.irobot.SecondActivity" 
android:layout_width="match_parent"> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textPersonName" 
    android:hint="Enter ip address" 
    android:ems="10" 
    android:layout_marginBottom="85dp" 
    android:id="@+id/editText_ip" 
    android:layout_above="@+id/button_send" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:text="Connect" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="144dp" 
    android:id="@+id/button_send" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="number" 
    android:hint="Enter port number" 
    android:ems="10" 
    android:layout_above="@+id/button_send" 
    android:layout_marginBottom="17dp" 
    android:id="@+id/editText_port" /> 

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.example.jobush50.irobot, PID: 2278 
       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jobush50.irobot/com.example.jobush50.irobot.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x22b8 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
       Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x22b8 
        at android.content.res.Resources.getText(Resources.java:331) 
        at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52) 
        at android.widget.TextView.setText(TextView.java:4550) 
        at com.example.jobush50.irobot.MainActivity.onCreate(MainActivity.java:45) 
        at android.app.Activity.performCreate(Activity.java:6664) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
        at android.app.ActivityThread.-wrap12(ActivityThread.java)  
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
        at android.os.Handler.dispatchMessage(Handler.java:102)  
        at android.os.Looper.loop(Looper.java:154)  
        at android.app.ActivityThread.main(ActivityThread.java:6077)  
        at java.lang.reflect.Method.invoke(Native Method)  
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  
Application terminated. 
+0

Не могли бы вы опубликовать содержимое 'activity_main' и' 'activity_second? Похоже, что есть проблема с присвоением/ссылкой ресурсов. – thomaspsk

+0

@thomaspsk Я добавил код xml для обоих действий –

+1

Вы ссылаетесь на 'R.id.editText_port' дважды в' SecondActivity' ... вы, вероятно, захотите использовать 'R.id.editText_ip' .... – user0815

ответ

0
textView2.setText(number); 

ОС пытается найти совпадения идентификатор, так как число INT попробовать

textView2.setText(String.valueOf(number)); 
Смежные вопросы