Java 启动新活动时应用程序崩溃

Java 启动新活动时应用程序崩溃,java,android,android-intent,android-fragments,android-bluetooth,Java,Android,Android Intent,Android Fragments,Android Bluetooth,我正在开发一个带有幻灯片片段的应用程序。一切正常,应用程序启动没有问题。问题发生在View.onClickListener中。以下是我的单个片段部分的代码: public static class ReceiveSectionFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInst

我正在开发一个带有幻灯片片段的应用程序。一切正常,应用程序启动没有问题。问题发生在View.onClickListener中。以下是我的单个片段部分的代码:

 public static class ReceiveSectionFragment extends Fragment {

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
     View rootView = inflater.inflate(R.layout.receive, container, false);
         rootView.findViewById(R.id.bt_server_start)
         .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
         Intent intent = new Intent(getActivity(), Receive.class);
         startActivity(intent);
                }
            });

        return rootView;
        }
    }
滑动到目标布局-
R.layout.receive
效果良好。所以这里没问题。在
R.id.bt\u服务器\u启动
按钮上,单击下面的类启动:

package com.receive.bluereceive;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

public class Receive extends Activity {

/**
 * Default Serial-Port UUID
 */
private String myUUID = "00001101-0000-1000-8000-00805F9B34FB";

/**
 * Default Bluetooth adapter on the device.
 */
private final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

/**
 * Magic number used in the Bluetooth enabling request.
 */
public final int REQUEST_ENABLE_BT = 2;

/**
 * The Server thread.
 */
private AcceptThread DeviceServer;

private NotificationCenter mNotificationCenter;

public final static String EXTRA_MESSAGE = "com.receive.intent.MESSAGE";

public final int BT_ENABLE_TIME = 35;

public final int BT_TIME_BTTIME = 1000 * BT_ENABLE_TIME;

public CountDownTimer BTCountDown;

public CountDownTimer MoveHistoryCountDown;

@Override
protected void onStart() {
    super.onStart();
    mNotificationCenter = new NotificationCenter();
    /*
     * Code responsible for calling method NotificationCenter() to print the incoming text to TextView field
     */
    registerReceiver(mNotificationCenter, new IntentFilter(EXTRA_MESSAGE));     
    /*
    * Start server on device
    */
    ServerThread();
    ((Button) findViewById(R.id.bt_server_start)).setEnabled(false);
    ((Button) findViewById(R.id.bt_server_stop)).setEnabled(true);
}

public void ServerThread() {
    DeviceServer = new AcceptThread();
    DeviceServer.start();
}

private class AcceptThread extends Thread {
    /*
     * That TAG will appear in the log in Eclipse
     */
    private final String ACC_TAG = AcceptThread.class.getName();
    /*
     * Bluetooth server socket
     */
    private final BluetoothServerSocket mServerSocket;

    public AcceptThread() {
        /*
         * Use a temporary object that is later assigned to mServerSocket, because mServerSocket is final
         */
        BluetoothServerSocket tmp = null;
        try {
            /*
             * MY_UUID is the app's UUID string, also used by the client code
             */
            tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(ACC_TAG, UUID.fromString(myUUID));
        } catch (IOException e) { }
        mServerSocket = tmp;
    }

    public void run() {
        BluetoothSocket socket = null;

        /*
         * Keep listening until exception occurs or a socket is returned
         */
        while (true) {
            try {
                Log.i(ACC_TAG,"Listening for a connection nearby...");
                socket = mServerSocket.accept();
                Log.i(ACC_TAG, "Connected to " + socket.getRemoteDevice().getName());
            } catch (IOException e) {
                break;
            }
            /*
             * If connection was accepted then proceed with code below
             * If Bluetooth socket does not equal null proceed with string reading as know script knows that device is connected
             * And if it is first ServerThread start, 
             */
            if (socket != null) {

                try {
                    String message;
                    DataInputStream incoming = new DataInputStream(socket.getInputStream());
                    message = incoming.readUTF();

                    Intent actual = new Intent(EXTRA_MESSAGE);
                    actual.putExtra("Message", String.format("%s",message));
                    getBaseContext().sendBroadcast(actual);


                } catch (IOException e) {
                    Log.e(ACC_TAG, "Error obtaining InputStream from socket");
                    e.printStackTrace();
                }
                try {
                    mServerSocket.close();
                } catch (IOException e) { }
                break;
            }
        }
    }

    /*
     * Will cancel the listening socket, and cause the thread to finish 
    */
    public void cancel() {
        try {
            mServerSocket.close();
        } catch (IOException e) { }
    }
}

private Vibrator getVibrator() {
    return (Vibrator) getSystemService(VIBRATOR_SERVICE);
}

/*
 * NOTIFICATION CLASS, PRINTS THE RECEIVED MESSAGE
 */
public class NotificationCenter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(EXTRA_MESSAGE)) {

            int counter = 0;
            ArrayList<String> historiaAL = new ArrayList<String>();
            historiaAL.add(intent.getExtras().getString("Message"));

            TextView actual = (TextView)findViewById(R.id.received_string);
            actual.setText(historiaAL.get(counter));

            TextView history = (TextView)findViewById(R.id.history_string);             
            String[] historiaA = historiaAL.toArray(new String[historiaAL.size()]);

            for(int i = 0; i < historiaAL.size(); i++)
            {
                history.append(historiaA[i]);
                history.append(" \n ");
            }


            getVibrator().vibrate(500);
            MoveHistoryCountDown = new HistoryMove(5000,5);
            MoveHistoryCountDown.start();
            counter++;
        } 
    }

}
public class HistoryMove extends CountDownTimer {
      public HistoryMove (long startTime, long interval) {
       super(startTime, interval);
      }

      @Override
      public void onFinish() {
          TextView recent = (TextView)findViewById(R.id.received_string);
          recent.setText(" ");
      }

      @Override
      public void onTick(long millisUntilFinished) {

      }
}
}
也许这是一些新手犯的错误,但是,这已经是一个小时了,我仍然无法破解

请不要降低声誉,我真的不能让它工作,即使它可能看起来微不足道


编辑:完整的主代码-

我没有看到在活动中的任何地方调用
setContentView
,这意味着
findViewById
返回空值

onCreate
方法添加到活动中,并调用
setContentView
设置布局

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);
}

Receive.java
中的第82行是哪一行?@Liseł我不确定;请登录您的编辑器。特别是,您发布的内容缺少导入。上次更新:
((按钮)findViewById(R.id.bt_server_start)).setEnabled(false)
这会抛出@mav提到的错误如果我添加
onCreate
方法,新页面就会出现,它只是覆盖了我的ActionBarI片段,我不确定你所说的“覆盖”是什么意思。如果不设置内容视图,则无法在活动中使用任何视觉元素…通过覆盖,我的意思是,如果使用onCreate方法,则我的带有导航选项卡的视图将被
R.layout.receive
覆盖。启动了完整的新布局,没有导航选项卡。但是您所指的按钮(
R.id.bt\u server\u start
和其他按钮)位于
R.layout.receive
,对吗?您必须将它们移动到一个片段中,或者更改活动/片段的布局以使其全部工作。
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);
}