0

您好我正在尝试使用 web 服务axis2将mysql中的数据显示到android listview中。当我运行我的应用程序时,会出现以下异常。这是 Logcat 的详细信息

12-28 21:20:45.180: E/AndroidRuntime(406): FATAL EXCEPTION: main
12-28 21:20:45.180: E/AndroidRuntime(406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kbj/com.kbj.Invitation}: java.lang.NullPointerException
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.os.Looper.loop(Looper.java:123)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-28 21:20:45.180: E/AndroidRuntime(406):  at java.lang.reflect.Method.invokeNative(Native Method)
12-28 21:20:45.180: E/AndroidRuntime(406):  at java.lang.reflect.Method.invoke(Method.java:521)
12-28 21:20:45.180: E/AndroidRuntime(406):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-28 21:20:45.180: E/AndroidRuntime(406):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-28 21:20:45.180: E/AndroidRuntime(406):  at dalvik.system.NativeStart.main(Native Method)
12-28 21:20:45.180: E/AndroidRuntime(406): Caused by: java.lang.NullPointerException
12-28 21:20:45.180: E/AndroidRuntime(406):  at com.kbj.Invitation.onCreate(Invitation.java:49)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-28 21:20:45.180: E/AndroidRuntime(406):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-28 21:20:45.180: E/AndroidRuntime(406):  ... 11 more
12-28 21:20:50.690: I/Process(406): Sending signal. PID: 406 SIG: 9

这是我的功能

1、Web Services类中的retrieveData

public ArrayList<InvitePlayerDetails> retrievePlayer(String userName){

    Connection conn=null;
    PreparedStatement stmt=null;

      ResultSet result = null; 
      String retrievedUserName = "";
      //Boolean b = false;
     // String retrievedPassword = "";
      ArrayList<InvitePlayerDetails> playersList = new ArrayList<InvitePlayerDetails>();

    try{


       conn = Util.getConnection();
       stmt =  conn.prepareStatement("SELECT kbj_username FROM kbj_registration");
       result = stmt.executeQuery();
       System.out.println(result);

       while(result.next()){
        retrievedUserName = result.getString("kbj_username");
        System.out.println(retrievedUserName);
        InvitePlayerDetails p1 = new InvitePlayerDetails(retrievedUserName, false);
       // ArrayList<InvitePlayerDetails> playersList = new ArrayList<InvitePlayerDetails>(retrievedUserName, b);
        playersList.add(p1);

        System.out.println("PlayerListAdded" + playersList);
    }


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

      finally
        {           
        Util.CloseDbResources(result, stmt, conn);

        }
return playersList;
}
}

这是我的android活动功能..

公共类邀请扩展活动{

private final String NAMESPACE = "http://ws.kbj.com";
private final String URL = "http://10.0.2.2:8085/WS_KBJ/services/Kbj_WebService?wsdl";
private final String SOAP_ACTION = "http://ws.kbj.com/retrievePlayer";
private final String METHOD_NAME = "retrievePlayer";


@Override
protected void onCreate(Bundle savedInstanceState) {


    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.invitation);

    final TextView userName = (TextView) findViewById(R.id.CurPlayername);
    userName.setText(getIntent().getExtras().getString("UserName"));

    final Button fbBtn = (Button) findViewById(R.id.fbButton);
    final Button inviteBtn = (Button) findViewById(R.id.inviteBtn);
    ListView listView = (ListView) findViewById(R.id.invitePlayerList);  
    ArrayList<InvitePlayerDetails> playersList = new ArrayList<InvitePlayerDetails>();  


    playersList = retriveData();


    InvitePlayersBaseAdapter adapter = new InvitePlayersBaseAdapter(this, playersList);  
    listView.setAdapter(adapter);  
    listView.setOnItemClickListener(new OnItemClickListener() {  
        public void onItemClick(AdapterView<?> arg0, View v, int position,  
                long id) {  

        }  
    });  
    final Intent openFbOrTwLoginPage = new Intent(this, FbOrTwLogin.class);
    final Intent openNotificationPage = new Intent(this, Notification.class);
    fbBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(openFbOrTwLoginPage);

        }
    });
    twBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(openFbOrTwLoginPage);

        }
    });
    inviteBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(openNotificationPage);

        }
}

public ArrayList<InvitePlayerDetails> retriveData(){
ArrayList<InvitePlayerDetails> playersList = new ArrayList<InvitePlayerDetails>();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo unameProp =new PropertyInfo();
    unameProp.setName("userName");//Define the variable name in the web service method
    unameProp.setValue(title);//set value for userName variable
    unameProp.setType(String.class);//Define the type of the variable
    request.addProperty(unameProp);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try{
        androidHttpTransport.call(SOAP_ACTION, envelope);
           SoapPrimitive response = (SoapPrimitive)envelope.getResponse();


           System.out.println("SoapPriitive:"+response);
           //status1 = Boolean.parseBoolean(response.toString());

    }
    catch(Exception e){

    }


    return playersList;
}

}

我创建了一个 pojo 类,它是

public class InvitePlayerDetails {


private String invitedplayerName;
private boolean ckBox;
  public InvitePlayerDetails(String invitedplayerName,boolean cb1) {  
        this.invitedplayerName = invitedplayerName;  
        this.ckBox = cb1;  
    }  

public String getInvitedplayerName() {
    return invitedplayerName;
}
public void setInvitedplayerName(String invitedplayerName) {
    this.invitedplayerName = invitedplayerName;
}
public boolean getCkBox() {
    return ckBox;
}
public void setCkBox(boolean b) {
    this.ckBox = b;
}

}

请帮我解决这个问题..告诉我我的代码哪里出了问题..如果有人有关于使用axis2在列表视图中显示来自mysql的数据的代码,请在此处提供..对不起我的英语..提前致谢..

4

1 回答 1

0

你没有指出哪一行是 49,但我有几个猜测:

userName.setText(getIntent().getExtras().getString("UserName"));

在这里,您不检查 if getIntent()getExtras()甚至getString("UserName")返回 null。在尝试使用它之前,您应该确保每个方法都返回有效数据。

我也没有twBtn在其他任何地方看到:

twBtn.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        startActivity(openFbOrTwLoginPage);

    }
});

它没有初始化,甚至没有定义......

如果这些都不在第 49 行,请仔细检查您的布局invitation.xml以确保您搜索的每个findViewById()视图都在此布局中。否则findViewById()会返回空...

于 2012-12-28T18:55:17.363 回答