将setText添加到主活动应用程序后突然停止

时间:2017-05-03 17:54:29

标签: android xml

我正在键入以在主活动中将setText用于TextView。我的应用停止了工作,请参阅我的nav_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/social_image"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@string/cd_main_image"
        android:scaleType="centerCrop"
        android:src="@drawable/left_menu_social_small" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" >

            <com.facebook.login.widget.ProfilePictureView
                android:id="@+id/profimage"
                android:layout_width="72dp"
                android:layout_height="72dp"
                android:layout_margin="20dp"
                android:src="@drawable/ic_person"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginLeft="4dp"
                android:gravity="center_vertical"
                android:orientation="vertical" >

                <zesteve.com.myapplication.font.RobotoTextView
                    android:id="@+id/user_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="4dp"
                    android:text="Ravi Shankar"
                    android:textColor="@android:color/white"
                    android:textSize="24sp"
                    app:typeface="robotoLight" />

                <zesteve.com.myapplication.font.RobotoTextView
                    android:id="@+id/user_location"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="from Visakhapatnam"
                    android:textColor="@android:color/white"
                    android:textSize="12sp"
                    app:typeface="robotoLight" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:orientation="horizontal" >

                    <zesteve.com.myapplication.font.MaterialDesignIconsTextView
                        android:id="@+id/icon_friends"
                        style="@style/TextViewAppearance.Title1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:padding="0dp"
                        android:text="@string/material_icon_account_multiple"
                        android:textColor="@color/main_color_500"
                        android:textSize="22dp" />

                    <zesteve.com.myapplication.font.RobotoTextView
                        android:id="@+id/number_of_friends"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="4dp"
                        android:text="152"
                        android:textColor="@android:color/white"
                        android:textSize="12sp"
                        app:typeface="robotoBold" />

                    <zesteve.com.myapplication.font.RobotoTextView
                        android:id="@+id/fb_gend"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="friends"
                        android:textColor="@android:color/white"
                        android:textSize="12sp"
                        app:typeface="robotoLight" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:orientation="horizontal" >

                    <zesteve.com.myapplication.font.MaterialDesignIconsTextView
                        android:id="@+id/icon_message"
                        style="@style/TextViewAppearance.Title1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:padding="0dp"
                        android:text="@string/material_icon_message"
                        android:textColor="@color/main_color_500"
                        android:textSize="22dp" />

                    <zesteve.com.myapplication.font.RobotoTextView
                        android:id="@+id/number_of_messages"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="4dp"
                        android:text="15"
                        android:textColor="@android:color/white"
                        android:textSize="12sp"
                        app:typeface="robotoBold" />

                    <zesteve.com.myapplication.font.RobotoTextView
                        android:id="@+id/fb_id"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="messages"
                        android:textColor="@android:color/white"
                        android:textSize="12sp"
                        app:typeface="robotoLight" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:orientation="horizontal" >

                    <zesteve.com.myapplication.font.MaterialDesignIconsTextView
                        android:id="@+id/icon_photos"
                        style="@style/TextViewAppearance.Title1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:padding="0dp"
                        android:text="@string/material_icon_image_box"
                        android:textColor="@color/main_color_500"
                        android:textSize="22dp" />

                    <zesteve.com.myapplication.font.RobotoTextView
                        android:id="@+id/number_of_photos"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="4dp"
                        android:text="22"
                        android:textColor="@android:color/white"
                        android:textSize="12sp"
                        app:typeface="robotoBold" />

                    <zesteve.com.myapplication.font.RobotoTextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="photos"
                        android:textColor="@android:color/white"
                        android:textSize="12sp"
                        app:typeface="robotoLight" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:orientation="horizontal" >

                    <zesteve.com.myapplication.font.MaterialDesignIconsTextView
                        android:id="@+id/icon_videos"
                        style="@style/TextViewAppearance.Title1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:padding="0dp"
                        android:text="@string/material_icon_video"
                        android:textColor="@color/main_color_500"
                        android:textSize="22dp" />

                    <zesteve.com.myapplication.font.RobotoTextView
                        android:id="@+id/number_of_videos"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="4dp"
                        android:text="2"
                        android:textColor="@android:color/white"
                        android:textSize="12sp"
                        app:typeface="robotoBold" />

                    <zesteve.com.myapplication.font.RobotoTextView
                        android:id="@+id/fb_email"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="videos"
                        android:textColor="@android:color/white"
                        android:textSize="12sp"
                        app:typeface="robotoLight" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <View
        android:id="@+id/divider_top"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/social_image"
        android:background="@color/material_light_yellow_600" />

</RelativeLayout>

和我的activity_main.xml

<?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:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:background="@color/main_color_grey_300"
    tools:context="zesteve.com.myapplication.MainActivity">


