我想在" inntent()"中开始新的活动。 " buttonn()"中使用的函数功能。该应用程序工作正常,但当我单击按钮触发意图时,应用程序每次都崩溃。我已经尝试了不同的方法来启动意图并改变了Manifest中的一些东西,但它仍然没有。工作
这是我的MainActivity.java代码:
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
ListView list;
private TextView textView;
ImageButton floatButton;
static String category;
static String name;
Button button;
ArrayList<String> aaaa;
ArrayList<String> bbbbb;
ArrayList<String> cccc;
ArrayList<String> dddd;
ArrayList<String> display;
public static int[] pics = {R.drawable.no, R.drawable.yes};
Context context;
public void add() {
aaaa.add("Example");
aaaa.add("Example");
aaaa.add("Example");
bbbb.add("Example");
bbbb.add("Example");
bbbb.add("Example");
cccc.add("Example");
cccc.add("Example");
dddd.add("Example");
dddd.add("Example");
dddd.add("Example");
dddd.add("Example");
}
public void changeDisplay(ArrayList<String> a) {
display.clear();
for (int m = 0; m < a.size(); m++) {
String ep = a.get(m);
display.add(ep);
}
}
public void inntent() {
Intent intent = new Intent(context, GoalsActivity.class);
intent.putExtra("category", category);
intent.putExtra("name", name);
startActivity(intent);
}
public void butonn(View view) {
inntent();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
context = this;
category = "aaaa";
name = "";
aaaa = new ArrayList<String>();
bbbb = new ArrayList<String>();
cccc = new ArrayList<String>();
dddd = new ArrayList<String>();
display = new ArrayList<>();
add();
changeDisplay(aaaa);
list = (ListView)findViewById(R.id.list_view);
list.setAdapter(new CustomAdapter(this, display, pics));
bottomNavigationView = (BottomNavigationView)findViewById(R.id.BottomaNavigationBar);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.wg) {
changeDisplay(aaaa);
category = "aaaa";
((CustomAdapter) list.getAdapter()).notifyDataSetChanged();
} else if (item.getItemId() == R.id.qg) {
changeDisplay(bbbb);
category = "bbbb";
((CustomAdapter) list.getAdapter()).notifyDataSetChanged();
} else if (item.getItemId() == R.id.challenges) {
changeDisplay(cccc);
category = "cccc";
((CustomAdapter) list.getAdapter()).notifyDataSetChanged();
} else if (item.getItemId() == R.id.more) {
changeDisplay(dddd);
category = "dddd";
((CustomAdapter) list.getAdapter()).notifyDataSetChanged();
}
return false;
}
});
floatButton = (ImageButton)findViewById(R.id.imageButton);
floatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "TEST", Toast.LENGTH_SHORT).show();
}
});
}
这是我的清单:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GoalsActivity"
android:screenOrientation="portrait"></activity>
</application>
答案 0 :(得分:0)
在onCreate方法中给出以下代码。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, GoalsActivity.class);
intent.putExtra("category", category);
intent.putExtra("name", name);
startActivity(intent);
}