我正在开发一个通过WiFi 直接执行对讲机功能的应用程序。我创建了使用媒体记录器线程类记录音频并使用 RTP 套接字( Sendersocket 类)将其发送到客户端的walkietalkiefunction活动。我尝试从片段开始此活动。我尝试了很多次,我发现了错误(运行时异常:无法实例化活动 ComponentInfo{com.walkietakli2.android.mcomp/com.walkietakli2.android.mcomp.WalkieTalkieFunction}:java.lang.NullPointerException)。所以请帮助我?我解决不了。你知道如何解决这个问题吗?
03-09 08:40:09.361: E/AndroidRuntime(1301): FATAL EXCEPTION: main
03-09 08:40:09.361: E/AndroidRuntime(1301): java.lang.RuntimeException: Unable to instantiate activity componentInfo{com.walkietakli2.android.mcomp/com.walkietakli2.android.mcomp.WalkieTalkieFunction}: java.lang.NullPointerException
03-09 08:40:09.361: E/AndroidRuntime(1301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-09 08:40:09.361: E/AndroidRuntime(1301): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-09 08:40:09.361: E/AndroidRuntime(1301): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-09 08:40:09.361: E/AndroidRuntime(1301): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-09 08:40:09.361: E/AndroidRuntime(1301): at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 08:40:09.361: E/AndroidRuntime(1301): at android.os.Looper.loop(Looper.java:137)
03-09 08:40:09.361: E/AndroidRuntime(1301): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-09 08:40:09.361: E/AndroidRuntime(1301): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 08:40:09.361: E/AndroidRuntime(1301): at java.lang.reflect.Method.invoke(Method.java:511)
03-09 08:40:09.361: E/AndroidRuntime(1301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-09 08:40:09.361: E/AndroidRuntime(1301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-09 08:40:09.361: E/AndroidRuntime(1301): at dalvik.system.NativeStart.main(Native Method)
03-09 08:40:09.361: E/AndroidRuntime(1301): Caused by: java.lang.NullPointerException
03-09 08:40:09.361: E/AndroidRuntime(1301): at com.walkietakli2.android.mcomp.WalkieTalkieFunction.<init>(WalkieTalkieFunction.java:26)
03-09 08:40:09.361: E/AndroidRuntime(1301): at java.lang.Class.newInstanceImpl(Native Method)
03-09 08:40:09.361: E/AndroidRuntime(1301): at java.lang.Class.newInstance(Class.java:1319)
03-09 08:40:09.361: E/AndroidRuntime(1301): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-09 08:40:09.361: E/AndroidRuntime(1301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-09 08:40:09.361: E/AndroidRuntime(1301): ... 11 more
这是代码:包com.walkietakli2.android.mcomp;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class WalkieTalkieFunction extends Activity {
private WifiP2pDevice device =null;
private WifiP2pInfo info =null;
private FreqManager frqman =new FreqManager();
private DeviceListFragment dlf=null;
//Context context =null;
//Object sstmonitor =new Object();
Object mrtmonitor=new Object();
String host = info.groupOwnerAddress.getHostAddress();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_walkie_talkie_function);
ImageButton recorder1=(ImageButton)findViewById(R.id.recorderbtn);
recorder1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SocketSenderThread sst =new SocketSenderThread(8988,device.deviceAddress,
sstmonitor);
EditText edtxt1= (EditText) findViewById (R.id.wtflbl1);
if(frqman.isInSameGroup
(dlf.getSelectedItemPosition()))
{
MediaRecorderThread mrt =new MediaRecorderThread(host,
mrtmonitor);
mrt.run();
edtxt1.setText("now recording");
// sst.init();
//sst.run();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.
activity_walkie_talkie_function, menu);
return true;
}
}
enter code here
package com.walkietakli2.android.mcomp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.os.Environment;
import android.util.Log;
public class SocketSenderThread implements Runnable{
public SocketSenderThread(int dport,
String daddr, Object ssMonitor) {
super();
this.daddr = daddr;
this.dport = dport;
ss_monitor = ssMonitor;
init();
}
private void init()
{
DatagramSocket src_socket = null;
try {
src_socket = new DatagramSocket();
Log.i(TAG, "init: dest addr"+InetAddress.getByName(daddr)+" port: "+dport);
rtp_socket = new RtpSocket(src_socket, InetAddress.getByName(daddr),dport);
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (SocketException e) {
Log.i(TAG, "Socket cannot be opened");
e.printStackTrace();
}
}
private InputStream audio_input;
private String daddr;
private int dport;
private static final String TAG = "WalkieTalkieEngine";
public static boolean all_files_sent = true;
RtpSocket rtp_socket = null;
Object ss_monitor = new Object();
@Override
public void run() {
int sample_rate = 8000;
int sample_size = 1;
int frame_size = Constants.PKT_SIZE;
int payload_type = 0;
boolean running = true;
int last_file_sent = 0;
int num_files = 0;
int frame_rate = sample_rate/(frame_size/sample_size);
while(running)
{
if(all_files_sent)
{
synchronized(ss_monitor)
{
try{
Log.i(TAG, "Waiting for notif");
ss_monitor.wait();
}
catch(InterruptedException e){}
num_files = MediaRecorderThread.max_file_num;
}
Log.i(TAG, "Got Notif");
}
// create # of streams and set that to audio_input
for (int i = last_file_sent; i < num_files; i++)
{
String path = null;
try {
Integer file_num = new Integer(i+1);
path =Environment.getExternalStorageState().
toString()+file_num.toString()+".3gp";
audio_input = new FileInputStream(path);
payload_type = i+1;
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
if (audio_input==null)
return;
Log.e(TAG, "Trying to to send RTP packet from File"+path);
byte[] buffer=new byte[frame_size+12];
RtpPacket rtp_packet=new RtpPacket(buffer,0);
rtp_packet.setPayloadType(payload_type);
int seqn=0;
long time=0;
long byte_rate=frame_rate*frame_size;
boolean reading=true;
Log.i(TAG, "Reading blocks of "+(buffer.length-12)+" bytes");
while(reading)
{
int num=0;
try {
num = audio_input.read(buffer,12,buffer.length-12);
} catch (IOException e1) {
e1.printStackTrace();
}
Log.i(TAG, "Number of blocks read: "+num);
if(num > 0)
{
rtp_packet.setSequenceNumber(seqn++);
rtp_packet.setTimestamp(time);
rtp_packet.setPayloadLength(num);
try {
rtp_socket.send(rtp_packet);
Log.i(TAG, "Sent packet of size: "+rtp_packet.getLength());
} catch (IOException e) {
Log.i(TAG, "Send failure");
e.printStackTrace();
}
long frame_time=(num*1000)/byte_rate;
time+=frame_time;
try { Thread.sleep(frame_time); } catch (Exception e) {}
}
else
{
reading=false;
Log.i(TAG, "Error reading from InputStream");
}
}//end while reading
File fileToDelete =new File(path);
fileToDelete.delete();
last_file_sent++;
} // end-for
all_files_sent = true;
} // end-while running
}
}