通过单击列表视图中的项目加载webview

时间:2012-03-16 10:41:08

标签: android listview webview

我想通过单击ListView中的项目来加载WebView,但是当单击某个项目时我的应用程序崩溃了。

编辑:现在发布logcat:

E/AndroidRuntime(1840): FATAL EXCEPTION: main
E/AndroidRuntime(1840): android.content.ActivityNotFoundException: Unable to find explicit activity class     {sport.news/sport.news.WebViewPage}; have you declared this activity in your AndroidManifest.xml?
E/AndroidRuntime(1840): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
E/AndroidRuntime(1840): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
E/AndroidRuntime(1840): at android.app.Activity.startActivityForResult(Activity.java:3190)
E/AndroidRuntime(1840): at android.app.Activity.startActivity(Activity.java:3297)
E/AndroidRuntime(1840): at sport.news.SportnewsActivity.onListItemClick(SportnewsActivity.java:31)
E/AndroidRuntime(1840): at android.app.ListActivityonItemClick(ListActivity.java:319)
E/AndroidRuntime(1840): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
E/AndroidRuntime(1840): at android.widget.AbsListView.performItemClick(AbsListView.java:1083)
E/AndroidRuntime(1840): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2539)
E/AndroidRuntime(1840): at android.widget.AbsListViewrun(AbsListView.java:3193)
E/AndroidRuntime(1840): at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime(1840): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(1840): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(1840): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(1840): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1840): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(1840): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(1840): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(1840): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(187):  Force finishing activity sport.news/.SportnewsActivity

主要活动:

package sport.news;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SportnewsActivity extends ListActivity {
String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
          "Wednesday", "Thursday", "Friday", "Saturday"
        };

           /** Called when the activity is first created. */
           @Override
           public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               //setContentView(R.layout.main);
               setListAdapter(new ArrayAdapter<String>(this,
                 R.layout.row, R.id.weekofday, DayOfWeek));
           }

        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
         // TODO Auto-generated method stub
            //super.onListItemClick(l, v, position, id);
            super.onListItemClick(l,v,position,id);
            Intent in = new Intent(this,WebViewPage.class);
            startActivity(in);
        }
        }

WebView活动:

package sport.news;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewPage  extends Activity{

@Override
 public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.webviewpage);
        WebView wbView = (WebView) findViewById(R.id.WebView);
        wbView.getSettings().setJavaScriptEnabled(true); 
        wbView.loadUrl("http://www.google.com");
   }
}

这是manifest.xml:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sport.news"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".SportnewsActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

 <activity android:name=".WebViewPage" />


<uses-permission android:name="android.permission.INTERNET" />

</manifest>

3 个答案:

答案 0 :(得分:0)

您是否在Manifest文件中声明了WebViewPage.class

 <activity android:name=".WebViewPage"/>

答案 1 :(得分:0)

Intent i = new Intent(Intent.ACTION_VIEW);
            Uri u = Uri.parse(websiteText.getText().toString());
            i.setData(u);
            try {
                startActivity(i);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(PassEdit.this,   R.string.invalid_website,   Toast.LENGTH_SHORT).show();
            }
        }

答案 2 :(得分:0)

请在</application>代码之前声明您的活动。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sport.news"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".SportnewsActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

 <activity android:name=".WebViewPage" />
</application>