2015-12-29 35 views
0

我是新來的android開發。我正嘗試創建一個應用程序來掃描QR碼字符串並將其發佈到Web服務器中。我在錯誤中獲取警告在'MainActivity'中的方法'post'具有不正確的簽名,並且同時單擊發布按鈕時出現錯誤java.lang.IllegalStateException:無法在父或祖先的Context中找到方法發佈(視圖) :定義了onClick屬性。http post不工作​​在android應用程序

如何解決方法中不正確的簽名問題?

如何在方法中傳遞兩個變量?

public class MainActivity extends AppCompatActivity { 

static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN"; 
public String contents; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //set the main content layout of the Activity 
    setContentView(R.layout.activity_main); 
} 

//product qr code mode 
public void Scan(View v) { 
    try { 
     //start the scanning activity from the  com.google.zxing.client.android.SCAN intent 
     Intent intent = new Intent(ACTION_SCAN); 
     intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); 
     startActivityForResult(intent, 0); 
    } catch (ActivityNotFoundException anfe) { 
     //on catch, show the download dialog 
     showDialog(MainActivity.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show(); 
    } 
} 

//alert dialog for downloadDialog 
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) { 
     AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act); 
    downloadDialog.setTitle(title); 
    downloadDialog.setMessage(message); 
    downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialogInterface, int i) { 
      Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android"); 
      Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
      try { 
       act.startActivity(intent); 
      } catch (ActivityNotFoundException anfe) { 

      } 
     } 
    }); 
    downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialogInterface, int i) { 
     } 
    }); 
    return downloadDialog.show(); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent intent)  { 
    if (requestCode == 0) { 
     if (resultCode == RESULT_OK) { 
      String contents = intent.getStringExtra("SCAN_RESULT"); 

      EditText resultTxt = (EditText) findViewById(R.id.contents); 
      resultTxt.setText(contents); 
      resultTxt.setVisibility(View.VISIBLE); 
     } 

    } 

} 

public void post(View view, Intent intent) { 

       String contents = intent.getStringExtra("SCAN_RESULT"); 
       new JSONtask().execute(contents); 

      } 


    public class JSONtask extends AsyncTask<String, String, Void> { 

    @Override 
    protected Void doInBackground(String... params) { 

     try { 

      // 1. URL 
      URL url = new URL("http://192.168.1.10:8080/SRNSmartLab/rest/service/storeNEdata"); 
      final String contents = params[0]; 
      // 2. Open connection 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

      // 3. Specify POST method 
      conn.setRequestMethod("POST"); 

      // 4. Set the headers 
      conn.setRequestProperty("Content-Type", "application/json"); 
      conn.setDoOutput(true); 

      // 5. Add JSON data into POST request body 


      //`5.1 Use Jackson object mapper to convert Contnet object into JSON 
      //ObjectMapper mapper = new ObjectMapper(); 

       // 5.2 Get connection output stream 
       DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); 

      // 5.3 Copy Content "JSON" into 
      wr.writeBytes(contents); 

      // 5.4 Send the request 
      wr.flush(); 

      // 5.5 close 
      wr.close(); 

      // 6. Get the response 
      int responseCode = conn.getResponseCode(); 
      System.out.println("\nSending 'POST' request to URL : " + url); 
      System.out.println("Response Code : " + responseCode); 

      BufferedReader in = new BufferedReader(
        new InputStreamReader(conn.getInputStream())); 
      String inputLine; 
      StringBuffer response = new StringBuffer(); 

      while ((inputLine = in.readLine()) != null) { 
       response.append(inputLine); 
      } 
      in.close(); 

      // 7. Print result 
      System.out.println(response.toString()); 

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

     return null; 
    } 

} 

}

這裏是佈局代碼。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.lab.smart.smartlabinventry.MainActivity" 
    tools:showIn="@layout/activity_main" 
    android:background="#143de0"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/Scan" 
     android:id="@+id/textView" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:textColor="#faf6f6" 
    android:textSize="30sp"/> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/Scan1" 
     android:id="@+id/button" 
     android:onClick="Scan" 
     android:layout_above="@+id/spinner" 
     android:layout_toRightOf="@+id/contents" 
     android:layout_toEndOf="@+id/contents" /> 

    <Spinner 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/spinner" 
     android:layout_centerVertical="true" 
     android:layout_alignRight="@+id/button2" 
     android:layout_alignEnd="@+id/button2" /> 
    <EditText 
     android:id="@+id/contents" 
     android:editable="false" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/spinner" 
     android:textColor="#fdf9f9" 
     android:layout_marginTop="15dp" 
     android:visibility="gone" 
     /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/upload" 
     android:id="@+id/button2" 
     android:onClick="post" 
     android:layout_alignTop="@+id/button" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

</RelativeLayout> 

回答

1

你的錯誤明確規定了你的錯誤

java.lang.IllegalStateException:找不到支持Android父母或祖先上下文方法後(查看):onclick屬性定義。

您呼籲從button2post()它會調用post(View view),你期待調用post(View view, Intent intent)。如果你想打電話post(View view, Intent intent)然後添加按鈕clicklistener和調用你的方法。

btnPost.setOnClickListener(new View.OnClickListener() { 
 
    @Override 
 
    public void onClick(View view) { 
 
     post(view,intent); 
 
    } 
 
});

+0

謝謝你的寶貴支持.. –

+0

@KaruppusamyPachiappan如果這是真的,接受和upvote未來refrences。 –

0

不,您不能有兩個OnClickListener參數。

爲了解決您的問題,只是存儲的掃描結果作爲成員變量,你已經有了:

public String contents; 

然後,在onActivityResult,把結果給成員變量,而不是一個局部變量:

public void onActivityResult(int requestCode, int resultCode, Intent intent)  { 
    if (requestCode == 0) { 
     if (resultCode == RESULT_OK) { 
      //modified: 
      contents = intent.getStringExtra("SCAN_RESULT"); 

      EditText resultTxt = (EditText) findViewById(R.id.contents); 
      resultTxt.setText(contents); 
      resultTxt.setVisibility(View.VISIBLE); 
     } 

    } 

} 

然後,在後()方法,只要確保contents不爲空:

 public void post(View view) { 

      if (contents != null) { 
       new JSONtask().execute(contents); 
      } 

     } 
+0

非常感謝丹尼爾。它的工作完美.. –