Use Image View to open new activity?

时间:2016-04-04 17:59:05

标签: java android android-studio

Im creating an app and I require an image to open up a new activity in android studios but it doesnt seem to work. Can anyone see any problems with the code below?

public class Menu extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
}
ImageView imgview = (ImageView) findViewById(R.id.table);
imgview.bringToFront();
imgview.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(Menu.this, Options.class);
        startActivity(myIntent);
    }
});
}

4 个答案:

答案 0 :(得分:1)

Move the ImageView and its code inside the onCreate() method.
Since you have a class named as Options.class A class from BitmapFactory.Options.class also exists, so please check your imports.

答案 1 :(得分:0)

Please rearrange you code to look like :

public class Menu extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    ImageView imgview = (ImageView) findViewById(R.id.table);
    imgview.bringToFront();
    imgview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(Menu.this, Options.class);
            startActivity(myIntent);
        }
    });
 }
}

Be sure that you Options class extend AppCompatActivity

public class Options extends AppCompatActivity {
**STUFF**
}

make sure that Options is declared in the AndroidManifiest.xml

<activity
android:name=".Options
android:theme="@style/AppTheme.NoActionBar">
</activity>

答案 2 :(得分:0)

Add the following code to your main activity:

public class MainActivity extends ActionBarActivity {

ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    iv = (ImageView)findViewById(R.id.imageView);
    iv.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view1){
            Intent myIntent = new Intent(MainActivity.this, Options.class);
            startActivity(myIntent);
        }
    });
}
}

Don't forget to get Intent in the Option.class activity:

public class Options extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_options);
    Intent intent0 = getIntent();
}
}

Also add android:clickable:"true" in XML code of the ImageView.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="156dp"
    android:src="@drawable/ss"
    android:clickable="true"/>

and Option should be declared in Menifest.xml as:

 </activity>
    <activity android:name=".Options"></activity>
</application>

答案 3 :(得分:0)

尝试ImageButton而不是ImageView

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
            imageButton.setImageResource(R.drawable.k);
            imageButton.setOnClickListener(this);

        }
  @Override
  public void onClick(View v) {
    Intent myIntent = new Intent(this, Options.class );
    startActivity(myIntent);

  }
}

在新的Activity XML中,试试这个。 不要忘记放置图像源(android:src =“@ drawable / k”)或者你会得到一个Nullpointerexception

<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="com.example.nirmesh.problem1.Options">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageview"
    android:src="@drawable/k"
    android:layout_centerInParent="true"/>