RuntimeException:无法启动活动ComponentInfo:java.lang.NullPointerException

时间:2012-12-28 18:23:03

标签: android mysql web-services axis2 listview-selector

您好我正在尝试使用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在listviews中显示来自mysql的数据的代码,请在这里提供.. 对不起我的英语不好.. 提前谢谢..

1 个答案:

答案 0 :(得分:0)

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

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

此处,您不会检查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()将返回null ...