在android中切换活动时出错

时间:2014-09-30 04:56:11

标签: java android webview

我有一个应用程序,它将RSS提要解析为列表,当我们点击列表中的项目时,它调用另一个活动在WebView中显示rss提要链接中的内容:

public class ListListener implements OnItemClickListener {
  // List item's reference
  List<RssItem> listItems;
  // Calling activity reference
  Activity activity;

  public ListListener(List<RssItem> aListItems, Activity anActivity) {
    listItems = aListItems;
    activity  = anActivity;
  }

/**
 * Start a browser with url from the rss item.
 */
  public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
    //Intent i = new Intent(Intent.ACTION_VIEW);
    //i.setData(Uri.parse(listItems.get(pos).getLink()));
    Intent i = new Intent(activity, WebPageActivity.class);
    i.putExtra("LINK", Uri.parse(listItems.get(pos).getLink()));
    //Intent intent = new Intent(this, WebPageController(listItems.get(pos).getLink()));
    activity.startActivity(i);

  }

}

从onItemClick它应该调用该WebView活动来显示网页

  public class WebPageActivity extends ActionBarActivity {
    private WebView webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String url = intent.getStringExtra("LINK");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_page);

        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webview = (WebView) findViewById(R.id.webView1);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl(url);
     }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.web_page, menu);
    return true;
}

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
   }
   }

编译时没有错误,但是当我点击应用程序继续进行WebPage活动时,它就会停止。 这是我的logcat http://pastie.org/9606345

3 个答案:

答案 0 :(得分:0)

用以下代码替换你的onCreate:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String url = intent.getStringExtra("LINK");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_page);

        webview = (WebView) findViewById(R.id.webView1);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl(url);
     }

问题是您在定义webview id之前获得了WebSettings。

答案 1 :(得分:0)

试试如下:

public class WebPageActivity extends ActionBarActivity {
    private WebView webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_page);

        Intent intent = getIntent();
        String url = intent.getStringExtra("LINK");

         webview = (WebView) findViewById(R.id.webView1);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl(url);
     }

答案 2 :(得分:0)

选中此Answer,它表示您需要修复项目依赖关系,因为&#39; gen&#39;首先,然后&#39; src&#39;。试试看。它应解决ClassDefNotFound error