无法从项目点击列表视图进入webview

时间:2013-01-17 11:11:03

标签: android listview webview android-listview android-webview

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

这是我的代码

爪哇

    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)

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

web位于“activity_ugsimpliy_beta布局”中。您正在通过说不包含web的setContentView(R.layout.webview)将内容视图更改为新的LAYOUT,因此错误即将发生。删除setContentView(R.layout.webview);从您的代码和使列表视图不可见,它将运行良好。