【问题标题】:Android ListActivity OnListItemClick error with WebviewsWebviews的Android ListActivity OnListItemClick错误
【发布时间】:2010-05-15 13:19:58
【问题描述】:

我一直在研究如何在我的 ListActivity 中的一行被点击时弹出一个 webview。单击它时,我什至无法显示 Toast。需要帮助

我还是 Android 新手,是一名正在努力学习的网络开发人员。之前谢谢。

包 com.mf.ar;

导入 org.json.JSONArray; 导入 org.json.JSONException; 导入 org.json.JSONObject;

导入android.app.Activity; 导入 android.app.AlertDialog; 导入android.app.ListActivity; 导入android.os.Bundle; 导入android.util.Log; 导入android.view.View; 导入android.view.Window; 导入 android.webkit.WebChromeClient; 导入 android.webkit.WebView; 导入 android.webkit.WebViewClient; 导入 android.widget.ArrayAdapter; 导入android.widget.Toast;

公共类 ListViewer 扩展 ListActivity { 私有字符串 mJSON; 私有字符串 [] 列表行; 私有字符串[] listRowsURI;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     //setContentView(R.layout.listview);

     Bundle the = getIntent().getExtras();
     this.mJSON = the.getString("JSON");
     Log.i("ListViewIntentJSON", this.mJSON);

     try {
JSONObject UrbJSON = new JSONObject(mJSON);

JSONObject UrbQuery = UrbJSON.getJSONObject("query");
int rows = UrbQuery.getInt("row");
Log.i("UrbRows", String.format("%d",rows));

JSONArray resultArray = UrbJSON.getJSONArray("result");

this.listRows = new String[rows];
for(int i=0; i<rows; i++) { 
 this.listRows[i] = resultArray.getJSONObject(i).
   getString("business_name").toString();
}
this.listRowsURI = new String[rows];
for(int i=0; i<rows; i++) { 
 this.listRowsURI[i] = resultArray.getJSONObject(i).
   getString("business_uri_mobile").toString();
}
     } catch(JSONException e) {
      e.printStackTrace();
     }

     this.setListAdapter(new ArrayAdapter<String>(this, 
             android.R.layout.simple_list_item_1, ListViewer.this.listRows));
 }

 @Override
 protected void onListItemClick(android.widget.ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);

  String theURI = ListViewer.this.listRowsURI[position].toString();
  /*String theURI = ListViewer.this.listRowsURI[position].toString();
  //String theURI = "http://www.mediafusion.web.id";

  WebView webview = new WebView(this);
  //setContentView(webview);
  //addContentView(webview, null);
  webview.getSettings().setJavaScriptEnabled(true);
  getWindow().requestFeature(Window.FEATURE_PROGRESS);
  final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
  public void onProgressChanged(WebView view, int progress) {
    // Activities and WebViews measure progress with different scales.
    // The progress meter will automatically disappear when we reach 100%
    activity.setProgress(progress * 1000);
  }
});
webview.setWebViewClient(new WebViewClient() {
  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
  }
});
  webview.loadUrl(theURI);*/

  Toast.makeText(ListViewer.this.getApplicationContext(), theURI, Toast.LENGTH_SHORT);

 }

}

【问题讨论】:

    标签: android webview listactivity


    【解决方案1】:

    删除代码中所有出现的getApplicationContext(),事情对你来说会更好。 ListViewerContext -- 只需在您的 Toast 中使用它即可。

    关于WebView,我认为您将获得更好的服务,使其成为自己的活动并从onListItemClick()开始该活动。

    【讨论】:

    • 我已删除 getApplicationContext() 并改用 ListViewer.this 但 Toast 仍然不会显示 :( 无论如何,我正在按照您的建议使 webview 成为自己的活动。将发布更多稍后:)
    • 我已经按照你说的做了,效果很好。谢谢指点!
    猜你喜欢
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多