好的,所以我有一个带有 8 个按钮的主类,并且我有每个按钮来设置内容视图布局 1、2 等。我已经在每个布局中实现了具有不同 ID 的 webview,但我似乎可以让它工作。计划是在每个布局中显示来自网站的图片,由于我不擅长编程,我想我会先尝试一下。这是我的问题:我怎样才能使这个设置工作(就我理解的而言),以及获得这个结果的最佳方法是什么?我得到空指针错误。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
public class Main extends Activity implements View.OnClickListener {
Button btn1, btn2, //etc..;
WebView webview_1, webview_2, //etc..;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(this);
Button btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(this);
//and so on
WebView one = (WebView) findViewById(R.id.webview1);
one.getSettings().setJavaScriptEnabled(true);
one.loadUrl("www.example.com/jpg);
WebView two = (WebView) findViewById(R.id.webview2);
two.getSettings().setJavaScriptEnabled(true);
two.loadUrl("www.example.com/jpg);
//etc
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
setContentView(R.layout.layout1);
return;
case R.id.button2:
setContentView(R.layout.layout2);
return;
//etc
}
//main_layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<LinearLayout
android:layout_width="450dip"
android:layout_height="450dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/norge"
android:orientation="vertical"
android:padding="150dp" >
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:gravity="center"
android:text="@string/overskrift_velg_sensor"
android:textSize="25dp"
tools:context=".Main" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
android:text="@string/button2" />
</LinearLayout>
//layout1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Object"
android:textAppearance="?android:attr/textAppearanceLarge" />
<WebView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/webview1"
android:layout_weight="4">
</WebView>
</LinearLayout>
//layout2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Object2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<WebView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/webview2"
android:layout_weight="4">
</WebView>
</LinearLayout>