2013-04-10 186 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

相关问题