无法从类型Activity对静态方法findViewById(int)进行静态引用

时间:2013-11-18 12:52:46

标签: java android

我在我的(第一个)主要活动中有onclick。我从另一个(第二个)活动类调用方法pay()。我在这里调用这个方法,因为我在这个类方法中传递了它。

tvdel.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        TableRow km= (TableRow)findViewById(tr.getId());                
        second.pay(dist1[km.getId()], pan1[km.getId()], taxno1[km.getId()],"N");
        pay2(dist1[km.getId()], pan1[km.getId()], taxno1[km.getId()],"N");
    }
});

然后,如果我将文本设置为文本视图,我会收到此错误。

错误:无法从类型Activity

中对非静态方法findViewById(int)进行静态引用
TextView test = (TextView)findViewById(R.id.otaxno);
test.setText("try");

我尝试了很多解决方案,但在我的案例中没有任何帮助。请帮助。

我的主要活动课程:

public String property(String taxtype, final String userid) {

    final String NAMESPACE = "http://tempuri.org/";
    final String URL = "http://192.168.1.1/JavaService/Service.asmx";
    final String SOAP_ACTION = "http://tempuri.org/GetTaxTypeDetails";
    final String METHOD_NAME = "GetTaxTypeDetails";


    Typeface tfavv = Typeface.createFromAsset(getAssets(),"fonts/Avvaiyar.ttf");


    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    request.addProperty("sDistrict", dis);
    request.addProperty("sPanchayat", pan);
    request.addProperty("sTaxType", taxtype);
    request.addProperty("sTaxNo", taxno);
    request.addProperty("sTaxName", taxname);
    request.addProperty("sUserId", userid);

    DisAndPanDetails C = new DisAndPanDetails();
    PropertyInfo pi = new PropertyInfo();
    pi.setName("DisAndPanDetails");
    pi.setValue(C);
    pi.setType(C.getClass());
    request.addProperty(pi);
    SoapSerializationEnvelope envelope2 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope2.dotNet = true;
    envelope2.setOutputSoapObject(request);
    envelope2.addMapping(NAMESPACE, "DisAndPanDetails", new DisAndPanDetails().getClass());
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.debug = true;

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope2);
        SoapObject response2 = (SoapObject) envelope2.getResponse();

        Log.i("myApp", response2.toString());
        System.out.println("check dddddddddddddd" + response2);

        envelope2.addMapping(NAMESPACE, "Panchayat",new DisAndPanDetails().getClass());
        androidHttpTransport.call(SOAP_ACTION, envelope2);
        final DisAndPanDetails[] personList2 = new DisAndPanDetails[response2.getPropertyCount()];

        for (int j = 0; j < personList2.length; j++) {
            SoapObject so = (SoapObject) response2.getProperty(j);
            DisAndPanDetails person2 = new DisAndPanDetails();
            person2.TaxNo = so.getProperty(0).toString();
            person2.TaxName= so.getPropertyAsString(1).toString();
            person2.NoData=so.getPropertyAsString(6).toString();
            person2.District=so.getPropertyAsString(7).toString();
            person2.Panchayat=so.getPropertyAsString(8).toString();
            person2.TaxType=so.getPropertyAsString(9).toString();
            person2.TaxStatus=so.getPropertyAsString(10).toString();
            personList2[j] = person2;
        }


        final String[] taxno1 = new String[personList2.length];
        String[] taxname1 = new String[personList2.length];
         dist1 = new String[personList2.length];
        final String[] pan1 = new String[personList2.length];
        final String[] taxtype1 = new String[personList2.length];
        String[] status1 = new String[personList2.length];


        System.out.println("stringarray: " + taxno1);
        String Nod = null;

        for (int i = 0; i < personList2.length; i++) 
        {

            taxno1[i] = Arrays.asList(personList2[i].TaxNo).toString().replace("[", "").replace("]", "");
            taxname1[i] = Arrays.asList(personList2[i].TaxName).toString().replace("[", "").replace("]", "");
            dist1[i] = Arrays.asList(personList2[i].District).toString().replace("[", "").replace("]", "");
            pan1[i] = Arrays.asList(personList2[i].Panchayat).toString().replace("[", "").replace("]", "");
            taxtype1[i] = Arrays.asList(personList2[i].TaxType).toString().replace("[", "").replace("]", "");
            status1[i] = Arrays.asList(personList2[i].TaxStatus).toString().replace("[", "").replace("]", "");
            Nod = personList2[i].NoData.toString();

            System.out.println(taxno1[i]);
            System.out.println(taxname1[i]);
            System.out.println(dist1[i]);
            System.out.println(pan1[i]);
            System.out.println(taxtype1[i]);
            System.out.println(status1[i]);
            System.out.println("nod"+Nod);

        }






        TableLayout ll = (TableLayout) findViewById(R.id.tablelayout1);


        if ( Nod.equals("0"))
        {}
        else 
        {
       for (i = 0; i < personList2.length; i++)
            {
        final TableRow tr = new TableRow(this);

//       final TableRow tr = (TableRow)findViewById(R.id.tabrow2);
          LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

          tr.setId(i);


          tr.setLayoutParams(lp);
          lp.setMargins(10, -10, 15, 5);


          final TextView tvLeft = new TextView(this);
          tvLeft.setLayoutParams(lp);
          tvLeft.setGravity(Gravity.CENTER);
          tvLeft.setText(taxno1[i]);


          final TextView tvCenter = new TextView(this);
          tvCenter.setLayoutParams(lp);
          tvCenter.setTypeface(tfavv);
          tvCenter.setText(taxname1[i]);


          final TextView tvRight = new TextView(this);
          tvRight.setLayoutParams(lp);
          tvRight.setText(pan1[i]);


          final TextView tvend = new TextView(this);
          tvend.setLayoutParams(lp);
          tvend.setText(dist1[i]);


          final TextView tvdel = new TextView(this);
          tvdel.setLayoutParams(lp);
          tvdel.setTextSize(20);
          tvdel.setGravity(Gravity.CENTER);
          tvdel.setTextColor(Color.GREEN);
          tvdel.setText("Pay");



          tvdel.setOnClickListener(new View.OnClickListener() {

              @Override
              public void onClick(View v) {

                Intent login = new Intent(getApplicationContext(), PropertyTaxpay.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                login.putExtra("no",taxno1);
                startActivity(login);
                TableRow km= (TableRow)findViewById(tr.getId());                
                System.out.println(km);                   
                System.out.println("clickeddddddddddd  tr"+tr.getId());
                System.out.println("clickeddddddddddd  km "+km.getId());
                System.out.println("clickeddddddddddd  i"+i);
                PropertyTaxpay.pay(dist1[km.getId()], pan1[km.getId()], taxno1[km.getId()],"P");
            //  pay2(dist1[km.getId()], pan1[km.getId()], taxno1[km.getId()],"P");
              }
          }
     );

          tr.addView(tvLeft);
          tr.addView(tvCenter);
          tr.addView(tvRight);
          tr.addView(tvend);
          tr.addView(tvdel);

          ll.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        }












        for (int i = 0; i < personList2.length; i++) 
        {
            System.out.println("last TaxNo: " + personList2[i].TaxNo);
            System.out.println("last TaxName: " + personList2[i].TaxName);
            System.out.println("last District: "+ personList2[i].District);
            System.out.println("last Panchayat: "   + personList2[i].Panchayat);
            System.out.println("last TaxType: " + personList2[i].TaxType);
            System.out.println("last TaxStatus: "   + personList2[i].TaxStatus);
            System.out.println(" ");
            System.out.println(" ");
            System.out.println(" ");
            System.out.println(" ");
            System.out.println(" ");
        }
}
    }

        catch (SocketException ex) {
            Log.e("Error : ", "Error on soapPrimitiveData() " + ex.getMessage());
            ex.printStackTrace();
        } catch (Exception e) {
            Log.e("Error : ", "Error soapPrimitiveData() " + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

在第二个活动类中:

protected static String pay(String dist, String pan,String taxno, String taxtype ) {

    final String NAMESPACE = "http://tempuri.org/";
    final String URL = "http://192.168.1.1/JavaService/Service.asmx";
    final String SOAP_ACTION = "http://tempuri.org/GetTaxDetails";
    final String METHOD_NAME = "GetTaxDetails";

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    request.addProperty("sDistrict", dist);
    request.addProperty("sPanchayat", pan);
    request.addProperty("sTaxType", taxtype);
    request.addProperty("sTaxNo", taxno);


    DisAndPanDetails D = new DisAndPanDetails();
    PropertyInfo pi = new PropertyInfo();
    pi.setName("DisAndPanDetails");
    pi.setValue(D);
    pi.setType(D.getClass());
    request.addProperty(pi);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;

    envelope.setOutputSoapObject(request);
    System.out.println(request);
    envelope.addMapping(NAMESPACE, "DisAndPanDetails",  new DisAndPanDetails().getClass());
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.debug = true;

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject response2 = (SoapObject) envelope.getResponse();

        Log.i("myApp", response2.toString());
        System.out.println("clicvvvvvvvv dddddddddddddd" + response2);
        String resultData = response2.getProperty(0).toString(); 

        System.out.println("clicvvvvvvvv resultData" + resultData);

         DisAndPanDetails[] personList2 = new DisAndPanDetails[response2.getPropertyCount()];


        for (int j = 0; j < personList2.length; j++) {
            DisAndPanDetails person2 = new DisAndPanDetails();
            person2.TaxName= response2.getProperty(0).toString();
            person2.TaxDoorNo=response2.getProperty(1).toString();
            person2.TaxStreetName=response2.getPropertyAsString(2).toString();
            person2.TaxWardNo=response2.getPropertyAsString(3).toString();
            person2.Error=response2.getPropertyAsString(4).toString();
            person2.MobileNo=response2.getPropertyAsString(5).toString();
            person2.NoData=response2.getPropertyAsString(6).toString();
            person2.NonTaxRights=response2.getPropertyAsString(7).toString();
            personList2[j] = person2;
        }

        String TaxName1 ;
        String TaxDoorNo1 ;
        String TaxStreetName1 ;
        String TaxWardNo1 ;
        String NonTaxRights1 ;
        String NoData1;
        String Error1;
        String MobileNo1;

        int i=0;
            TaxName1 = personList2[i].TaxName.toString();
            TaxDoorNo1 = personList2[i].TaxDoorNo.toString();
            TaxStreetName1 = personList2[i].TaxStreetName.toString();
            TaxWardNo1 = personList2[i].TaxWardNo.toString();
            NonTaxRights1 = personList2[i].NonTaxRights.toString();
            NoData1 = personList2[i].NoData.toString();
            Error1 = personList2[i].Error.toString();
            MobileNo1 = personList2[i].MobileNo.toString();

            System.out.println("TaxName1"+TaxName1);
            System.out.println("TaxDoorNo1"+TaxDoorNo1);
            System.out.println("TaxStreetName1"+TaxStreetName1);
            System.out.println("TaxWardNo1"+TaxWardNo1);
            System.out.println("NonTaxRights1"+NonTaxRights1);
            System.out.println("NoData1"+NoData1);
            System.out.println("Error1"+Error1);
            System.out.println("MobileNo1"+MobileNo1);


        /*  LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.payment, null);
            TextView test = (TextView)v.findViewById(R.id.otaxno);
            test.setText("try");


            */


    TextView test = (TextView)getActivity().findViewById(R.id.otaxno);
            test.setText("try");





    }
    catch (SocketException ex) {
        Log.e("Error : ", "Error on soapPrimitiveData() " + ex.getMessage());
        ex.printStackTrace();
    } catch (Exception e) {
        Log.e("Error : ", "Error  soapPrimitiveData() " + e.getMessage());
        e.printStackTrace();
    }

    return null;

}

1 个答案:

答案 0 :(得分:0)

尝试在类级别声明文本视图,并在onCreate()中将其初始化为

  public class PropertyTax extends Activity{
       private static TextView test=null;
       @Override
       public void onCreate(Bundle savedInstanceState) {
        //CODE
        test=(TextView)findViewById(R.id.otaxno);
        ...
       }
    }

并将静态方法pay()内的文本设置为

test.setText("try");