2013-04-10 4 views
0

Android 앱에 문제가 있습니다. Android-Device와 PC간에 직렬 방식으로 통신하려고합니다. 메신저 Android NDK를 사용하는 경우. serialport를 사용하는 방법을 사용하자 마자 오류 메시지가 나타납니다. .getSerialPort() 또는 .getOutputStream()Android : 활동을 시작할 수 없습니다. 구성 요소 정보

물론 Google을 통해이 문제를 해결하려고했습니다. 하지만 사실 나는 아직 내 문제에 대한 해결책을 찾지 못했습니다. 내 매니페스트에 필요한 모든 것이 포함되어 있는지 확인하고 있습니다. 바라건대 누군가 내 코드에서 문제를 찾을 수 있기를 바랍니다.

메신저가 stackoverflow에 대한 질문을 처음 작성한 것이므로 앞으로 더 나은 방법으로 묻는 방법에 대한 힌트를 얻으려고합니다. 정보가 누락 된 경우 알려 주시면 최대한 빨리 게시 해 드리겠습니다.

package com.example.serialtest3; 

import java.io.IOException; 
import java.io.OutputStream; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import com.example.serialtest3.SerialPort; 

public class MainActivity extends Activity { 

TextView tv; 
EditText et;  
SerialPort s; 

protected OutputStream out= new OutputStream() { 

@Override 
public void write(int oneByte) throws IOException { 
} 
}; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    out = s.getOutputStream(); 
    try { 
     s.getSerialPort();       
    } catch (InvalidParameterException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (SecurityException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    Button b1 = (Button) findViewById(R.id.button1); 
    b1.setOnClickListener(new View.OnClickListener() { 


     public void onClick(View v) { 

      String text = et.getText().toString(); 
      if (v.getId()==R.id.button1){ 
       try { 
       out.write(new String(text).getBytes()); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

     } 
    }); 

    et =(EditText) findViewById(R.id.editT); 
} 

하여 SerialPort :

package com.example.serialtest3; 

import java.io.File; 
import java.io.FileDescriptor; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.security.InvalidParameterException; 
import android.util.Log; 
import com.example.serialtest3.*; 

public class SerialPort { 

private static final String TAG = "SerialPort"; 
SerialPort serial; 
/* 
* Do not remove or rename the field mFd: it is used by native method close(); 
*/ 
private FileDescriptor mFd; 
private FileInputStream mFileInputStream; 
private FileOutputStream mFileOutputStream; 

public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException { 

    /* Check access permission */ 
    if (!device.canRead() || !device.canWrite()) { 
     try { 
      /* Missing read/write permission, trying to chmod the file */ 
      Process su; 
      su = Runtime.getRuntime().exec("/system/bin/su"); 
      String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" 
        + "exit\n"; 
      su.getOutputStream().write(cmd.getBytes()); 
      if ((su.waitFor() != 0) || !device.canRead() 
        || !device.canWrite()) { 
       throw new SecurityException(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      throw new SecurityException(); 
     } 
    } 

    mFd = open(device.getAbsolutePath(), baudrate, flags); 
    if (mFd == null) { 
     Log.e(TAG, "native open returns null"); 
     throw new IOException(); 
    } 
    mFileInputStream = new FileInputStream(mFd); 
    mFileOutputStream = new FileOutputStream(mFd); 

} 

public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException { 
    String path = "/dev/ttyO0"; 
    int baudrate= 115200; 
    serial = new SerialPort(new File(path), baudrate, 0); 
    return serial; 
} 

// Getters and setters 
public InputStream getInputStream() { 
    return mFileInputStream; 
} 

public OutputStream getOutputStream() { 
    return mFileOutputStream; 
} 

// JNI 
private native static FileDescriptor open(String path, int baudrate, int flags); 
public native void close(); 
static { 
    System.loadLibrary("serial_port"); // this lib contains open and close method which are written in the c-code 
} 

}

안드로이드 매니페스트 :

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.serialtest3" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="10" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.serialtest3.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

내 mainactivity입니다

오류 메시지 :

01-01 04:03:29.451: D/AndroidRuntime(2370): Shutting down VM 
01-01 04:03:29.451: W/dalvikvm(2370): threadid=1: thread exiting with uncaught exception (group=0x40a421f8) 
01-01 04:03:29.451: E/AndroidRuntime(2370): FATAL EXCEPTION: main 
01-01 04:03:29.451: E/AndroidRuntime(2370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.serialtest3/com.example.serialtest3.MainActivity}: java.lang.NullPointerException 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at android.app.ActivityThread.access$600(ActivityThread.java:123) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at android.os.Looper.loop(Looper.java:137) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at dalvik.system.NativeStart.main(Native Method) 
01-01 04:03:29.451: E/AndroidRuntime(2370): Caused by: java.lang.NullPointerException 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at com.example.serialtest3.MainActivity.onCreate(MainActivity.java:65) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at android.app.Activity.performCreate(Activity.java:4465) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
01-01 04:03:29.451: E/AndroidRuntime(2370):  ... 11 more 
01-01 04:04:56.035: D/AndroidRuntime(2421): Shutting down VM 
01-01 04:04:56.035: W/dalvikvm(2421): threadid=1: thread exiting with uncaught exception (group=0x40a421f8) 
01-01 04:04:56.035: E/AndroidRuntime(2421): FATAL EXCEPTION: main 
01-01 04:04:56.035: E/AndroidRuntime(2421): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.serialtest3/com.example.serialtest3.MainActivity}: java.lang.NullPointerException 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at android.app.ActivityThread.access$600(ActivityThread.java:123) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at android.os.Looper.loop(Looper.java:137) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at dalvik.system.NativeStart.main(Native Method) 
01-01 04:04:56.035: E/AndroidRuntime(2421): Caused by: java.lang.NullPointerException 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at com.example.serialtest3.MainActivity.onCreate(MainActivity.java:54) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at android.app.Activity.performCreate(Activity.java:4465) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
01-01 04:04:56.035: E/AndroidRuntime(2421):  ... 11 more 
01-01 04:05:03.232: I/Process(2421): Sending signal. PID: 2421 SIG: 9 

activity_main.xml :이 초기화되지

<RelativeLayout 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: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=".MainActivity" > 


<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="155dp" 
    android:text="Button" /> 

<EditText 
    android:id="@+id/editT" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="80dp" 
    android:ems="10" 
    android:lines="1" /> 

답변

3
SerialPort s; 

. 그래서 이것은 null을 가리킬 것입니다. 그것이 던지고있는 이유입니다 Null Pointer Exception

+0

이게 제 실수를 해결했습니다. 고맙습니다. :) 나는 그것을 보지 못했습니다./ – DrDieHard

+0

디버깅 기술을 향상 시키십시오. 이는 직접적인 오류입니다. 디버그 로그를 살펴보고 문제의 원인이되는 행을 추적하십시오. – Triode

관련 문제