0

I can not open the APK normally, but the debugger has no serious problems. I consider that the problem may occur in here.

MainActivity.java:

private TextView text;
public static Toast toast = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.text);
    text.setText("IP address : "+getLocalHostIp());

    new ServerListener().start();
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/setText"
        android:layout_width="match_parent"
        android:layout_height="51dp"
        android:layout_weight="1" />

</LinearLayout>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
陳毓凱
  • 29
  • 4

2 Answers2

2

You are using the wrong id for textView in your code which is causing the app to crash.

You've defined setText as id for the TextView in your .xml file android:id="@+id/setText", and you are using "text" as the id to map the TextView in your code text = (TextView) findViewById(R.id.text);, which obviously can not be found and is the cause of the problem.

to solve this, do one of the following:

  1. Change text = (TextView) findViewById(R.id.text); to text = (TextView) findViewById(R.id.setText);
  2. or change android:id="@+id/setText" to android:id="@+id/text"
Akhilesh Awasthi
  • 2,688
  • 4
  • 21
  • 30
0

Your id for TextView is wrong, you can use below code

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="51dp"
    android:layout_weight="1" />
Emad Seliem
  • 608
  • 1
  • 4
  • 5