android - 单击项目时无法从 ListView 进入 webview

标签 android listview webview android-listview android-webview

我的这段代码有问题,我无法进入在我的 ListView 项点击中定义的 url,我得到了空指针异常。我已经创建了互联网许可。

这是我的代码

Java

    public class UGSimpliyBeta extends Activity {
    ListView list_utama;
    Intent intent;
    WebView web;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ugsimpliy_beta);
        list_utama = (ListView)findViewById(R.id.list_utama);
        web = (WebView)findViewById(R.id.webview);

        String list_content[] = new String[]{"UG Web Service", "Schedule Service", "Exam Service", "Studentsite News", "About"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list_content)
                {
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) 
                    {
                        View view = super.getView(position, convertView, parent);
                        TextView textView=(TextView) view.findViewById(android.R.id.text1);
                        /*YOUR CHOICE OF COLOR*/
                        textView.setTextColor(Color.WHITE);
                        return view;
                    }
                };

        list_utama.setAdapter(adapter);

        list_utama.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id)
            {
                Object item = parent.getItemAtPosition(pos);
                String content = item.toString();
                if(content.equals("UG Web Service"))
                {
                    Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show();
                }
                else if(content.equals("Schedule Service"))
                {
                    Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show();
                }
                else if(content.equals("Exam Schedule"))
                {
                    Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show();
                }
                else if(content.equals("Studentsite News"))
                {
                    Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show();
                    setContentView(R.layout.webview);
                    final Activity activity = UGSimpliyBeta.this;
                    final ProgressDialog progressdialog = new ProgressDialog(activity);
                    progressdialog.setCancelable(true);
                    web.setWebViewClient(new WebViewClient());
web.setWebChromeClient(new WebChromeClient()
                    {
                        public void onProgressChanged(WebView view, int progress)
                        {
                            progressdialog.show();
                            progressdialog.setProgress(0);
                            activity.setProgress(progress * 1000);
                            if(progress == 100 && progressdialog.isShowing())
                            {
                                progressdialog.dismiss();
                            }
                        }
                    });
                    web.loadUrl("https://www.studentsite.gunadarma.ac.id/news/news.php");

                }
                else
                {
                    Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show();
                    intent = new Intent(UGSimpliyBeta.this, About.class);
                    startActivity(intent);
                }
            }
        });
    }

WebView .xml

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id = "@+id/view"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:scrollbars = "none">
</WebView>

activity_ugsimply_beta.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:background = "@drawable/tile">
<ImageView
    android:layout_width = "200px"
    android:layout_height = "200px"
    android:layout_above = "@+id/title"
    android:layout_centerHorizontal = "true"
    android:layout_centerVertical = "true"      
    android:src = "@drawable/app"/>
<TextView
    android:id = "@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity = "center"
    android:textColor="#ffffff"
    android:text="@string/hello_world"
    tools:context=".UGSimpliyBeta" />
<ListView 
    android:id = "@+id/list_utama"
    android:layout_width="fill_parent"
    android:layout_height = "wrap_content"
    android:layout_below="@+id/title"
    android:layout_centerHorizontal = "true"
    android:layout_centerVertical = "true"
    android:cacheColorHint="#00000000"/>
</RelativeLayout>

这是为了 logcat

  12-06 20:59:27.277: E/AndroidRuntime(3760): FATAL EXCEPTION: main
  12-06 20:59:27.277: E/AndroidRuntime(3760): java.lang.NullPointerException
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at com.rk.ug_simplify.UGSimpliyBeta$2.onItemClick(UGSimpliyBeta.java:69)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at android.widget.AdapterView.performItemClick(AdapterView.java:292)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at android.widget.AbsListView$1.run(AbsListView.java:3168)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at android.os.Handler.handleCallback(Handler.java:605)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at android.os.Handler.dispatchMessage(Handler.java:92)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at android.os.Looper.loop(Looper.java:137)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at android.app.ActivityThread.main(ActivityThread.java:4340)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at java.lang.reflect.Method.invokeNative(Native Method)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at java.lang.reflect.Method.invoke(Method.java:511)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  12-06 20:59:27.277: E/AndroidRuntime(3760):   at dalvik.system.NativeStart.main(Native Method)

是什么导致错误以及如何解决?谢谢。

最佳答案

试试这个:在 setContentView(R.layout.webview) 之后; 把这段代码 web = (WebView)findViewById(R.id.view);

但在一个 Activity 中调用多次 setContentView() 是一种习惯。 将您的 webView 放在另一个 Activity 或 Dialog 中可能会更好。

关于android - 单击项目时无法从 ListView 进入 webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14377762/

相关文章:

java - 单击 listView 上的按钮后获取 _id 和位置项目 - Android

android - 将 webview 内容保存到 android 存储并加载它

objective-c - NSMutableURLRequest 不支持用户代理更改

android - macOS 安卓模拟器 : qemu-system-i386 - image not found

android - 如何在android中收听相机拍摄的图片

android - Google Analytics 回溯 Proguard

android - 使用不同布局时的 Listview 回收

android - 从 android ContactsContract 数据库加载数据后 fragment 未填充

java - 在 WebView 加载之前替换 fragment 时应用程序崩溃

android - proguard 启用时不生成发布版本