2013-04-10 65 views
0

我的Android應用程序出現問題。 我試圖以串行方式在我的Android設備和我的電腦之間進行通信。 即時通訊使用Android NDK。 只要我開始使用使用串口的方法,即時獲取錯誤消息。 .getSerialPort()或.getOutputStream()Android:無法啓動活動組件信息

當然,我已經試圖通過谷歌解決這個問題。 但實際上我沒有找到解決我的問題呢。 我檢查了我的清單是否包含所需的所有東西,而且我很確定它確實包含了。 希望你的某個人可以在我的代碼中找到問題。

這是我第一次問一個關於stackoverflow的問題,所以我想提示如何在未來以更好的方式提問。 如果有任何信息丟失,告訴我,我會盡快發佈。

這是我mainactivity:

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 
} 

}

Android清單:

<?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> 

錯誤消息:

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 2013-04-10 13:39:01

+0

儘量提高你的調試技巧,這些都是直截了當的錯誤。嘗試通過調試日誌並找出導致問題的線路。 – Triode 2013-04-10 13:46:10

相關問題