-1

我无法弄清楚为什么这两个按钮不起作用,我有一个布局文件,当用户在我的应用程序中选择某个主题时会在其中创建。有了布局,它有一个 web 视图,它有 2 个按钮可以后退和前进。

   <Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:text="Back" 
    android:layout_alignParentLeft="true" 
    android:layout_height="wrap_content" 
    android:background="@drawable/button_blue" 
    style="@style/ButtonText">
  </Button>

  <Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:background="@drawable/button_blue"
    style="@style/ButtonText" 
    android:text="Forward">
 </Button>


  <WebView
    android:id="@+id/webview01"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1.12" >

这不是整个布局文件,但这是我正在为 webview 和 2 个按钮使用的一些东西,现在在我的主要活动中是我的按钮代码。

      @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (Prefs.theme.equals("Theme1"))
        setContentView(R.layout.main);
    else if (Prefs.theme.equals("Theme2"))
        setContentView(R.layout.main2);
    else if (Prefs.theme.equals("Theme3"))
        setContentView(R.layout.main3);
    else setContentView(R.layout.main);



    btnForward=(Button) findViewById (R.id.button1);
    btnBackward=(Button) findViewById (R.id.button2);
    btnForward.setOnClickListener(this);
    btnBackward.setOnClickListener(this);


    // more code within on create .....

// Later in the code 

public void onClick(View v) {

    switch(v.getId()) {

        case R.id.button1:
            WebViewClientDemoActivity.web.goBack();
           break;

        case R.id.button2:
            WebViewClientDemoActivity.web.goForward();
           break;
    }

//

我的问题是默认情况下它加载 main.xml 而不是 main3(这是 2 个按钮)

LogCat 错误

  07-27 16:00:34.802: E/AndroidRuntime(547): FATAL EXCEPTION: main
  07-27 16:00:34.802: E/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity       ComponentInfo{com.jaisonbrooks.enlighten/com.jaisonbrooks.enlighten.WebViewClientDemoActivity}: java.lang.NullPointerException
  07-27 16:00:34.802: E/AndroidRuntime(547):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at android.os.Handler.dispatchMessage(Handler.java:99)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at android.os.Looper.loop(Looper.java:123)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at android.app.ActivityThread.main(ActivityThread.java:3683)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at java.lang.reflect.Method.invokeNative(Native Method)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at java.lang.reflect.Method.invoke(Method.java:507)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at dalvik.system.NativeStart.main(Native Method)
  07-27 16:00:34.802: E/AndroidRuntime(547): Caused by: java.lang.NullPointerException
  07-27 16:00:34.802: E/AndroidRuntime(547):    at com.jaisonbrooks.enlighten.WebViewClientDemoActivity.onCreate(WebViewClientDemoActivity.java:75)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  07-27 16:00:34.802: E/AndroidRuntime(547):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
  07-27 16:00:34.802: E/AndroidRuntime(547):    ... 11 more
4

3 回答 3

1

它应该工作。确保您没有 2 个相同的按钮@id
当您将按钮与一个相乘时,这是一个常见问题。(复制粘贴)

于 2012-07-27T15:26:20.550 回答
1

首先在您的 onCreate 方法中通过 ids 找到您的按钮:

public class YourActivity extends Activity implements OnClickListener {
Button btnForward;
Button btnBackward;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnForward=(Button) findViewById (R.id.btnForward);
        btnBackward=(Button) findViewById (R.id.btnBackward);

        //set listeners
        btnForward.setOnClickListener(this);
        btnBackward.setOnClickListener(this);

        // your code here .... 
    }

@Override
public void onClick(View v ) {
     switch(v.getId()) {

        case R.id.btnBackward:
            WebViewClientDemoActivity.web.goBack();
           break;
        case R.id.btnForward:
            WebViewClientDemoActivity.web.goForward();
           break;
    }
}

}

于 2012-07-27T15:47:16.967 回答
0
<Button 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:text="Back" 
android:layout_alignParentLeft="true" 
android:layout_height="wrap_content" 
android:background="@drawable/button_blue" 
style="@style/ButtonText" />

<Button 
android:id="@+id/button2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentRight="true" 
android:background="@drawable/button_blue"
style="@style/ButtonText" 
android:text="Forward" />

尝试这个。

然后稍后在java代码中,执行此操作

 Button btnNext = (Button) findViewById(R.id.button2);
 btnNext.setOnClickListener(new OnClickListener()
 {
        @Override
    public void onClick(View v)
    {
         WebViewClientDemoActivity.web.goForward();
    }
 });


 Button btnBack = (Button) findViewById(R.id.button1);
 btnNext.setOnClickListener(new OnClickListener()
 {
        @Override
    public void onClick(View v)
    {
         WebViewClientDemoActivity.web.goBack();
    }
 });
于 2012-07-27T15:22:21.433 回答