2016-08-18 149 views
0

我是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

後'的setContentView(佈局)移動你的''findViewByid' – Raghunandan

回答

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 -components到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

更新您mainactivity

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(); 
} 
相關問題