我是android studio的新手。我正在创建一个应用程序,它将使用套接字在PC和移动设备之间传递数据。 我已将基于命令的程序与android相结合。 在这种情况下,客户端将号码发送到服务器,服务器将检查它是否为素数,根据它,服务器将回复发送给客户端。但是我在启动主要活动时遇到致命异常。
Logcat是:
08-18 11:59:26.977 1728-1728/com.example.admin.test_purpose W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb4e68288)
08-18 11:59:26.977 1728-1728/com.example.admin.test_purpose E/AndroidRuntime:
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.admin.test_purpose/com.example.admin.test_purpose.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1825)
at com.example.admin.test_purpose.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
这是我的源文件
基于命令的服务器程序
import java.net.*;
import java.io.*;
class tcpServerPrime {
public static void main(String args[]) {
try {
ServerSocket ss = new ServerSocket(8001);
System.out.println("Server Started...............");
Socket s = ss.accept();
DataInputStream in = new DataInputStream(s.getInputStream());
int x= in.readInt();
DataOutputStream otc = new DataOutputStream(s.getOutputStream());
int y = x/2;
if(x ==1 || x ==2 || x ==3) {
otc.writeUTF(x + "is Prime");
System.exit(0);
}
for(int i=2; i<=y; i++) {
if(x%i != 0) {
otc.writeUTF(x + " is Prime");
} else {
otc.writeUTF(x + " is not Prime");
}
}
}catch(Exception e) {
System.out.println(e.toString());
}
}
}
主要活动
package com.example.admin.test_purpose;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.net.*;
import java.io.*;
public class MainActivity extends Activity {
EditText user_num = (EditText)findViewById(R.id.editText);
TextView reply = (TextView)findViewById(R.id.textView);
Button btnsbm = (Button)findViewById(R.id.button);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onclicklistener();
}
public void onclicklistener(){
btnsbm.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Socket cs = new Socket("192.168.1.101", 8001);
int a = Integer.parseInt(user_num.getText().toString());
DataOutputStream out = new DataOutputStream(cs.getOutputStream());
out.writeInt(a);
DataInputStream in = new DataInputStream(cs.getInputStream());
reply.setText(in.readUTF());
cs.close();
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_gravity="center_horizontal"
android:hint="@string/user_value"
android:layout_marginTop="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp" />
</LinearLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.test_purpose">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
答案 0 :(得分:2)
问题在于这三行:
EditText user_num = (EditText)findViewById(R.id.editText);
TextView reply = (TextView)findViewById(R.id.textView);
Button btnsbm = (Button)findViewById(R.id.button);
问题是,您在视图甚至初始化之前调用findViewById()
。
将findViewById
- 组件移至onCreate()
,然后它应该有效:
EditText user_num;
TextView reply;
Button btnsbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_num = (EditText)findViewById(R.id.editText);
reply = (TextView)findViewById(R.id.textView);
btnsbm = (Button)findViewById(R.id.button);
onclicklistener();
}
答案 1 :(得分:0)
更新主要活动
EditText user_num;
TextView reply;
Button btnsbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_num = (EditText)findViewById(R.id.editText);
reply = (TextView)findViewById(R.id.textView);
btnsbm = (Button)findViewById(R.id.button);
onclicklistener();
}