2017-10-21 6 views
-1

DrawingLayout을 만드는 중이며 각 파트에 더 많은 XML 파일이 있으므로 1 개의 클래스와 여러 XML 파일 만 있습니다. 이제는 주요 활동에서 하나의 XML 파일로 TextView 텍스트를 설정하고 레이아웃 인플레이터를 사용해 보았습니다.하지만 작업을하지 못했습니다. 어떤 경우에는 오류가 발생했고 다른 경우에는 도움을 요청하는 이유가 없습니다.
가 여기 내 activity_menu.xml 코드다른 XML 파일에서 TextView 텍스트 변경

다음
<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:background="@color/colorBackground" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_navigation" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     app:itemTextColor="@color/colorPrimary" 
     android:background="@color/colorBackground" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_navigation" 
     app:menu="@menu/activity_navigation_drawer" /> 

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

이 nav_header_navigation.xml (즉 하나의 내가 MenuActivity.class에서 사용하는 주요 XML 파일입니다) (그것은 내가 내 MenuActivity에서 변경할 텍스트 2 TextViews을 포함 클래스)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:id="@+id/linearLayout1" 
    android:layout_height="@dimen/nav_header_height" 
    android:background="@drawable/side_nav_bar" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <TextView 
     android:id="@+id/imeNaloga" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:text="" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

    <TextView 
     android:id="@+id/modelMarka" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="[email protected]" /> 

</LinearLayout> 
+1

더 구체적으로하시기 바랍니다. 첨부 자바 코드 –

+0

당신이 볼 수 있듯이 내가 id를 두 textviews에 할당하고, imeNaloga = (EditText) findViewById (R.id.imeNaloga)와 같이 호출하려고 할 때; , then imeNaloga.setText ("Something"); 하지만 null 포인터 예외가 발생했습니다 – Praziluk

+0

또한 시도 : LayoutInflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE); 보기 vi = inflater.inflate (R.layout.nav_header_navigation, null); //log.xml이 귀하의 파일입니다. TextView tv = (TextView) vi.findViewById (R.id.imeNaloga); tv.setText ("Nesto"); 오류는 발생하지 않지만 응용 프로그램에서는 아무 것도 발생하지 않습니다. – Praziluk

답변

0

나는 마침내 해결책을 발견하고 미래에 누군가가 도움이되기를 바랍니다 :

NavigationView nav_view= (NavigationView)findViewById(R.id.nav_view);//this is navigation view from my main xml where i call another xml file 
View header = nav_view.getHeaderView(0);//set View header to nav_view first element (i guess) 
TextView imeNaloga = (TextView)header.findViewById(R.id.imeNaloga);//now assign textview imeNaloga to header.id since we made View header. 
imeNaloga.setText(Ime);// And now just set text to that textview 
+0

답변 나를 도와 주었다. 이것은 "탐색 드로어"설정으로 새 템플릿 안드로이드 파일을 만들고 서랍 안의 머리글을 편집하려는 경우에 해당합니다. – Victor