如何使用导航抽屉更改活动?

时间:2016-01-04 11:48:04

标签: android android-intent android-activity android-navigation-drawer

我需要在我的Android应用程序上使用导航抽屉,但我遇到了一个问题:当我点击它时,我看到一个错误:不幸的是,你的应用已停止

这里有我使用的代码:

  public class Profiles extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener{


Button bLogout,etAddImage,bAllInfo;
TextView etName,etInfo;
ImageView etImage;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profiles);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


    etName = (TextView) findViewById(R.id.etName);
    etInfo = (TextView) findViewById(R.id.etInfo);
    etImage = (ImageView) findViewById(R.id.etImage);
    etAddImage = (Button) findViewById(R.id.etAddImage);
    bAllInfo = (Button) findViewById(R.id.bAllInfo);

    Bundle b= Profiles.this.getIntent().getExtras();
    String[] array=b.getStringArray(null);
    etName.setText(array[0] + " " + array[1]);

    if (savedInstanceState==null){
    getFragmentManager().beginTransaction()
    .add(R.id.container, new Fragment())
    .commit();
    }

    etAddImage.setOnClickListener(this);
    bAllInfo.setOnClickListener(this);

    etInfo.setText("Anni: " + array[2] + "\n" + "Email: " + array[3]);

}



@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.profiles, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
   /* Fragment fragment;
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    fragment = new ListFragment();
    int id = item.getItemId();*/

    switch(item.getItemId()){

        case R.id.profile:
                startActivity(new Intent(this,Profiles.class));
            break;
        case R.id.diet:
            break;
        case R.id.exercise:
            break;
        case R.id.info:
            break;
    }

   /* ft.replace(R.id.container, fragment);
    ft.addToBackStack(null);
    ft.commit();*/

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.etAddImage:
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
            break;
        case R.id.bAllInfo:
            startActivity(new Intent(this,AddInfo.class));
            break;
    }
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            etImage.setImageURI(selectedImageUri);
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
        return cursor.getString(column_index);
    }
}



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

Android工作室告诉我问题出在这个代码行中:

 Bundle b= Profiles.this.getIntent().getExtras();
        String[] array=b.getStringArray(null);
        etName.setText(array[0] + " " + array[1]);

同样在控制台中我看到:

enter image description here

我不知道问题是什么,有人可以帮助我吗?感谢

1 个答案:

答案 0 :(得分:0)

当您开始活动时,您不会发送额外内容 但是,基于您的堆栈跟踪,我理解在onCreate of Profiles活动中,您正在这样做:

waitpid

其中b为var str = "9087654321"; str.slice(0,3) + str.slice(3,-2).replace(/./g,'*') + str.slice(-2); // "908*****21" ,并在其上调用getStringArray(),将抛出 NPE

尽管onCreate的Profiles活动片段非常有用。