我有一个recyclerview,当点击它进入相应的活动时。我通过使用bundle绑定它们然后将它放入intent中来传递它们的位置。我从各个类中检索数据。这些活动中的每一个都有viewpager在其中那么我把那个位置放在活动中创建的ImagePagerAdapter
对象中。图像完全加载到viewpager中。我在这些活动中有一个按钮,它会进入不同的活动,当我尝试导航回到活动但是
应用程序崩溃。这是出现的错误:
错误:
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sabudaniel61.ktdc/com.example.sabudaniel61.ktdc.NandFragment}: java.lang.NullPointerException
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:153)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.os.Looper.loop(Looper.java:176)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5302)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: Caused by: java.lang.NullPointerException
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at com.example.sabudaniel61.ktdc.NandFragment.onCreate(NandFragment.java:66)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5326)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:153)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.os.Looper.loop(Looper.java:176)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5302)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-27 03:40:40.220 5043-5043/com.example.sabudaniel61.ktdc E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
RecyclerAdapter.java:
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
position = getAdapterPosition();
switch(position) {
case 0:
Intent intent = new Intent(context, AranyaFragment.class);
Bundle a = new Bundle();
a.putInt("Aranya", position);
intent.putExtras(a);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(context, BolgattyFragment.class);
Bundle b = new Bundle();
b.putInt("Bolg", position);
intent1.putExtras(b);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
break;
case 2:
Intent intent2 = new Intent(context, MascotFragment.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle c = new Bundle();
c.putInt("Mas", position);
intent2.putExtras(c);
context.startActivity(intent2);
break;
case 3:
Intent intent3 = new Intent(context, WaterScapes.class);
Bundle d = new Bundle();
d.putInt("Water", position);
intent3.putExtras(d);
intent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent3);
break;
case 4:
Intent intent4 = new Intent(context, SamudraFragment.class);
Bundle e = new Bundle();
e.putInt("Samudra", position);
intent4.putExtras(e);
intent4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent4);
break;
case 5:
Intent intent5 = new Intent(context, GoldenFragment.class);
Bundle f = new Bundle();
f.putInt("Gold", position);
intent5.putExtras(f);
intent5.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent5);
break;
case 6:
Intent intent6 = new Intent(context, RainFragment.class);
Bundle g = new Bundle();
g.putInt("Rain", position);
intent6.putExtras(g);
intent6.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent6);
break;
case 7:
Intent intent7 = new Intent(context, PeriyarFragment.class);
Bundle h = new Bundle();
h.putInt("Periyar", position);
intent7.putExtras(h);
intent7.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent7);
break;
case 8:
Intent intent8 = new Intent(context, PepperFragment.class);
Bundle i = new Bundle();
i.putInt("Pepper", position);
intent8.putExtras(i);
intent8.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent8);
break;
case 9:
Intent intent9 = new Intent(context, NandFragment.class);
Bundle j = new Bundle();
j.putInt("Nand", position);
intent9.putExtras(j);
intent9.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent9);
break;
case 10:
Intent intent10 = new Intent(context, TamarFragment.class);
Bundle k = new Bundle();
k.putInt("Tamar", position);
intent10.putExtras(k);
intent10.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent10);
break;
}
}
});
}
NandFragment.java
(实际上不是故障):在下面的代码中,我使用
Bundle b=getIntent().getExtras();
pos= b.getInt("Nand");
该按钮转到另一个活动,当我尝试返回应用程序崩溃时。错误发生在
pos= b.getInt("Nand");
public class NandFragment extends AppCompatActivity implements OnMapReadyCallback {
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nandanamlayout);
Bundle b=getIntent().getExtras();
pos= b.getInt("Nand");
Button button1;
button1=(Button) findViewById(R.id.nrrooms);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), NandRooms.class);
startActivity(intent);
}
});
ViewPager viewPager = (ViewPager) findViewById(R.id.vpfragment9);
ImagePagerAdapter pagerAdapter=new ImagePagerAdapter(this,pos);
viewPager.setAdapter(pagerAdapter);
viewPager.setPageTransformer(true, new ParallaxPageTransformer());}}
NandRooms.java:
public class NandRooms extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rooms);
Toolbar toolbar=(Toolbar) findViewById(R.id.rooms);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Rooms");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);}}
如果你还不明白,请告诉我......
答案 0 :(得分:1)
您的问题是,当您点击向上按钮时,您不会像第一次导航到NandFragment活动时那样发送任何额外内容。因此,当您使用向上按钮返回活动时,getIntent().getExtras()
将返回null,因为没有已发送的Extras。处理此问题的方法是覆盖向上操作按钮,然后处理"返回导航"你自己,沿途插入你的演员。为此,您需要覆盖onOptionsItemSelected()
并查找android.R.id.home
操作。然后你会做这样的事情:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
Intent upIntent = new Intent(this, NandFragment.class);
//put in your extras here...
startActivity(upIntent);
//return true to let the OS know you handled this action yourself
return true;
}
return false;
}
答案 1 :(得分:0)
在你获得额外价值之前,请先检查你是否先拥有它,否则如果它不存在你就会收到错误。
if (getIntent().hasExtra("Nand")) {
Bundle b=getIntent().getExtras();
pos= b.getInt("Nand");
}
else{
Log.d("TAG", "Bundle is not set");
}