我照着网上的代码打 系统提示我错误(提示说:无法在空对象引用上启动活动组件信息)代码如下:
package com.example.fragementtest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn1;
Button btn2;
Intent a,b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.button1);
btn1.setOnClickListener(new BL());
btn2 = findViewById(R.id.button2);
btn2.setOnClickListener(new BL());
replaceFragment(new RightFra());
}
private class BL implements View.OnClickListener{
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
a = new Intent(MainActivity.this,Another.class);
startActivity(a);
break;
case R.id.button2:
b = new Intent(MainActivity.this,RightFra.class);
startActivity(b);
break;
default:
break;
}
}
}
private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.commit();
}
}
哪位大神可以指导一下我,看看这个代码错在哪里,怎么修改