我有这段代码:
09-20 18:06:37.279 21398-21398/info.androidhive.camerafileupload E/AndroidRuntime: FATAL EXCEPTION: main
Process: info.androidhive.camerafileupload, PID: 21398
java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.camerafileupload/info.androidhive.camerafileupload.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference
at info.androidhive.camerafileupload.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
使用Javascript / JQuery时我想隐藏一些部分。 但是,上面的代码给了我一个语法错误。
我该怎么做才能比较我的listitem中的id和javascript字符串?
我已经尝试使用<script>
var array = ["test1", "test2", "test & test"];
$(document).ready(function() {
for (i = 0; i < array.length; i++) {
$("#" + array[i]).hide();
}
});
</script>
<ul>
<li id="test1"></li>
<li id="test2"></li>
<li id="test & test"></li>
</ul>
和"& ;"
由于我从数据库中获取了listitem的id,因此我不想改变那一方。
答案 0 :(得分:0)
您不能在元素的id中使用空格。
您可能不希望在元素的id中使用&符号,但如果您真的需要,可以使用css选择器中的一个反斜杠转义&
,并在jQuery选择器中使用两个反斜杠
在你的例子中:
var array = ["test1", "test2", "test\\&test"];
$(document).ready(function() {
for (i = 0; i < array.length; i++) {
$("#" + array[i]).hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<ul>
<li id="test1"></li>
<li id="test2"></li>
<li id="test&test"></li>
</ul>
答案 1 :(得分:0)
id不能包含空格
用“test_test”替换你的id“test&amp; test”,它会起作用。
var array = ["test1", "test2", "test_test"];
$(document).ready(function() {
for (i = 0; i < array.length; i++) {
$("#" + array[i]).hide();
}
});
和js
\\
或者如果你真的需要使用&符号在js数组中使用var beacons = {
"key": { "one": "char", "two": "sue", "three": "johnny" }, "key2": { "one": "char", "two": "sue", "three": "johnny" }, "key3": { "one": "char", "two": "cara", "three": "johnny" }
};
var beaconsArray = [];
for(var key in beacons) {
beaconsArray.push(beacons[key]);
}
console.log(beaconsArray);