Android onitemclick空指针异常?

Android onitemclick空指针异常?,android,nullpointerexception,Android,Nullpointerexception,我有一个通过“加载学生列表”按钮加载的列表:但是当我点击列表中的某个项目而不是进入下一个活动时,它会崩溃:NULL Pointer EXECOPTION:我将stID(学生Id)绑定为额外的。McClick给了我正确的学生id,它怎么可能是空的,我想传递给intent的上下文可能有问题;但这只是猜测;你们帮我把那个虫子钉住 package com.example.connection; import java.io.BufferedReader; import java.io.IOExcepti

我有一个通过“加载学生列表”按钮加载的列表:但是当我点击列表中的某个项目而不是进入下一个活动时,它会崩溃:NULL Pointer EXECOPTION:我将stID(学生Id)绑定为额外的。McClick给了我正确的学生id,它怎么可能是空的,我想传递给intent的上下文可能有问题;但这只是猜测;你们帮我把那个虫子钉住

package com.example.connection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;




import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import android.view.View;
import android.view.View.OnClickListener;

public class SelectStudent extends Activity {
private BluetoothAdapter btAdapter = null;
  private BluetoothSocket btSocket = null;
  private OutputStream outStream = null;

  // Well known SPP UUID
  private static final UUID MY_UUID =
      UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

  // Insert your server's MAC address
  private static String address =  "01:36:05:23:4A:44";
  public static String grpid;
    Context obj;
  Context objx;
  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    objx=this;
      super.onCreate(savedInstanceState);
    setContentView(R.layout.select_student);
    final ListView listview = (ListView) findViewById(R.id.stdlist);
    final ArrayList<String> list = new ArrayList<String>();
     Button btnLogin= (Button) findViewById(R.id.LoadList);
    Button btnClose =(Button) findViewById(R.id.Exit);
    btnClose.setText("Close");

    Button insert = (Button) findViewById(R.id.add_btn);




 insert.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                Intent intent = new Intent(SelectStudent.this, InsertMarks.class);
                startActivity(intent);

            }
        });

        btAdapter = BluetoothAdapter.getDefaultAdapter();
        btnClose.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            System.exit(0);
            }
        });
        btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            try
            {
                // Set up a pointer to the remote node (Machine/PC) using it's address.
                BluetoothDevice device = btAdapter.getRemoteDevice(address);

                // -- Notes -- //
                // Two things are needed to make a connection:
                // A MAC address, which we got above.
                // A Service ID or UUID.

             // UUID for SPP.
                try {
                  btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
                } catch (IOException e) {
                  //AlertBox("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
                }

                // During message pass no devise discovery is allowed.
                btAdapter.cancelDiscovery();

                // Establish the connection.  This will block until it connects.
                try {
                  btSocket.connect();
                  //out.append("\n...Connection established and data link opened...");
                } catch (IOException e) {
                  try {
                    btSocket.close();
                  } catch (IOException e2) {
                    //AlertBox("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
                  }
                }

                // Create a data stream so we can talk to server.
                //out.append("\n...Sending message to server...");
                String message = "2\n";
                //out.append("\n\n...The message that we will send to the server is: "+message);

                try {
                  outStream = btSocket.getOutputStream();
                } catch (IOException e) {
                  //AlertBox("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
                }

                byte[] msgBuffer = message.getBytes();
                try {
                  outStream.write(msgBuffer);
                } catch (IOException e) {
                  String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
                  if (address.equals("00:00:00:00:00:00")) 
                    msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";
                  msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";

                  //AlertBox("Fatal Error", msg);       
                }
                InputStream inStream;
                try {
                    inStream = btSocket.getInputStream();
                    BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
                    String lineRead=bReader.readLine();
                    Toast.makeText(objx, lineRead, Toast.LENGTH_SHORT).show();
                            bReader.close();
                            inStream.close();
                            do
                            {
                                try
                                {
                                    String id,temp;
                                    int length;
                                    id=lineRead.substring(0, lineRead.indexOf("*"));
                                    length=lineRead.indexOf("*");
                                    temp=lineRead.substring(length+1);
                                    lineRead=temp;
                                    if(!id.equals(""))
                                         list.add(id);

                                }
                                catch(Exception exp)
                                {
                                    Toast.makeText(getApplication(), exp.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                                final StableArrayAdapter adapter = new StableArrayAdapter(objx,
                                        android.R.layout.simple_list_item_1, list);
                                    listview.setAdapter(adapter);
                            }
                            while(lineRead.contains("*"));
                            //finish();

                } catch (IOException e) {
                    e.printStackTrace();
                }

                //out.append("\n...In onPause()...");



                if (outStream != null) {
                  try {
                    outStream.flush();
                  } catch (IOException e) {
                   // AlertBox("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
                  }
                }

                try     {
                  btSocket.close();
                } catch (IOException e2) {
                 // AlertBox("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
                }

                //bReader.close();
                //inStream.close();

            }
            catch(Exception exp)
            {
                Toast.makeText(getApplication(), "Error Found!"+exp.getMessage(), Toast.LENGTH_SHORT).show();
            }

            }

        });





        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
              public void onItemClick(AdapterView<?> parent, final View view,
                  int position, long id) {
               try
               {
                   final String item = (String) parent.getItemAtPosition(position);

                    //InsertMarks.stdid=position;

                    Intent i= new Intent(SelectStudent.this,InsertMarks.class);
                    i.putExtra("stID", item);
                    startActivity(i);
                    //Toast.makeText(getApplication(), "Working"+item, Toast.LENGTH_SHORT).show();

                    //String app = SelectStudent.this.toString();

                    //Toast.makeText(getApplication(), "Current Context: "+app, Toast.LENGTH_LONG).show();

                    //next();
               }
               catch(Exception exp)
               {
                   //Toast.makeText(objx, exp.getMessage(), Toast.LENGTH_LONG).show();
               }
              };


            });
          }

          private class StableArrayAdapter extends ArrayAdapter<String> {

            HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

            public StableArrayAdapter(Context context, int textViewResourceId,
                List<String> objects) {
              super(context, textViewResourceId, objects);
              for (int i = 0; i < objects.size(); ++i) {
                mIdMap.put(objects.get(i), i);
              }
            }

            @Override
            public long getItemId(int position) {
              String item = getItem(position);
              return mIdMap.get(item);
            }

            @Override
            public boolean hasStableIds() {
              return true;
            }

          }
      }   

