片段按钮导致backstack错误

时间:2018-05-02 01:38:24

标签: android android-fragments android-volley

我正在尝试创建一个应用程序,它将使用volley从服务器获取一些信息。您应该输入您的信息,然后点击底部的登录信息,但是因为某些原因它会一直崩溃。

首先,当点击按钮时,它会崩溃并且logcat是: https://pastebin.com/deQi874r

主要活动代码:

public class UniqueProductFields
{
    public IEnumerable<string> Products { get; set; }
    public IEnumerable<string> Categories { get; set; }
    public IEnumerable<string> Subcategories { get; set; }
}   

登录片段(按钮所在的位置):

public class MainActivity extends AppCompatActivity {

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

    Toolbar toolbar = (Toolbar)findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);

    TextView textView = (TextView)findViewById(R.id.currentFragDisplay);
    textView.setText("My Profile");

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.fragmentContainer, new Personal_Profile_Fragment());
    fragmentTransaction.commit();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.toolbar_menu, menu);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int item_id = item.getItemId();
    switch (item_id) {
        case R.id.accDetails:
            swapFragment(item_id);
            return true;
        case R.id.personalProfileLookup:
            swapFragment(item_id);
            return true;
        case R.id.favoriteCharacters:
            swapFragment(item_id);
            return true;
        case R.id.friendProfileLookup:
            swapFragment(item_id);
            return true;
        case R.id.tipsAndTricks:
            swapFragment(item_id);
            return true;

    }
    return false;
}

private void swapFragment(int fragmentID){

    TextView tracker = (TextView) findViewById(R.id.currentFragDisplay);

    Fragment fragment = null;
    if(fragmentID == R.id.personalProfileLookup) {
        tracker.setText("My Profile");
        fragment = new Personal_Profile_Fragment();
    } else if(fragmentID == R.id.favoriteCharacters) {
        tracker.setText("Favorite Characters");
        fragment = new Favorite_Characters_Fragment();
    } else if(fragmentID == R.id.friendProfileLookup) {
        tracker.setText("Friend Lookup");
        fragment = new Friend_Profile_Fragment();
    } else if(fragmentID == R.id.tipsAndTricks) {
        tracker.setText("Tips and Tricks");
        fragment = new TipsandTricks_Fragment();
    } else if(fragmentID == R.id.accDetails) {
        tracker.setText("Log In");
        fragment = new loginFragment();
    }


    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragmentContainer, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}

}

1 个答案:

答案 0 :(得分:0)

问题在于您登录点击。您正在传递按钮而不是

中的视图
login(v);

您传递的V是按钮的实例,而不是视图。所以这个

username = v.findViewById(R.id.enteredUsername).toString();
password = v.findViewById(R.id.enteredPassword).toString();

将引用空指针。希望这有帮助