<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/cpb_blue"
    android:id="@+id/toolbar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:title="Select Your Location" />

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/drawerLayout" >

    <FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/containerView">
    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/shitstuff"
        app:itemTextColor="@color/white"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawermenu"

        />

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

</LinearLayout>

和MainActiviry是

package zesteve.com.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.facebook.login.widget.ProfilePictureView;

import static zesteve.com.myapplication.R.attr.headerLayout;
import static zesteve.com.myapplication.R.attr.layout;

public class MainActivity extends AppCompatActivity {
    DrawerLayout mDrawerLayout;
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;
    Session session;

    TextView fbuname , fbulocation , fbuid , fbuemail , fbugend ;
    ImageView fbpropic;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;

        session= new Session(MainActivity.this);

        fbuname = (TextView) findViewById(R.id.user_name);
        fbulocation = (TextView) findViewById(R.id.user_location);
        //fbuid = (TextView) findViewById(R.id.fb_id);
        //fbuemail = (TextView) findViewById(R.id.fb_email);
        //fbugend = (TextView) findViewById(R.id.fb_gend);


        //fbulocation.setText("Hyderabad");
        fbuname.setText("Ramachandra");





        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();



                if (menuItem.getItemId() == R.id.settings) {
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();

                }

                if (menuItem.getItemId() == R.id.home) {
                    FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                    xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
                }


                if (menuItem.getItemId() == R.id.logout){

                    finish();
                    startActivity(new Intent(MainActivity.this, LoginActivity.class));
                }

                return false;
            }

        });



        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
                R.string.app_name);

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        mDrawerToggle.syncState();

    }



    boolean doubleBackToExitPressedOnce = false;

    @Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;
            }
        }, 2000);
    }


}

nav_header.xml是抽屉菜单的标题。我想使用TextViewsetText中显示名称。但我的应用程序突然停止了。

logcat的

05-03 23:06:16.764 2439-2439/zesteve.com.myapplication E/dalvikvm: Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.internal.zzadf.zzg
05-03 23:06:20.376 2439-2439/zesteve.com.myapplication E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
05-03 23:06:20.388 2439-2439/zesteve.com.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{zesteve.com.myapplication/zesteve.com.myapplication.MainActivity}: java.lang.NullPointerException
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                                             at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                             at android.os.Looper.loop(Looper.java:137)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:511)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: java.lang.NullPointerException
                                                                             at zesteve.com.myapplication.MainActivity.onCreate(MainActivity.java:55)
                                                                             at android.app.Activity.performCreate(Activity.java:5104)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                             at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                             at android.os.Looper.loop(Looper.java:137) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                             at dalvik.system.NativeStart.main(Native Method) 

TabFragment.java

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import zesteve.com.myapplication.R;


public class TabFragment extends Fragment {

    public static TabLayout tabLayout;
    public static ViewPager viewPager;
    public static int int_items = 3 ;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View x =  inflater.inflate(R.layout.tab_layout,null);
            tabLayout = (TabLayout) x.findViewById(R.id.tabs);
            viewPager = (ViewPager) x.findViewById(R.id.viewpager);


        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));


        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                    tabLayout.setupWithViewPager(viewPager);
                   }
        });

        return x;

    }

    class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }



        @Override
        public Fragment getItem(int position)
        {
          switch (position){
              case 0 : return new PrimaryFragment();
              case 1 : return new SocialFragment();
              case 2 : return new UpdatesFragment();
          }
        return null;
        }

        @Override
        public int getCount() {

            return int_items;

        }



        @Override
        public CharSequence getPageTitle(int position) {

            switch (position){
                case 0 :
                    return "Category";
                case 1 :
                    return "Events";
                case 2 :
                    return "Updates";
            }
                return null;
        }
    }

}

如果您有任何疑问,请发表评论。

1 个答案:

答案 0 :(得分:2)

您必须使用导航header view的引用来使用TextViewuser_nameuser_location)。

更新MainActivity,如下所示:

public class MainActivity extends AppCompatActivity {

    DrawerLayout mDrawerLayout;
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;
    Session session;

    TextView fbuname , fbulocation , fbuid , fbuemail , fbugend ;
    ImageView fbpropic;

    // NavigationView Header
    View mHeaderView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;

        session= new Session(MainActivity.this);

        // NavigationView Header
        mHeaderView =  mNavigationView.getHeaderView(0);

        // Views
        fbuname = (TextView) mHeaderView.findViewById(R.id.user_name);
        fbulocation = (TextView) mHeaderView.findViewById(R.id.user_location);

        // Set text
        fbulocation.setText("Hyderabad");
        fbuname.setText("Ramachandra");

        ...........
        ......................
    }
}

希望这会有所帮助〜