下面的所有代码都可以按需要工作。也就是说,直到我尝试将第二个 onClickListener 添加到另一个图像。我尝试添加的代码以及我尝试添加它的位置在下面的代码中列为注释。有人看到我在这里遗漏的任何东西吗?我觉得这个额外的实现应该没有任何问题。
公共类 MainActivity 扩展 Activity {
ImageView mImage;
//ImageView journal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImage = (ImageView)MainActivity.this.findViewById(R.id.Floaterimg);
//journal = (ImageView)MainActivity.this.findViewById(R.id.journbtn);
//journal.setOnClickListener(new View.OnClickListener() {
//@Override
//public void onClick (View v) {
//Intent i = new Intent(MainActivity.this,
//SecondActivity.class);
//startActivity(i);
//}
//});
final Handler randomizer = new Handler();
final Runnable rrandomizer = new Runnable() {
public void run() {
RelativeLayout.LayoutParams params = new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.topMargin = (int)(Math.random()*2000 + 1);
params.leftMargin = (int)(Math.random()*3000 + 1);
mImage.setLayoutParams(params);
randomizer.postDelayed(this, 5000);
}
};
rrandomizer.run();
mImage.setOnClickListener(new View.OnClickListener() {
int numClicks = 0;
@Override
public void onClick(View arg0) {
numClicks++;
if(numClicks > 5) {
Bitmap bitmap =
BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
ByteArrayOutputStream baos = new
ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
intent.putExtra("picture", b);
}
}
});
}
};
编辑:
按下按钮后来自 logcat 的错误:
10-18 14:17:32.529: E/InputDispatcher(17677): Event_report_payload: Failed to write keypress event to kernel, error = 5
10-18 14:17:32.529: E/dhcpcd(7483): open_udp_socket: No such device
10-18 14:17:32.549: E/dhcpcd(7483): open_socket: No such file or directory
10-18 14:17:32.549: E/dhcpcd(7483): send_raw_packet: No such file or directory
10-18 14:17:32.609: E/InputDispatcher(17677): Event_report_payload: Failed to write keypress event to kernel, error = 5
10-18 14:17:32.759: E/AndroidRuntime(26780): Uncaught handler: thread main exiting due to uncaught exception
10-18 14:17:32.759: E/Monkey(26780): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
10-18 14:17:32.809: E/AndroidRuntime(26780): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
10-18 14:17:32.809: E/AndroidRuntime(26780): FATAL EXCEPTION: main
10-18 14:17:32.809: E/AndroidRuntime(26780): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.SecondActivity}: java.lang.NullPointerException
10-18 14:17:32.809: E/AndroidRuntime(26780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
10-18 14:17:32.809: E/AndroidRuntime(26780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
10-18 14:17:32.809: E/AndroidRuntime(26780): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-18 14:17:32.809: E/AndroidRuntime(26780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:945)
10-18 14:17:32.809: E/AndroidRuntime(26780): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 14:17:32.809: E/AndroidRuntime(26780): at android.os.Looper.loop(Looper.java:130)
10-18 14:17:32.809: E/AndroidRuntime(26780): at android.app.ActivityThread.main(ActivityThread.java:3719)
10-18 14:17:32.809: E/AndroidRuntime(26780): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 14:17:32.809: E/AndroidRuntime(26780): at java.lang.reflect.Method.invoke(Method.java:507)
10-18 14:17:32.809: E/AndroidRuntime(26780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
10-18 14:17:32.809: E/AndroidRuntime(26780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
10-18 14:17:32.809: E/AndroidRuntime(26780): at dalvik.system.NativeStart.main(Native Method)
10-18 14:17:32.809: E/AndroidRuntime(26780): Caused by: java.lang.NullPointerException
10-18 14:17:32.809: E/AndroidRuntime(26780): at com.example.inkuesttest.SecondActivity.onCreate(SecondActivity.java:18)
10-18 14:17:32.809: E/AndroidRuntime(26780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 14:17:32.809: E/AndroidRuntime(26780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
10-18 14:17:32.809: E/AndroidRuntime(26780): ... 11 more
10-18 14:17:32.829: E/AndroidRuntime(26780): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
10-18 14:17:32.869: E/ActivityManager(17677): set systemprop back to original value~
编辑:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle extras = getIntent().getExtras();
//line18
byte[] b = extras.getByteArray("picture");
//end line18
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
}
}