简体   繁体   中英

Use Image View to open new activity?

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);
    }
});
}

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.

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>

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>

try ImageButton instead of 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);

  }
}

In your new Activity XML, try this. Dont Forget to put the image source(android:src="@drawable/k") or you will get a 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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM