2016-10-26 2 views
-1

Am Anfang - ich habe alle Lösungen versucht. HierVoid android.widget.TextView.setText (java.lang.CharSequence) 'auf einem Nullobjekt Referenz

ist der Fehler:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
        at com.justfashion.ActivityMainWallet.onCreate(ActivityMainWallet.java:211)` 

ActivityMainWallet.java:210-212 Linien:

TextView navigationTotalCreditTextView = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_total_credits); 
    navigationTotalCreditTextView.setText("Total Credits: " + PreferenceConnector.readInteger(aiContext, 
      PreferenceConnector.WALLETPOINTS,"")); 

XML:

<android.support.v4.widget.DrawerLayout 
 
    xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:id="@+id/drawer_layout" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:background="#ffffff" > 
 

 
    <TextView 
 
     android:id="@+id/nav_drawer_wallet_id" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_centerHorizontal="true" 
 
     android:layout_centerVertical="true" 
 
     android:gravity="center" 
 
     android:textColor="@color/md_white_1000" 
 
     android:textSize="@dimen/twentyfive" /> 
 
    <TextView 
 
     android:id="@+id/nav_drawer_total_credits" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_centerHorizontal="true" 
 
     android:layout_centerVertical="true" 
 
     android:textColor="@color/md_white_1000" 
 
     android:textSize="@dimen/twentyfive" /> 
 
    <TextView 
 
     android:id="@+id/nav_drawer_username" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_centerHorizontal="true" 
 
     android:layout_centerVertical="true" 
 
     android:gravity="center" 
 
     android:textColor="@color/md_white_1000" 
 
     android:textSize="@dimen/twentyfive" /> 
 

 
    <RelativeLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent"> 
 

 
     <RelativeLayout android:id="@+id/content_frame_container" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="match_parent" 
 
      android:background="#ffffff" 
 
      android:layout_below="@+id/toolbar_app_bar_layout"> 
 

 
      <FrameLayout 
 
       android:id="@+id/content_frame" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="match_parent"> 
 
      </FrameLayout> 
 

 
     </RelativeLayout> 
 

 
     <android.support.v4.view.ViewPager 
 
      android:id="@+id/pager" 
 
      android:layout_width="fill_parent" 
 
      android:layout_height="match_parent" 
 
      android:layout_below="@+id/toolbar_app_bar_layout" /> 
 

 
     <android.support.design.widget.AppBarLayout 
 
      android:id="@+id/toolbar_app_bar_layout" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
 

 
      <include android:id="@+id/toolbar" 
 
       layout="@layout/toolbar" /> 
 

 
      <android.support.design.widget.TabLayout 
 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
 
       android:id="@+id/sliding_tabs" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:layout_below="@+id/toolbar" 
 
       app:tabMode="scrollable" 
 
       app:paddingStart="16dp" 
 
       app:tabPaddingStart="16dp" 
 
       app:tabPaddingEnd="16dp" 
 
       app:tabMinWidth="96dp" 
 
       app:tabGravity="center" /> 
 

 
     </android.support.design.widget.AppBarLayout> 
 

 
    </RelativeLayout> 
 

 
    <android.support.design.widget.NavigationView 
 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
 
     android:id="@+id/navigation_view" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:layout_gravity="start" 
 
     android:background="@color/md_white_1000" 
 
     app:headerLayout="@layout/nav_drawer_header" 
 
     app:menu="@menu/menu_drawer" 
 
     app:itemTextAppearance="@style/TextAppearance.AppCompat.Menu" 
 
     android:clipToPadding="false"> 
 

 
     <!--<FrameLayout--> 
 
      <!--android:layout_width="match_parent"--> 
 
      <!--android:layout_height="wrap_content"--> 
 
      <!--android:layout_gravity="bottom"--> 
 
      <!--android:background="@color/md_white_1000"--> 
 
      <!--android:elevation="4dp"--> 
 
      <!--android:layout_marginBottom="-96dp">--> 
 

 
      <!--<Button android:id="@+id/navigation_button_footer"--> 
 
       <!--android:layout_width="match_parent"--> 
 
       <!--android:layout_height="match_parent"--> 
 
       <!--android:text=""--> 
 
       <!--android:textSize="13sp"--> 
 
       <!--android:textColor="@color/md_grey_800"--> 
 
       <!--android:lines="3"--> 
 
       <!--android:gravity="center"--> 
 
       <!--style="@style/Widget.AppCompat.ActionButton"--> 
 
       <!--android:paddingTop="20dp"--> 
 
       <!--android:paddingLeft="20dp"--> 
 
       <!--android:paddingRight="20dp"--> 
 
       <!--android:paddingBottom="20dp"/>--> 
 

 
     <!--</FrameLayout>--> 
 

 
    </android.support.design.widget.NavigationView> 
 

 
    <RelativeLayout 
 
     android:id="@+id/lay_connection" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:layout_marginTop="@dimen/actmain_margintop" 
 
     android:background="#3c3c3c" 
 
     android:visibility="gone" > 
 

 
     <TextView 
 
      android:id="@+id/text_error" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_centerHorizontal="true" 
 
      android:layout_centerVertical="true" 
 
      android:gravity="center" 
 
      android:text="@string/error_no_internet" 
 
      android:textColor="@color/md_white_1000" 
 
      android:textSize="@dimen/twentyfive" /> 
 

 

 
     <RelativeLayout 
 
      android:id="@+id/lay_dialog" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="match_parent" 
 
      android:visibility="gone" > 
 

 
      <ProgressBar 
 
       android:id="@+id/progressBar1" 
 
       style="?android:attr/progressBarStyleLarge" 
 
       android:layout_width="wrap_content" 
 
       android:layout_height="wrap_content" 
 
       android:layout_centerHorizontal="true" 
 
       android:layout_centerVertical="true" /> 
 

 
      <TextView 
 
       android:id="@+id/textView2" 
 
       android:layout_width="wrap_content" 
 
       android:layout_height="wrap_content" 
 
       android:layout_below="@+id/progressBar1" 
 
       android:layout_centerHorizontal="true" 
 
       android:text="@string/error_no_internet" 
 
       android:textColor="@color/md_white_1000" 
 
       android:textSize="@dimen/twentyfive" /> 
 
     </RelativeLayout> 
 
    </RelativeLayout> 
 

 
</android.support.v4.widget.DrawerLayout>

Ich habe alles versucht. Ist es ein Problem mit PreferenceConnector? Hier ist PreferenceConnector.WALLETPOINTS,"")) in PreferenceConnection.java:

public static final String WALLETPOINTS   = "walletpoints"; 

Hilfe Jungs bitte! Ich habe versucht, jede Lösung, aber niemand hat diesen Fehler mit PrefectionConnector.

#edit

gelöst, aber jetzt diesen Fehler ich erhalte:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSeq‌​uence)' on a null object reference at com.justfashion.ActivityMainWallet.onCreate(ActivityMainWall‌​et.java:251) 
+1

Haben Sie "mDrawerLay" Objekt initialisieren? – vidulaJ

+1

Poste den vollständigen Code von 'onCreate()' bitte. – earthw0rmjim

+0

@vidulaJ http://pastebin.com/SxkTrWdP – user2393513

Antwort

1

Das Problem ist, dass Sie nach TextView in falscher Ansicht suchen.

Sie tun dies:

TextView navigationTotalCreditTextView = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_total_credits); 

Aber die Textansicht ist in mDrawerLayout. siehe unten und auch deine XML.

mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); 
mDrawerLay  = (NavigationView)findViewById(R.id.navigation_view); 

das Problem zu tun, dies zu beheben:

TextView navigationTotalCreditTextView = (TextView)mDrawerLayout.findViewById(R.id.nav_drawer_total_credits); 
+0

Vielen Dank Kumpel! Aber jetzt habe ich diesen Fehler: 'verursacht durch: java.lang.NullPointerException: Versuch, virtuelle Methode 'void android.support.v7.widget.Toolbar.setTitle (java.lang.CharSequence)' auf a null-Objekt Referenz bei com.justfashion.ActivityMainWallet.onCreate (ActivityMainWallet.java:251) ' ich in XML hinzugefügt: ' ' Ist es richtig? – user2393513

+0

Sie erhalten auch die falsche Symbolleiste! Achte immer auf die Ansicht, in der du einen Gegenstand finden willst! ich meine view.findviewbyid! @ user2393513 –

+0

wie es zu beheben? Wo ist eine falsche Symbolleiste? In Java oder XML? – user2393513

1

Try this;

TextView navigationTotalCreditTextView = (TextView)findViewById(R.id.nav_drawer_total_credits); 
navigationTotalCreditTextView.setText("Total Credits: " + PreferenceConnector.readInteger(aiContext, 
     PreferenceConnector.WALLETPOINTS,"")); 
+0

Vielen Dank Kumpel! Aber jetzt habe ich diesen Fehler: 'verursacht durch: java.lang.NullPointerException: Versuch, virtuelle Methode 'void android.support.v7.widget.Toolbar.setTitle (java.lang.CharSequence)' auf a null-Objekt Referenz bei com.justfashion.ActivityMainWallet.onCreate (ActivityMainWallet.java:251) ' ich in XML hinzugefügt: ' ' Ist es richtig? – user2393513

Verwandte Themen