我有两个活动,MainActivity和SecondActivity。将int值从MainActivity传递给SecondActivity时,它变为“0”。我已尝试使用和不使用bundle,尝试了StackOverFlow上已有的各种解决方案,但没有去。
这是我的代码: MainActivity
final Intent intent = new Intent(MainActivity.this, SecondActivity.class);
final TextView textView = findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Bundle bundle = new Bundle();
bundle.putInt("category",9);
intent.putExtras(bundle);
startActivity(intent);
}
});
SecondActivity:
package com.example.app;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
ViewPager viewPager;
TabsPagerAdapter pagerAdapter;
private int mMedCategory = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// String stringCategory = getIntent().getStringExtra("category_string");
int medCategory = getIntent().getIntExtra("category_int", -1);
setMedCategory(medCategory);
viewPager = findViewById(R.id.viewpager);
pagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
}
public int getMedCategory(){
return mMedCategory; //This value goes to TabsPagerAdapter
}
public void setMedCategory(int i){
mMedCategory = i;
}
}
答案 0 :(得分:1)
使用Intent#getIntExtra()
从意图中检索整数值:
来自MainActivity
:
Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
myIntent.putExtra("category", 9);
MainActivity.this.startActivity(myIntent);
来自SecondActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
medCategory = intent.getIntExtra("category", -1);
}
请注意,如果无法找到密钥,这会将-1
的默认值分配给medCategory
。
答案 1 :(得分:1)
你可以试试这个:
<强> MainActivity 强>
Error: spawn EACCES
at exports._errnoException (util.js:1026:11)
at ChildProcess.spawn (internal/child_process.js:313:11)
at Object.exports.spawn (child_process.js:380:9)
at module.exports (/home/dothmobi/public_html/demo/drivzi_admin/materialadmin/material/node_modules/opn/index.js:75:24)
at Object.open (/home/dothmobi/public_html/demo/drivzi_admin/materialadmin/material/node_modules/browser-sync/lib/utils.js:186:9)
at Object.openBrowser (/home/dothmobi/public_html/demo/drivzi_admin/materialadmin/material/node_modules/browser-sync/lib/utils.js:165:23)
at EventEmitter.service:running (/home/dothmobi/public_html/demo/drivzi_admin/materialadmin/material/node_modules/browser-sync/lib/internal-events.js:42:23)
at emitOne (events.js:101:20)
at EventEmitter.emit (events.js:188:7)
at /home/dothmobi/public_html/demo/drivzi_admin/materialadmin/material/node_modules/browser-sync/lib/browser-sync.js:260:19
<强> SecondActivity 强>
TextView textView = findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("category_int", 9);
intent.putExtra("category_string", "9");
startActivity(intent);
}
});
您可以查看How do I pass data between Activities in Android application?
答案 2 :(得分:0)
使用此代码。删除final并尝试在onclick方法中初始化意图而不是在它之外。
在您的OnClick
Intent intent=new Intent(this,Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putInt("category",9);
intent.putExtras(bundle);
startActivity(intent);
在您的第二个活动中
public class Main2Activity extends AppCompatActivity {
private int mMedCategory =0;
public int getmMedCategory() {
return mMedCategory;
}
public void setmMedCategory(int mMedCategory) {
this.mMedCategory = mMedCategory;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
int demoCategory =bundle.getInt("category");
setmMedCategory(demoCategory);
}else {
Toast.makeText(this, "null", Toast.LENGTH_SHORT).show();
}
}
public void Show(View view) {
Toast.makeText(this, String.valueOf(getmMedCategory()), Toast.LENGTH_SHORT).show();
}
}
修改强>
为您创建了一个演示项目。显示方法是按钮的onclick方法。
答案 3 :(得分:0)
我不确定,但您可以将其用于第二项活动:
SecondActivity secondActivity = new SecondActivity();
if (secondActivity.getIntent.getExtras() != null)
{
int medCat = secondActivity.getIntent.getExtras().getInt("category");
}