0

这可能是一个非常简单的错误,但我找不到它!如果我在布局中的 Button 之前有 ImageView,它想在启动时关闭应用程序!但如果我先有 Button 然后有 ImageView!?这可能是什么原因?我已经粘贴了一些代码,希望能告诉你错误是什么?感谢帮助!谢谢!

活动:

public class Activity_1 extends Activity {

ImageView imageView;

public final static String test ="Hello";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_1);


    Button btnImageMenu = (Button) findViewById(R.id.btnStartActivity_2);
    imageView = (ImageView) findViewById(R.id.full_image_view);

    registerForContextMenu(imageView);

    // Button to open activity with gridview
    btnImageMenu.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent i = new Intent("com.test.ACTIVITY_2"); // Alternative
            startActivityForResult(i, 1);
        }
    });
}

// Create context menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);

    menu.setHeaderTitle("Options for this menu"); // Title for the menu
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_1_menu, menu);
}

// Handle selected items from context menu
@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    // Open activity with gridview of thumbnails
    case R.id.menu_option_1:
        // Toast.makeText(Activity_1.this, "" + "Menu option 1", Toast.LENGTH_SHORT).show();

        // Activate activity 3 with listview of contacts
        Intent i = new Intent("com.test.ACTIVITY_3"); // Alternative
        startActivityForResult(i, 2);

        return true;

        // Open activity with listview of contacts
    case R.id.menu_option_2:
        Toast.makeText(Activity_1.this, "" + "Menu option 2",
                Toast.LENGTH_SHORT).show();
        return true;

    default:
        return super.onContextItemSelected(item);
    }
}

// Handle return values from different activities
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {

        if (requestCode == 1) {

            String imageId = data.getExtras().getString("imageId");

            Bitmap bitmap = BitmapFactory.decodeFile(imageId);
            imageView.setImageBitmap(bitmap);
        }

        else if(requestCode == 2) {
            String contact1 = data.getExtras().getString("contactName");
            String contact2 = data.getExtras().getString("test");
            Toast.makeText(Activity_1.this, "" + "Value from activity 3: " + contact1 + " " + contact2, Toast.LENGTH_SHORT).show();

        }
    }
}

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/full_image_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<Button
    android:id="@+id/btnStartActivity_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btnStartActivity_2" />

错误日志:

02-24 20:34:50.945: I/ApplicationPackageManager(13296): cscCountry is not German : NEE
02-24 20:34:50.976: D/AndroidRuntime(13296): Shutting down VM
02-24 20:34:50.984: W/dalvikvm(13296): threadid=1: thread exiting with uncaught exception (group=0x40018578)
02-24 20:34:51.000: E/AndroidRuntime(13296): FATAL EXCEPTION: main
02-24 20:34:51.000: E/AndroidRuntime(13296): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.assignment.two/com.test.assignment.two.Activity_1}: java.lang.ClassCastException: android.widget.ImageView
02-24 20:34:51.000: E/AndroidRuntime(13296):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at android.os.Looper.loop(Looper.java:130)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at java.lang.reflect.Method.invokeNative(Native Method)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at java.lang.reflect.Method.invoke(Method.java:507)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at dalvik.system.NativeStart.main(Native Method)
02-24 20:34:51.000: E/AndroidRuntime(13296): Caused by: java.lang.ClassCastException: android.widget.ImageView
02-24 20:34:51.000: E/AndroidRuntime(13296):    at com.test.assignment.two.Activity_1.onCreate(Activity_1.java:31)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-24 20:34:51.000: E/AndroidRuntime(13296):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-24 20:34:51.000: E/AndroidRuntime(13296):    ... 11 more
4

1 回答 1

0

也许没有错误..试试这个:

使用 android:src="yourimage" 为 Imageview 设置图像

项目 >> 清洁

这将清理您的项目并重建类。希望这能解决您的问题。

于 2013-02-24T18:38:43.430 回答