我正在处理我的应用程序,用户在其中输入IP地址和端口号并将连接到服务器。一旦我填写了IP地址和端口号并按下连接,应用程序就会崩溃。是我得到的错误。 编辑:为两个活动添加了xml EDIT2:更改了textview ID,它仍然崩溃,新的错误消息一直向下。
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)
我的主要活动
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 :(得分:0)
textView2.setText(number);
操作系统正在尝试查找匹配ID,因为number是int try
textView2.setText(String.valueOf(number));