public class MainActivity extends Activity implements View.OnClickListener {
Button button_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_1 = findViewById(R.id.button_1);
button_1.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_1:
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
break;
}
}
}
2活动
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
String[] countries = {"Urugay", "Paraguay", "Jamaica", "Peru", "Mexico"};
int[] population = {6770000, 2300000, 500000, 6310000, 7000000};
Button btnSubmit;
int sum;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Map<String, Integer> countryData = new HashMap<>(countries.length);
for (int i = 0; i < countries.length; i++) {
countryData.put(countries[i], population[i]);
}
countryData.forEach((key, value) -> System.out.println(key + " -> " + value));
ListView countriesList = findViewById(R.id.countriesList);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_multiple_choice, countries);
countriesList.setAdapter(adapter);
int sum = 0;
for (int i = 0; i < population.length; i++)
{
sum += population[i];
}
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener((View.OnClickListener) this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(this, Main3Activity.class);
intent.putExtra("average", sum);
startActivity(intent);
}
}
3活动
public class Main3Activity extends AppCompatActivity {
TextView tvView;
int sum;
int defaultValue = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
tvView = (TextView) findViewById(R.id.tvView);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("average", 0);
tvView.setText("Avarage =: " + sum);
}
}
有日志
`09-23 12:32:10.647 1858-1858/com.example.admin.app_1 I/art: Late-enabling -
Xcheck:jni
09-23 12:32:10.648 1858-1858/com.example.admin.app_1 W/art: Unexpected CPU
variant for X86 using defaults: x86
09-23 12:32:11.454 1858-1858/com.example.admin.app_1 W/System: ClassLoader
referenced unknown path: /data/app/com.example.admin.app_1-1/lib/x86
09-23 12:32:11.478 1858-1858/com.example.admin.app_1 I/InstantRun: starting
instant run server: is main process
09-23 12:32:12.678 1858-1874/com.example.admin.app_1 E/libEGL:
load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library
"/system/lib/egl/libGLES_emulation.so" not found
09-23 12:32:13.500 1858-1874/com.example.admin.app_1 I/OpenGLRenderer:
Initialized EGL, version 1.4
09-23 12:32:23.288 1858-1858/com.example.admin.app_1 I/Choreographer:
Skipped 653 frames! The application may be doing too much work on its main
thread.
09-23 12:32:23.731 1858-1874/com.example.admin.app_1 E/EGL_emulation: tid
1874: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
09-23 12:32:23.735 1858-1874/com.example.admin.app_1 W/OpenGLRenderer:
Failed to set EGL_SWAP_BEHAVIOR on surface 0xcf2c0cc0, error=EGL_BAD_MATCH
09-23 12:32:25.207 1858-1858/com.example.admin.app_1 I/Choreographer:
Skipped 114 frames! The application may be doing too much work on its main
thread.
09-23 12:32:25.792 1858-1858/com.example.admin.app_1 I/Choreographer:
Skipped 34 frames! The application may be doing too much work on its main
thread.
09-23 12:38:30.845 1858-1858/com.example.admin.app_1 W/art: Before Android
4.1, method android.graphics.PorterDuffColorFilter
android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-23 12:38:35.052 1858-1858/com.example.admin.app_1 I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
at void android.support.v4.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
at void android.support.v7.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
at java.lang.Object java.lang.reflect.Constructor.newInstance0!(java.lang.Object[]) (Constructor.java:-2)
at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:430)
at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:645)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:787)
09-23 12:38:35.053 1858-1858/com.example.admin.app_1 I/art: at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:727)
at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:858)
at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:821)
at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:518)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:426)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:377)
at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
at void com.example.admin.app_1.Main2Activity.onCreate(android.os.Bundle) (Main2Activity.java:27)
at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6679)
at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1118)
at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2618)
at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2726)
at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1477)
at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
at void android.os.Looper.loop() (Looper.java:154)
at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6119)
at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:886)
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:776)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.admin.app_1-1/base.apk", zip file "/data/app/com.example.admin.app_1-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.admin.app_1-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.admin.app_1-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.admin.app_1-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.admin.app_1-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.admin.app_1-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.admin.app_1-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.admin.app_1-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.admin.app_1-1/split_lib_slice_7_ap
at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
at void android.support.v4.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
at void android.support.v7.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
at java.lang.Object java.lang.reflect.Constructor.newInstance0!(java.lang.Object[]) (Constructor.java:-2)
at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:430)
at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:645)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:787)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:727)
at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:858)
at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:821)
at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:518)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:426)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:377)
at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
at void com.example.admin.app_1.Main2Activity.onCreate(android.os.Bundle) (Main2Activity.java:27)
at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6679)
at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1118)
at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2618)
at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2726)
at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1477)
at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
at void android.os.Looper.loop() (Looper.java:154)
at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6119)
at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:886)
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:776)
09-23 12:38:35.674 1858-1858/com.example.admin.app_1 I/System.out: Peru -> 6310000
Paraguay -> 2300000
Mexico -> 7000000
Jamaica -> 500000
Urugay -> 6770000
09-23 12:38:35.678 1858-1858/com.example.admin.app_1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.app_1, PID: 1858
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.app_1/com.example.admin.app_1.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.admin.app_1.Main2Activity.onCreate(Main2Activity.java:39)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Android Studio不会替换代码中的任何错误
应用程序有一个可以选择的列表,每个项目都有一个int参数。用户应选择列表中的一些位置。之后,在第三个活动应用程序中显示所选职位的平均数量。
第一个活动没有问题。但是在选择mainactivity应用程序上的按钮后,该按钮将停止。