Je reçois toujours un Java.lang.NullPointerException
lorsque j'essaie d'utiliser ScrollView
dans une activité.
La chose étrange est que j'ai utilisé exactement la même configuration Scrollview
dans d'autres activités. Et tout d'un coup, je reçois des erreurs. J'ai essayé de nettoyer le projet et de le reconstruire.
Et j'ai essayé le bon vieux redémarrage du programme. J'ai beaucoup appris sur les exceptions de pointeur nul aujourd'hui, mais je n'ai pas été en mesure de comprendre pourquoi je continue à obtenir problèmes de rend.
C'est le Java.lang.NullPointerException
qui est jeté sur le XML:
Java.lang.NullPointerException
at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:738)
at Android.view.BridgeInflater.createViewFromTag(BridgeInflater.Java:149)
at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:704)
at Android.view.LayoutInflater.rInflate_Original(LayoutInflater.Java:835)
at Android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.Java:70)
at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:811)
at Android.view.LayoutInflater.rInflateChildren(LayoutInflater.Java:798)
at Android.view.LayoutInflater.rInflate_Original(LayoutInflater.Java:838)
at Android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.Java:70)
at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:811)
at Android.view.LayoutInflater.rInflateChildren(LayoutInflater.Java:798)
at Android.view.LayoutInflater.rInflate_Original(LayoutInflater.Java:838)
at Android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.Java:70)
at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:811)
at Android.view.LayoutInflater.rInflateChildren(LayoutInflater.Java:798)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:515)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:394)
at com.Android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.Java:223)
at com.Android.layoutlib.bridge.Bridge.createSession(Bridge.Java:426)
at com.Android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.Java:350)
at com.Android.tools.idea.rendering.RenderTask$2.compute(RenderTask.Java:510)
at com.Android.tools.idea.rendering.RenderTask$2.compute(RenderTask.Java:498)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.Java:888)
at com.Android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.Java:498)
at com.Android.tools.idea.rendering.RenderTask.access$600(RenderTask.Java:72)
at com.Android.tools.idea.rendering.RenderTask$3.call(RenderTask.Java:610)
at com.Android.tools.idea.rendering.RenderTask$3.call(RenderTask.Java:607)
at com.Android.tools.idea.rendering.RenderService.runRenderAction(RenderService.Java:362)
at com.Android.tools.idea.rendering.RenderTask.render(RenderTask.Java:607)
at com.Android.tools.idea.rendering.RenderTask.render(RenderTask.Java:629)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.Java:652)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.Java:80)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.Java:594)
at com.intellij.openapi.progress.impl.CoreProgressManager$2.run(CoreProgressManager.Java:152)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.Java:452)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.Java:402)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.Java:54)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.Java:137)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.Java:589)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.Java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.Java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.Java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.Java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.Java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.Java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.Java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.Java:351)
at Java.util.concurrent.Executors$RunnableAdapter.call(Executors.Java:511)
at Java.util.concurrent.FutureTask.run(FutureTask.Java:266)
at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1142)
at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:617)
at Java.lang.Thread.run(Thread.Java:745)
Donc, rien de tout cela n'est du code que j'ai écrit. J'ai donc plongé dans la trace de la pile et j'ai trouvé ceci:
Java.lang.NullPointerException: Attempt to invoke virtual method 'boolean Java.lang.String.equals(Java.lang.Object)' on a null object reference
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2325)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2387)
at Android.app.ActivityThread.access$800(ActivityThread.Java:151)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1303)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5254)
at Java.lang.reflect.Method.invoke(Native Method)
at Java.lang.reflect.Method.invoke(Method.Java:372)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:903)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:698)
Caused by: Java.lang.NullPointerException: Attempt to invoke virtual method 'boolean Java.lang.String.equals(Java.lang.Object)' on a null object reference
at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:715)
at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:806)
at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:809)
at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:809)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:504)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:414)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:365)
at com.Android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.Java:378)
at Android.app.Activity.setContentView(Activity.Java:2145)
at com.mitchellpalermo.tutroubleshooting.ContactPage.onCreate(ContactPage.Java:11)
at Android.app.Activity.performCreate(Activity.Java:5990)
at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1106)
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2278)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2387)
at Android.app.ActivityThread.access$800(ActivityThread.Java:151)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1303)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5254)
at Java.lang.reflect.Method.invoke(Native Method)
at Java.lang.reflect.Method.invoke(Method.Java:372)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:903)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:698)
Qui pointe vers ligne 11 sur le Contact.Java fichier. Qui ressemble à ceci (j'ai mis des indicateurs autour de la ligne 11):
package com.mitchellpalermo.tutroubleshooting;
import Android.app.Activity;
import Android.os.Bundle;
public class Contact extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact); // line 11
}
}
Je ne vois pas de problème avec ligne 11 .
Maintenant, je ne peux pas comprendre ce qui ne va pas avec mon code. Entre problèmes de rend et NullPointerExceptions
, je n'arrive pas à comprendre ce qui ne va pas avec mon code.
Ceci est un exemple de XML dans une activité dans laquelle le ScrollView
ne génère aucune erreur:
<ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="fill_parent"
Android:orientation="vertical"
tools:context="com.mitchellpalermo.tutroubleshooting.ComputerOrProjector">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:orientation="vertical">
<TextView
style="@style/HeaderStyle"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:text="@string/solutions"
Android:textStyle="bold" />
<!-- A CardView that contains a TextView -->
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:layout_margin="15dp"
card_view:cardBackgroundColor="#FFFFFF"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical">
<TextView
style="@style/Solution"
Android:text="@string/pc_v_fifth" />
<View
Android:layout_width="match_parent"
Android:layout_height="2dp"
Android:background="@Android:color/darker_gray" />
<TextView
style="@style/Solution"
Android:text="@string/pc_v_first" />
<View
Android:layout_width="match_parent"
Android:layout_height="2dp"
Android:background="@Android:color/darker_gray" />
<TextView
style="@style/Solution"
Android:text="@string/pc_v_second" />
<View
Android:layout_width="match_parent"
Android:layout_height="2dp"
Android:background="@Android:color/darker_gray" />
<TextView
style="@style/Solution"
Android:text="@string/pc_v_third" />
<View
Android:layout_width="match_parent"
Android:layout_height="2dp"
Android:background="@Android:color/darker_gray" />
<TextView
style="@style/Solution"
Android:text="@string/pc_v_fourth" />
<View
Android:layout_width="match_parent"
Android:layout_height="2dp"
Android:background="@Android:color/darker_gray" />
</LinearLayout>
</Android.support.v7.widget.CardView>
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="horizontal">
<Button
style="@style/NextButton"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:onClick="sendEmail"
Android:text="@string/fixed"
Android:textStyle="bold" />
<Button
style="@style/NextButton"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:onClick="notFixedButton"
Android:text="@string/not_fixed"
Android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Et voici le activité dans lequel je reçois des erreurs:
<ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:fillViewport="false">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical">
<TextView
style="@style/HeaderStyle"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:text="@string/still_not_fixed" />
<!-- A CardView that contains a TextView -->
<Android.support.v7.widget.CardView xmlns:card_view="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:layout_margin="15dp"
card_view:cardBackgroundColor="#FFFFFF"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:text="@string/hector" />
<view
Android:layout_width="match_parent"
Android:layout_height="2dp"
Android:backgroundTint="@Android:color/darker_gray" />
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:text="@string/robert" />
<view
Android:layout_width="match_parent"
Android:layout_height="2dp"
Android:backgroundTint="@Android:color/darker_gray" />
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:text="@string/mitch" />
</Android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
Aucune suggestion? Et merci beaucoup d'avance!
Changement <view
à <View
, car view
ne concerne pas la vue vide . C'est pour la vue personnalisée définie via class
attr, comme ci-dessous:
<view
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
class="com.your.package.YourCustomView" />
Et tu as
Caused by: Java.lang.NullPointerException: Attempt to invoke virtual method 'boolean Java.lang.String.equals(Java.lang.Object)' on a null object reference
à cause de LayoutInflater
essaie d'analyser class
attr:
LayoutInflater
code source
//...
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
boolean ignoreThemeAttr) {
if (name.equals("view")) { // line 724
name = attrs.getAttributeValue(null, "class"); // line 725
}
// Apply a theme wrapper, if allowed and one is specified.
if (!ignoreThemeAttr) {
final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
final int themeResId = ta.getResourceId(0, 0);
if (themeResId != 0) {
context = new ContextThemeWrapper(context, themeResId);
}
ta.recycle();
}
if (name.equals(TAG_1995)) { // line 738
// Let's party like it's 1995!
return new BlinkLayout(context, attrs);
}
//...
view
et obtient true
class
attr et obtient null
blink
et obtient un plantageEn outre, il y a une chose curieuse, Android ont ajouté Easter Egg, vous pouvez l'essayer:
<view
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
class="blink">
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:gravity="center"
Android:text="Some text" />
</view>
Après 2 jours, j'ai résolu ce problème, utilisez View au lieu de view
<View
Android:layout_width="match_parent"
Android:layout_height="1dp"
Android:background="#faf4f4"></View>