查看您的LogCat,这表示在
InsertMarks
活动中存在
NullPointerException
,我猜您无法恢复从
SelectStudent
传递到
InsertMarks
的值

您应该这样做(在
InsertMarks
中):


但是我不能确定,因为您没有发布
InsertMarks
代码。

InsertMarks
活动
上的第48行是什么?请摆脱该系统。退出(0)InsertMarks活动中存在
NullPointerException
。。。请在第48行检查InsertMask活动的第48行在哪里?我将如何将studentID传递给InsertMarks活动?谢谢MikekekeepsonShine:继续
  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="15dp"
        android:text="@string/SelectStudent"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/stdlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/Exit"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignRight="@+id/Exit"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp"
        android:scrollbarAlwaysDrawVerticalTrack="true" >

    </ListView>

    <Button
        android:id="@+id/LoadList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Exit"
        android:layout_alignBottom="@+id/Exit"
        android:layout_alignParentLeft="true"
        android:text="@string/LoadList" />

    <Button
        android:id="@+id/Exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="17dp"
        android:layout_toRightOf="@+id/LoadList"
        android:text="@string/Next" />

    <Button
        android:id="@+id/add_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/stdlist"
        android:layout_toRightOf="@+id/stdlist"
        android:text="@string/add" />

        </RelativeLayout>
      01-07 19:28:28.888: E/AndroidRuntime(4354): FATAL EXCEPTION: main
      01-07 19:28:28.888: E/AndroidRuntime(4354): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.connection/com.example.connection.InsertMarks}: java.lang.NullPointerException
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2084)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2111)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at android.app.ActivityThread.access$600(ActivityThread.java:134)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at android.os.Handler.dispatchMessage(Handler.java:99)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at android.os.Looper.loop(Looper.java:137)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at android.app.ActivityThread.main(ActivityThread.java:4666)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at java.lang.reflect.Method.invokeNative(Native Method)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at java.lang.reflect.Method.invoke(Method.java:511)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at dalvik.system.NativeStart.main(Native Method)
      01-07 19:28:28.888: E/AndroidRuntime(4354): Caused by: java.lang.NullPointerException
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at com.example.connection.InsertMarks.onCreate(InsertMarks.java:48)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at android.app.Activity.performCreate(Activity.java:4510)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2048)
      01-07 19:28:28.888: E/AndroidRuntime(4354):   ... 11 more
String yourStudent = getIntent().getStringExtra("stID");