应用程序中窗口小部件的可见性

时间:2014-05-21 08:25:43

标签: android visibility

我在小部件的可见性方面遇到问题..这是代码.. 该错误显示在Java文件的SetVisible命令中。直到我的知识,我写了写代码。

MainActivity.java

public class MainActivity extends Activity {

Switch switch1;
Spinner spinner;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    switch1 = (Switch) findViewById(R.id.switch1);

    switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub

            if (isChecked) {

                spinner.setVisibility(View.VISIBLE);
                button.setVisibility(View.VISIBLE);
            } else {

                spinner.setVisibility(View.INVISIBLE);
                button.setVisibility(View.INVISIBLE);
            }

        }
    });
}}

activity_main.xml中

<LinearLayout 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:orientation="vertical"
tools:context="${packageName}.${activityClass}" >

<Switch
    android:id="@+id/switch1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checked="false"
    android:text="@string/breakingnews" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="invisible" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:text="@string/ok" />

的Manifest.xml

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

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.newsapp.MainActivity"
        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="com.example.newsapp.NewsStrip"
        android:label="@string/title_activity_news_strip" >
    </activity>
</application>

logcat的

 05-21 04:20:37.231: I/Choreographer(2478): Skipped 30 frames!  The application may be doing too much work on its main thread.
05-21 04:20:37.321: D/gralloc_goldfish(2478): Emulator without GPU emulation detected.
05-21 04:20:42.572: E/InputEventReceiver(2478): Exception dispatching input event.
05-21 04:20:42.572: E/MessageQueue-JNI(2478): Exception in MessageQueue callback: handleReceiveCallback
05-21 04:20:42.622: E/MessageQueue-JNI(2478): java.lang.NullPointerException
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at com.example.newsapp.MainActivity$1.onCheckedChanged(MainActivity.java:34)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.widget.Switch.setChecked(Switch.java:666)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.widget.Switch.animateThumbToCheckedState(Switch.java:645)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.widget.Switch.stopDrag(Switch.java:635)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.widget.Switch.onTouchEvent(Switch.java:595)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.View.dispatchTouchEvent(View.java:7384)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.View.dispatchPointerEvent(View.java:7564)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.os.MessageQueue.nativePollOnce(Native Method)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.os.MessageQueue.next(MessageQueue.java:132)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.os.Looper.loop(Looper.java:124)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at android.app.ActivityThread.main(ActivityThread.java:5103)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at java.lang.reflect.Method.invokeNative(Native Method)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at java.lang.reflect.Method.invoke(Method.java:525)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-21 04:20:42.622: E/MessageQueue-JNI(2478):   at dalvik.system.NativeStart.main(Native Method)
05-21 04:20:42.622: D/AndroidRuntime(2478): Shutting down VM
05-21 04:20:42.632: W/dalvikvm(2478): threadid=1: thread exiting with uncaught exception (group=0xb3f76648)
05-21 04:20:42.672: E/AndroidRuntime(2478): FATAL EXCEPTION: main
05-21 04:20:42.672: E/AndroidRuntime(2478): java.lang.NullPointerException
05-21 04:20:42.672: E/AndroidRuntime(2478):     at com.example.newsapp.MainActivity$1.onCheckedChanged(MainActivity.java:34)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.widget.Switch.setChecked(Switch.java:666)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.widget.Switch.animateThumbToCheckedState(Switch.java:645)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.widget.Switch.stopDrag(Switch.java:635)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.widget.Switch.onTouchEvent(Switch.java:595)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.View.dispatchTouchEvent(View.java:7384)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.View.dispatchPointerEvent(View.java:7564)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.os.MessageQueue.nativePollOnce(Native Method)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.os.MessageQueue.next(MessageQueue.java:132)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.os.Looper.loop(Looper.java:124)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at java.lang.reflect.Method.invokeNative(Native Method)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at java.lang.reflect.Method.invoke(Method.java:525)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-21 04:20:42.672: E/AndroidRuntime(2478):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

就像LogCat中的错误一样,您在此处有一个NullPointerException

onCheckedChanged(MainActivity.java:34) 

您的问题是spinnerbuttonnull。你没有初始化就可以使用它们。

尝试在setContentView()

下添加此代码
spinner= (Spinner) findViewById(R.id.spinner1);
button= (Button) findViewById(R.id.button1);

选中link以了解如何阅读Logcat并在代码中找到error

答案 1 :(得分:0)

Button和Spinner没有价值,你忘了放     button =(Button)findViewById(R.id.buttonid); 和旋转器一样。