我的应用程序崩溃了一段非常特殊的代码,似乎没有任何问题......实际上,这适用于大多数设备,但不适用于我的nexus 7(这是我测试的唯一一款平板电脑)它,来考虑它)。
这是它正在破解的代码行,
findViewById(R.id.btbutton).setVisibility(View.GONE);
位于onResume
这是堆栈跟踪
12-11 18:06:52.955 5674-5674/com.blank.blank E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.blank.blank, PID: 5674
java.lang.RuntimeException: Unable to resume activity {com.blank.blank/com.blank.blank.activities.MerchantListActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at
com.blank.blank.activities.MerchantListActivity.onResume(MerchantListActivity.java:164)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
at android.app.Activity.performResume(Activity.java:5310)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
再次请记住,这适用于其他设备。我已经尝试为代码行创建一个按钮变量并设置其可见性,我已经检查过我已经正确设置了内容布局,并且我已经检查过我的xml中的变量名与我活动中的名称相匹配。如果我对该行进行注释,它也会出现在onResume
感谢您的帮助
以防万一,这是xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<fragment
android:id="@+id/merchant_list"
android:name="com.blank.blank.fragments.MerchantListFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:tag="ly.wise.ProgramList"
tools:layout="@android:layout/list_content" />
<Button android:id="@+id/statuscalcbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculating Status"
android:minHeight="0dp"
android:background="@color/blue_wisely"
android:textColor="@color/white_opaque"
android:padding="2dp"
android:visibility="gone"/>
<Button android:id="@+id/calcexplain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="We'll notify you when we're done"
android:minHeight="0dp"
android:background="@color/blue"
android:textColor="@color/white_opaque"
android:textAppearance="?android:attr/textAppearanceSmall"
android:padding="2dp"
android:visibility="gone"/>
<Button android:id="@+id/btbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Turn On Bluetooth"
android:background="@color/blue"
android:textColor="@color/white_opaque"
android:visibility="visible"/>
<Button android:id="@+id/btbuttonexplain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tap to learn why"
android:minHeight="0dp"
android:background="@color/blue"
android:textColor="@color/white_opaque"
android:textAppearance="?android:attr/textAppearanceSmall"
android:padding="2dp"
android:visibility="visible"/>
</LinearLayout>
这里是我在我的活动的oncreate方法中设置布局的地方:
setContentView(R.layout.activity_merchant_list);
答案 0 :(得分:-1)
我不确定这会有所帮助,但我会首先尝试将按钮声明为按钮,然后设置可见性:
btbutton = (Button)findViewById(R.id.btbutton);
btbutton.setVisibility(View.GONE);