点击按钮无法将数据发送到服务器?

时间:2014-05-01 08:43:10

标签: android android-fragments

public class PubHer extends Fragment{

EditText eCont;
Spinner spinnerareas3, spinnerareas4;
Button publish, switchArea;
static Context con;
String ite, cont, sno=""+12;
private Context globalContext = null;
public int item1, item2;

public static void fun(Context c) {
    con = c;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_pubher, container, false);
    spinnerareas3 = (Spinner) rootView.findViewById(R.id.spinnerplan1);
    // ListView list = (ListView) rootView.getl

     spinnerareas4 = (Spinner) rootView.findViewById(R.id.spinnerplan2);
    // spinnerareas3.setOnItemSelectedListener();
     spinnerareas3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {                

                 item1 = position;
                 ite =  ""+parent.getSelectedItemPosition();
                 CharSequence charS = item1+"";
                 Toast.makeText(con ,charS , Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        }); 

     spinnerareas4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {                

                 item2 = position;
                 CharSequence charS = item2+"";
                 Toast.makeText(con ,charS , Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        }); 

     publish = (Button) rootView.findViewById(R.id.bPublish);
     publish.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              cont = eCont.getText().toString();
              upload();
          }
     });
     switchArea = (Button) rootView.findViewById(R.id.bJumper);
     switchArea.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            updateDetail();
          }
        private void updateDetail() {
            // TODO Auto-generated method stub
            Intent i = new Intent("com.example.notifier.JumpList");
            startActivity(i);

        }
     });

    return rootView;
}

protected void upload() {
    // TODO Auto-generated method stub
    if(item2==0)
        new Sync_upload_event_info(this.getActivity(), 0).execute(sno,ite,cont);
    else if(item2==1)
        new Sync_upload_adver_info(this.getActivity(),0).execute(sno,ite,cont);
    else
        new Sync_upload_emer_info(this.getActivity(),0).execute(sno,ite,cont);

}

}

这是我的服务器同步文件之一     公共类Sync_upload_event_info扩展了AsyncTask {

private Context context;
String sno, place_id, event_text;

public Sync_upload_event_info(Context context,int flag) 
{   
    context=this.context; 
    int getorpost=flag;
} 

protected void onPreExecute(){ }
@Override
protected String doInBackground(String... arg0) 
{ 

        try{ 
            sno     = (String)arg0[0]; 
            place_id  = (String)arg0[1];
            event_text  = (String)arg0[2];
            String link = "http://notifiermmn.site50.net/upload_event_info.php?sno="+sno+"&place_id="+place_id+"&event_text="+event_text;

            link=link.replaceAll(" ", "%20");

            System.out.println("LINK:"+link);
            //URL url = new URL(link); 
            HttpClient client = new DefaultHttpClient(); 
            HttpGet request = new HttpGet(); 
            //request.setURI(new URI(link)); 
            HttpResponse response = client.execute(new HttpGet(link)); 
            return "";
            }
        catch(Exception e)
        { 
            return new String("Exception: " + e.getMessage()); 
        }
        }
 protected void onPostExecute(String result){

   }

}

一旦我按下发布按钮并将微调器4设置为事件,即Sync_upload_event_info或其他两个我的应用程序崩溃。 logcat还指示发布按钮单击处理但无法解析

1 个答案:

答案 0 :(得分:1)

我认为您忘记创建eCont EditText的引用,如:

 EditText eCont= (EditText)rootView.findViewById(R.id.cont);

您的应用程序在您的发布按钮Click事件中崩溃eContNULL