我在使用按钮在 2 张图像之间交叉淡入淡出时遇到问题。该按钮未实现,但问题是我什至无法加载图像视图。Eclipse 不会在 java 代码中给出错误,但是当我尝试运行应用程序时,它会崩溃。我在下面包含了 MainActivity.java、LogCat 和 XML 文件。
public class MainActivity extends ActionBarActivity {
private View imageView1;
private View imageView2;
private int mShortAnimationDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
imageView1 = findViewById(R.id.imageView1);
imageView2 = findViewById(R.id.imageView2);
// Initially hide the content view.
imageView2.setVisibility(View.GONE);
// Retrieve and cache the system's default "short" animation time.
mShortAnimationDuration = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
private void crossfade() {
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
imageView1.setAlpha(0f);
imageView1.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
imageView1.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(null);
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
imageView2.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
imageView2.setVisibility(View.GONE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
我的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ca.mohawk.you.lab6a.MainActivity$PlaceholderFragment" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/desert" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/desert" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:text="@string/button_click" />
</RelativeLayout>
日志猫:
04-07 21:51:13.463: D/AndroidRuntime(1099): Shutting down VM
04-07 21:51:13.473: W/dalvikvm(1099): threadid=1: thread exiting with uncaught exception (group=0xb3ab2ba8)
04-07 21:51:13.483: E/AndroidRuntime(1099): FATAL EXCEPTION: main
04-07 21:51:13.483: E/AndroidRuntime(1099): Process: ca.mohawk.you.lab6a, PID: 1099
04-07 21:51:13.483: E/AndroidRuntime(1099): java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.mohawk.you.lab6a/ca.mohawk.you.lab6a.MainActivity}: java.lang.NullPointerException
04-07 21:51:13.483: E/AndroidRuntime(1099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-07 21:51:13.483: E/AndroidRuntime(1099): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 21:51:13.483: E/AndroidRuntime(1099): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-07 21:51:13.483: E/AndroidRuntime(1099): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 21:51:13.483: E/AndroidRuntime(1099): at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 21:51:13.483: E/AndroidRuntime(1099): at android.os.Looper.loop(Looper.java:136)
04-07 21:51:13.483: E/AndroidRuntime(1099): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 21:51:13.483: E/AndroidRuntime(1099): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 21:51:13.483: E/AndroidRuntime(1099): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 21:51:13.483: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 21:51:13.483: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 21:51:13.483: E/AndroidRuntime(1099): at dalvik.system.NativeStart.main(Native Method)
04-07 21:51:13.483: E/AndroidRuntime(1099): Caused by: java.lang.NullPointerException
04-07 21:51:13.483: E/AndroidRuntime(1099): at ca.mohawk.you.lab6a.MainActivity.onCreate(MainActivity.java:36)
04-07 21:51:13.483: E/AndroidRuntime(1099): at android.app.Activity.performCreate(Activity.java:5231)
04-07 21:51:13.483: E/AndroidRuntime(1099): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-07 21:51:13.483: E/AndroidRuntime(1099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-07 21:51:13.483: E/AndroidRuntime(1099): ... 11 more
04-07 21:54:14.453: D/AndroidRuntime(1164): Shutting down VM
04-07 21:54:14.453: W/dalvikvm(1164): threadid=1: thread exiting with uncaught exception (group=0xb3ab2ba8)
04-07 21:54:14.463: E/AndroidRuntime(1164): FATAL EXCEPTION: main
04-07 21:54:14.463: E/AndroidRuntime(1164): Process: ca.mohawk.you.lab6a, PID: 1164
04-07 21:54:14.463: E/AndroidRuntime(1164): java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.mohawk.you.lab6a/ca.mohawk.you.lab6a.MainActivity}: java.lang.NullPointerException
04-07 21:54:14.463: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-07 21:54:14.463: E/AndroidRuntime(1164): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 21:54:14.463: E/AndroidRuntime(1164): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-07 21:54:14.463: E/AndroidRuntime(1164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 21:54:14.463: E/AndroidRuntime(1164): at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 21:54:14.463: E/AndroidRuntime(1164): at android.os.Looper.loop(Looper.java:136)
04-07 21:54:14.463: E/AndroidRuntime(1164): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 21:54:14.463: E/AndroidRuntime(1164): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 21:54:14.463: E/AndroidRuntime(1164): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 21:54:14.463: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 21:54:14.463: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 21:54:14.463: E/AndroidRuntime(1164): at dalvik.system.NativeStart.main(Native Method)
04-07 21:54:14.463: E/AndroidRuntime(1164): Caused by: java.lang.NullPointerException
04-07 21:54:14.463: E/AndroidRuntime(1164): at ca.mohawk.you.lab6a.MainActivity.onCreate(MainActivity.java:36)
04-07 21:54:14.463: E/AndroidRuntime(1164): at android.app.Activity.performCreate(Activity.java:5231)
04-07 21:54:14.463: E/AndroidRuntime(1164): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-07 21:54:14.463: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-07 21:54:14.463: E/AndroidRuntime(1164): ... 11 more
04-07 21:54:17.973: I/Process(1164): Sending signal. PID: 1164 SIG: 9
04-07 22:01:16.333: D/AndroidRuntime(1184): Shutting down VM
04-07 22:01:16.333: W/dalvikvm(1184): threadid=1: thread exiting with uncaught exception (group=0xb3ab2ba8)
04-07 22:01:16.353: E/AndroidRuntime(1184): FATAL EXCEPTION: main
04-07 22:01:16.353: E/AndroidRuntime(1184): Process: ca.mohawk.you.lab6a, PID: 1184
04-07 22:01:16.353: E/AndroidRuntime(1184): java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.mohawk.you.lab6a/ca.mohawk.you.lab6a.MainActivity}: java.lang.NullPointerException
04-07 22:01:16.353: E/AndroidRuntime(1184): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-07 22:01:16.353: E/AndroidRuntime(1184): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 22:01:16.353: E/AndroidRuntime(1184): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-07 22:01:16.353: E/AndroidRuntime(1184): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 22:01:16.353: E/AndroidRuntime(1184): at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 22:01:16.353: E/AndroidRuntime(1184): at android.os.Looper.loop(Looper.java:136)
04-07 22:01:16.353: E/AndroidRuntime(1184): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 22:01:16.353: E/AndroidRuntime(1184): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 22:01:16.353: E/AndroidRuntime(1184): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 22:01:16.353: E/AndroidRuntime(1184): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 22:01:16.353: E/AndroidRuntime(1184): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 22:01:16.353: E/AndroidRuntime(1184): at dalvik.system.NativeStart.main(Native Method)
04-07 22:01:16.353: E/AndroidRuntime(1184): Caused by: java.lang.NullPointerException
04-07 22:01:16.353: E/AndroidRuntime(1184): at ca.mohawk.you.lab6a.MainActivity.onCreate(MainActivity.java:36)
04-07 22:01:16.353: E/AndroidRuntime(1184): at android.app.Activity.performCreate(Activity.java:5231)
04-07 22:01:16.353: E/AndroidRuntime(1184): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-07 22:01:16.353: E/AndroidRuntime(1184): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-07 22:01:16.353: E/AndroidRuntime(1184): ... 11 more
04-07 22:01:59.343: I/Process(1184): Sending signal. PID: 1184 SIG: 9
04-07 22:02:01.843: D/AndroidRuntime(1205): Shutting down VM
04-07 22:02:01.843: W/dalvikvm(1205): threadid=1: thread exiting with uncaught exception (group=0xb3ab2ba8)
04-07 22:02:01.853: E/AndroidRuntime(1205): FATAL EXCEPTION: main
04-07 22:02:01.853: E/AndroidRuntime(1205): Process: ca.mohawk.you.lab6a, PID: 1205
04-07 22:02:01.853: E/AndroidRuntime(1205): java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.mohawk.you.lab6a/ca.mohawk.you.lab6a.MainActivity}: java.lang.NullPointerException
04-07 22:02:01.853: E/AndroidRuntime(1205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-07 22:02:01.853: E/AndroidRuntime(1205): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 22:02:01.853: E/AndroidRuntime(1205): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-07 22:02:01.853: E/AndroidRuntime(1205): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 22:02:01.853: E/AndroidRuntime(1205): at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 22:02:01.853: E/AndroidRuntime(1205): at android.os.Looper.loop(Looper.java:136)
04-07 22:02:01.853: E/AndroidRuntime(1205): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 22:02:01.853: E/AndroidRuntime(1205): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 22:02:01.853: E/AndroidRuntime(1205): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 22:02:01.853: E/AndroidRuntime(1205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 22:02:01.853: E/AndroidRuntime(1205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 22:02:01.853: E/AndroidRuntime(1205): at dalvik.system.NativeStart.main(Native Method)
04-07 22:02:01.853: E/AndroidRuntime(1205): Caused by: java.lang.NullPointerException
04-07 22:02:01.853: E/AndroidRuntime(1205): at ca.mohawk.you.lab6a.MainActivity.onCreate(MainActivity.java:36)
04-07 22:02:01.853: E/AndroidRuntime(1205): at android.app.Activity.performCreate(Activity.java:5231)
04-07 22:02:01.853: E/AndroidRuntime(1205): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-07 22:02:01.853: E/AndroidRuntime(1205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-07 22:02:01.853: E/AndroidRuntime(1205): ... 11 more
04-07 22:02:51.663: D/AndroidRuntime(1251): Shutting down VM
04-07 22:02:51.663: W/dalvikvm(1251): threadid=1: thread exiting with uncaught exception (group=0xb3ab2ba8)
04-07 22:02:51.673: E/AndroidRuntime(1251): FATAL EXCEPTION: main
04-07 22:02:51.673: E/AndroidRuntime(1251): Process: ca.mohawk.you.lab6a, PID: 1251
04-07 22:02:51.673: E/AndroidRuntime(1251): java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.mohawk.you.lab6a/ca.mohawk.you.lab6a.MainActivity}: java.lang.NullPointerException
04-07 22:02:51.673: E/AndroidRuntime(1251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-07 22:02:51.673: E/AndroidRuntime(1251): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-07 22:02:51.673: E/AndroidRuntime(1251): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-07 22:02:51.673: E/AndroidRuntime(1251): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-07 22:02:51.673: E/AndroidRuntime(1251): at android.os.Handler.dispatchMessage(Handler.java:102)
04-07 22:02:51.673: E/AndroidRuntime(1251): at android.os.Looper.loop(Looper.java:136)
04-07 22:02:51.673: E/AndroidRuntime(1251): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 22:02:51.673: E/AndroidRuntime(1251): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 22:02:51.673: E/AndroidRuntime(1251): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 22:02:51.673: E/AndroidRuntime(1251): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 22:02:51.673: E/AndroidRuntime(1251): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 22:02:51.673: E/AndroidRuntime(1251): at dalvik.system.NativeStart.main(Native Method)
04-07 22:02:51.673: E/AndroidRuntime(1251): Caused by: java.lang.NullPointerException
04-07 22:02:51.673: E/AndroidRuntime(1251): at ca.mohawk.you.lab6a.MainActivity.onCreate(MainActivity.java:36)
04-07 22:02:51.673: E/AndroidRuntime(1251): at android.app.Activity.performCreate(Activity.java:5231)
04-07 22:02:51.673: E/AndroidRuntime(1251): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-07 22:02:51.673: E/AndroidRuntime(1251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-07 22:02:51.673: E/AndroidRuntime(1251): ... 11 more