简体   繁体   中英

ImageView is not working in android

I created an ImageView in this code.

activity_main.xml

<ImageView
    android:id="@+id/start_windows"
    android:src="@drawable/start_windows"
    android:scaleType = "centerCrop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:layout_margin="0px"
    android:layout_marginBottom="0px"
    android:layout_marginLeft="0px"
    android:layout_marginRight="0px"
    android:layout_marginStart="0px"
    android:layout_marginTop="0px" />

MainActivity.java

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        setContentView(R.layout.activity_main);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.start_windows), size.x, size.y, true);


        ImageView iv_background = (ImageView) findViewById(R.id.start_windows);
        iv_background.setImageBitmap(bmp);
    }
}

I ran my application in the emulator and got the error "Unfortunately, App has stopped". I connected my phone and ran again, yet I got the same error too "Unfortunately, App has stopped". How do I solve this problem?

This is my messages displayed in the LogCat:

   08-19 23:55:22.011    2032-2032/com.newapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.newapplication, PID: 2032
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.newapplicationp/com.newapplication.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5257)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
         Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
                at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:302)
                at android.app.Activity.requestWindowFeature(Activity.java:3605)
                at com.newapplication.MainActivity.onCreate(MainActivity.java:19)
                at android.app.Activity.performCreate(Activity.java:5990)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5257)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

对于AppCompat库,应在super.onCreate()之前调用requestFeature(...) super.onCreate()

Remember! Don't call setContentView() before requestFeature() .

Doing so will cause this exception:

 Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

This will fix your problem:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
     requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
...
...
...

I modified your code to be as follow, and it worked (put requestWindowFeature before super.onCreate:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.start_windows), size.x, size.y, true);


        ImageView iv_background = (ImageView)findViewById(R.id.start_windows);
        iv_background.setImageBitmap(bmp);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Please try!

i think it has nothing to do with your image view, try changing how you make your app go fullscreen by changing your app theme in the styles using this

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

or directly in the manifest in using this.

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

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