找不到局部变量'r'

时间:2017-05-16 08:36:52

标签: java android xml

Cannot find local variable 'r'

总是在“imgCnr”和“txtUsername”上得到空值,所以我无法运行我的应用程序,这是第一页中使用的相同代码,但它的工作正常, 由于imgCnr imageView和txtUsername TextView

的空值,所有页面都有异常

我编写的android代码

package `com.example.jishil1.hobnobzz;`

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

/**
 * Created by Jishil1 on 01-02-2017.
 */

public class Education extends AppCompatActivity {

    ImageButton imgFeed;
    ImageButton imgGroup;
    ImageButton imgEducation;
    ImageButton imgNotify;
    ImageButton imgSearch;
    ImageButton imgProfile;
    ImageButton back;
    String M69User;
    DrawerLayout mDrawerLayout;
    ImageButton Admin;
    ImageButton Teacher;
    ImageButton Parent;
    ImageView imgCnr;
    TextView txtUsername;
    String ProfilePic;
    String UserName;
    SharedPreferences mPrefs;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.education_drawer);
        imgGroup = (ImageButton) findViewById(R.id.GroupNav);
        imgFeed = (ImageButton) findViewById(R.id.FeedNav);
        imgFeed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i =  new Intent(Education.this,PostFeeds.class);
                startActivity(i);
            }
        });
        imgEducation = (ImageButton) findViewById(R.id.EducationNav);
        imgNotify = (ImageButton) findViewById(R.id.NotificationNav);
        imgSearch = (ImageButton) findViewById(R.id.SearchNav);
        imgProfile = (ImageButton) findViewById(R.id.ProfileNav);
        back=(ImageButton)findViewById(R.id.back);

        Admin = (ImageButton) findViewById(R.id.Admin);
        Teacher = (ImageButton) findViewById(R.id.Teacher);
        Parent = (ImageButton) findViewById(R.id.Parent);
        SharedPreferences sp = getSharedPreferences("userID", MODE_PRIVATE);
        String userIDa = sp.getString("M69User", null);
        M69User = userIDa;
        //Bind Profile picture and username-------------- Start
        new Loadprofile().execute();
        //Bind Profile picture and username-------------- End
        Admin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent Aintent = new Intent(Education.this, Admin_School_List.class);
                /*Admin.setImageResource(R.drawable.admin_icon_grey);
                Teacher.setImageResource(R.drawable.teacher_icon);
                Parent.setImageResource(R.drawable.parent_icon);*/
                startActivity(Aintent);


            }


        });

        Teacher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent Tintent = new Intent(Education.this, Teacher_Activity.class);
                /*Teacher.setImageResource(R.drawable.teacher_click);
                Parent.setImageResource(R.drawable.parent_icon);
                Admin.setImageResource(R.drawable.admin_icon);*/
                startActivity(Tintent);
            }
        });

        Parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent Pintent = new Intent(Education.this, Parent_Activity.class);
                /*Parent.setImageResource(R.drawable.parent_click);
                Teacher.setImageResource(R.drawable.teacher_icon);
                Admin.setImageResource(R.drawable.admin_icon);*/
                startActivity(Pintent);
            }
        });

        imgProfile = (ImageButton) findViewById(R.id.ProfileNav);
        mDrawerLayout =(DrawerLayout) findViewById(R.id.drawer_layout);

        imgProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
        });

    }
    public class Loadprofile extends  AsyncTask<Void,Void,Void>
    {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void v) {
            super.onPostExecute(v);
            //Bind Profile picture and username-------------- Start
            imgCnr = (ImageView) findViewById(R.id.imageView_1);
            txtUsername=(TextView)findViewById(R.id.txtUser_name);
            mPrefs = getSharedPreferences("Hbpref_1", MODE_PRIVATE);
            ProfilePic = mPrefs.getString("ProfilePic", null);
            UserName = mPrefs.getString("UserName",null);
            Picasso.with(getBaseContext())
                    .load(ProfilePic)
                    .into(imgCnr);
            txtUsername.setText(UserName);
            //Bind Profile picture and username-------------- End
        }

        @Override
        protected Void doInBackground(Void... params) {
            return null;
        }
    }
}

和我的抽屉页面设计

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <include
        layout="@layout/admin_school_menus"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/profile_header"
        app:menu="@menu/activity_main_drawer" />

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

标题布局设计

<?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="wrap_content"
    android:background="@color/white"
    android:gravity="bottom"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout1"
        android:orientation="horizontal"
        android:gravity="center"
        android:paddingTop="16dp">
    <ImageView
        android:id="@+id/imageView_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:background="@drawable/profile_circle" />
    </LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/LinearLayout2"
    android:layout_below="@+id/LinearLayout1"
    android:orientation="horizontal"
    android:gravity="center"
    android:paddingTop="16dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtUser_name"
        android:textSize="12dp"
        android:textColor="@color/blue"
        android:gravity="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout3"
        android:layout_below="@+id/LinearLayout2"
        android:orientation="horizontal"
        android:gravity="center"
        android:paddingTop="16dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txtUser_percentage"
            android:textSize="12dp"
            android:textColor="#a53692"
            android:gravity="center"
            android:text="Your Profile is 75% Completed"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout5"
        android:layout_below="@+id/LinearLayout3"
        android:orientation="horizontal"
        android:gravity="center"
        android:paddingTop="16dp">
        <ImageView
            android:id="@+id/imageView_Percentage"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:src="@drawable/loader"
            android:background="@drawable/profile_circle" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearLayout4"
        android:layout_below="@+id/LinearLayout5"
        android:orientation="horizontal"
        android:gravity="center"
        android:paddingTop="16dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txtUser_crt_profile"
            android:textSize="12dp"
            android:textColor="@color/blue"
            android:gravity="center"
            android:text="(Complete your profile and Earn more Points)"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    </LinearLayout>




</LinearLayout>

gradle这个

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.02"
    defaultConfig {
        applicationId "com.example.jishil1.hobnobzz"
        minSdkVersion 16

        targetSdkVersion 25
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        manifestPlaceholders = [manifestApplicationId: "${applicationId}",
                                onesignal_app_id: "d5add592-bb18-4fb8-978b-c017ecb68a2b",
                                onesignal_google_project_number: "383936065721"]
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile "com.android.support:support-core-utils:25.3.1"
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:multidex:1.0.0'
    compile "com.squareup.picasso:picasso:2.4.0"
    testCompile 'junit:junit:4.12'
    compile 'com.google.android.gms:play-services-appindexing:8.4.0'
    compile 'com.android.support:support-annotations:25.3.1'
    compile 'com.onesignal:OneSignal:2.+@aar'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.github.clans:fab:1.6.4'
    compile 'com.android.support:design:25.3.1'
    testCompile 'junit:junit:4.12'




    /*compile 'com.google.android.gms:play-services:8.1.0'
    compile 'com.google.android.gms:play-services-ads:8.1.0'
    compile 'com.google.android.gms:play-services-identity:8.1.0'
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
    compile 'com.onesignal:OneSignal:2.+@aar'
    compile 'com.google.android.gms:play-services-analytics:8.1.0'
    compile 'com.google.android.gms:play-services-location:8.1.0'*/


}

1 个答案:

答案 0 :(得分:0)

  

您可以尝试以下代码

View layout = getLayoutInflater().inflate(R.layout.profile_header,null);
imgCnr = (ImageView) layout.findViewById(R.id.imageView_1);