【问题标题】:How to load html files as items in listview.?如何将 html 文件加载为列表视图中的项目。?
【发布时间】:2015-03-02 12:01:55
【问题描述】:

我有类似下面链接中的问题。!

passing url and title to a webview in android

我在 MainActivity 中有这样的代码:

ListView lv;
Arrayadapter<String>aa;
String items[]={"item1","item2"};

@Override
protected void onCreate(Bundle Savedinstancestate){
super.onCreate(Savedinstancestate);
lv=(ListView)findViewById(R.id.listView1);

aa=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_dropdown_item_1line,items) ;
lv.setAdapter(aa);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Intent newActivity0 = new Intent(MainActivity.this, Mywebpage.class);
    newActivity0.putExtra("title", str[position]);
    switch (position) {
        case 0:
            newActivity0.putExtra("url", "http://www.google.com");
            break;
        case 1:
            newActivity0.putExtra("url", "file:///android_asset/item1.html");
            break;
        case 2:
            newActivity0.putExtra("url", "file://android_asset/item2.html");
            break;
    }

Mypage 类中获取意图就像

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webviewpage);

    Bundle extras = getIntent().getExtras();
    String title, url;

    if (extras != null) {
        title = extras.getString("title");
        url = extras.getString("url");

        WebView wbView = (WebView) findViewById(R.id.WebView);
        wbView.getSettings().setJavaScriptEnabled(true);
        wbView.loadUrl(url);

//WbView.setWebViewClient(new myWebViewClient()); }

并且尝试过设置和不设置 webview 客户端(我在最后评论的那一行,带有相应的类)。

当我尝试单击该项目时,出现运行时 NULLPOINTER 异常。

【问题讨论】:

  • @Lele 斜线没有太多:它们必须正好是 3,如正确发布的那样。
  • 我只会传递文件名。其余的可以添加到接收Activity中。
  • 好吧,很难通过看到很少或没有代码来判断...变量是否通过了?文件存在吗?扩展名是否一致(htm vs html)?你如何将 url 加载到 WebView 中?完整的日志也会有所帮助。似乎找不到视图。
  • 也许setContentView() 没有在找到视图之前被调用?
  • in Mypage class get intent ... , Mywebpage.class); 类名不匹配。而这个缺少斜线:newActivity0.putExtra("url", "file://android_asset/item2.html");

标签: android android-activity webview


【解决方案1】:

我找到了问题的答案!我已经声明了 Webview,在我的第二个活动中的 onCreate 方法之前识别它,这导致了这个问题。!

同样在之前提供的代码中,如 putExtra("stringname", "value");

因此,在获得附加功能后,在 Bundle 中

extras.getExtra("stringname");

够了,以前在那个地方搞砸了。!

猜猜,任何额外的 logcat 信息都将有助于进一步解决至少一个问题。!由于已经有一段时间了,之后由于一些问题我能够获得logcat信息,所以我自己工作。!但是谢谢你的帮助。!都投了赞成票。! :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2017-12-04
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多