【问题标题】:Blank screen on Activity start even with correct onCreate() and setContentView()即使使用正确的 onCreate() 和 setContentView(),Activity 上的空白屏幕也会启动
【发布时间】:2017-12-09 12:58:29
【问题描述】:

每当我在单击 button4 按钮时尝试从 Bacon1.java 启动 PlayListActivity.java 时都会出现空白屏幕em> 或 button_audio 。我认为 onCreate() 和 setContentView() 没有什么问题。但是我仍然不明白为什么每次启动该活动时都会出现空白屏幕。如何解决这个问题?

这是启动器活动:Bacon1.java

package com.example.dell_1.myapp3;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Toast;
import java.io.File;


public class Bacon1 extends Activity {

    private static int RESULT_LOAD_IMAGE = 1;
    private static int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;

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



                    public void onClick3(View v) {
                        buttonClicked(v);

                        Intent o = new Intent(this,PlayListActivity.class);
                        startActivity(o);
    }


    private void buttonClicked(View view) {

        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            Snackbar.make(view, "Permission not Granted, Requesting permission.", Snackbar.LENGTH_LONG).show();
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {

                Snackbar.make(view, "We need permission to internal storage for displaying songs", Snackbar.LENGTH_LONG).show();

            } else {

                Snackbar.make(view, "Allow myapp3 to access this device's internal storage", Snackbar.LENGTH_LONG).show();

                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
    }


    public void onClick2(View view) {

        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }


    public void onClick4(View view) {

        Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
        File file = Environment.getExternalStorageDirectory();
        viewIntent1.setDataAndType(Uri.fromFile(file), "video/*");
        startActivity(Intent.createChooser(viewIntent1, null));
    }

    public void onClick5(View view) {

        Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
        File file = Environment.getExternalStorageDirectory();
        viewIntent1.setDataAndType(Uri.fromFile(file), "zip/*");
        startActivity(Intent.createChooser(viewIntent1, null));
    }

    public void onClick6(View view) {

        Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
        File file = Environment.getExternalStorageDirectory();
        viewIntent1.setDataAndType(Uri.fromFile(file), "text/*");
        startActivity(Intent.createChooser(viewIntent1, null));
    }

    public void onClick7(View view) {

        Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
        File file = Environment.getExternalStorageDirectory();
        viewIntent1.setDataAndType(Uri.fromFile(file), "application/*");
        startActivity(Intent.createChooser(viewIntent1, null));
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(Bacon1.this, "WRITE_CONTACTS granted", Toast.LENGTH_SHORT)
                            .show();

                } else {

                    Toast.makeText(Bacon1.this, "WRITE_CONTACTS Denied", Toast.LENGTH_SHORT)
                            .show();

                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    }

这是对应的xml:activity_bacon1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dell_1.myapp3.Bacon1">

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </ImageView>


    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="134dp"
        android:layout_marginStart="134dp"
        android:layout_marginTop="63dp"
        android:onClick="onClick2"
        android:text="@string/button_images" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_alignStart="@+id/button3"
        android:layout_below="@+id/button3"
        android:layout_marginTop="22dp"
        android:onClick="onClick3"
        android:text="@string/button_audio" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button4"
        android:layout_alignStart="@+id/button4"
        android:layout_below="@+id/button4"
        android:layout_marginTop="20dp"
        android:onClick="onClick4"
        android:text="@string/button_videos" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button5"
        android:layout_alignStart="@+id/button5"
        android:layout_below="@+id/button5"
        android:layout_marginTop="16dp"
        android:onClick="onClick5"
        android:text="@string/button_zip" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button6"
        android:layout_alignStart="@+id/button6"
        android:layout_below="@+id/button6"
        android:layout_marginTop="19dp"
        android:onClick="onClick7"
        android:text="@string/button_apps" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:text="@string/button_documents"
        android:layout_below="@+id/button7"
        android:layout_alignLeft="@+id/button7"
        android:onClick="onClick6"
        android:layout_alignStart="@+id/button7" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="@string/text_internal"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="99dp"
        android:layout_marginStart="99dp" />
</RelativeLayout>

这是启动的Activity:PlayListActivity.java

package com.example.dell_1.myapp3;


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;

public class PlayListActivity extends ListActivity {
    // Songs list
    public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

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

        ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();


        SongsManager plm = new SongsManager();
        // get all songs from sdcard
        this.songsList = plm.getPlayList();

        // looping through playlist
        for (int i = 0; i < songsList.size(); i++) {
            // creating new HashMap
            HashMap<String, String> song = songsList.get(i);

            // adding HashList to ArrayList
            songsListData.add(song);
        }

        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, songsListData,
                R.layout.playlist_item, new String[]{"songTitle"}, new int[]{
                R.id.songTitle});

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();
        // listening to single listitem click
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // getting listitem index
                int songIndex = position;

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        AndroidBuildingMusicPlayerActivity.class);
                // Sending songIndex to PlayerActivity
                in.putExtra("songIndex", songIndex);
                setResult(100, in);
                // Closing PlayListView
                finish();
            }
        });
    }


}

这是对应的xml文件:activity_play_list

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#242424"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

【问题讨论】:

    标签: java android xml user-interface android-activity


    【解决方案1】:

    把你的 Bacon1 类改成这样

    package com.example.dell_1.myapp3;
    
    import android.Manifest;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.os.Environment;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.view.View;
    import android.widget.Toast;
    import java.io.File;
    
    
    public class Bacon1 extends Activity {
    
        private static int RESULT_LOAD_IMAGE = 1;
        private static int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_bacon1);
        }
    
    
    
                        public void onClick3(View v) {
                            buttonClicked(v);
    //We are going to call from buttonClicked method and onRequestPermissionsResult
                           // Intent o = new Intent(this,PlayListActivity.class);
                            //startActivity(o);
        }
    
    
        private void buttonClicked(View view) {
    
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
    
                Snackbar.make(view, "Permission not Granted, Requesting permission.", Snackbar.LENGTH_LONG).show();
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.READ_EXTERNAL_STORAGE)) {
    
                    Snackbar.make(view, "We need permission to internal storage for displaying songs", Snackbar.LENGTH_LONG).show();
    
                } else {
    
                    Snackbar.make(view, "Allow myapp3 to access this device's internal storage", Snackbar.LENGTH_LONG).show();
    
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                            MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
    
                    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                    // app-defined int constant. The callback method gets the
                    // result of the request.
                }
            }
    else
    {
     Intent o = new Intent(this,PlayListActivity.class);
                            startActivity(o);
    }
        }
    
    
        public void onClick2(View view) {
    
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    
            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    
    
        public void onClick4(View view) {
    
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            File file = Environment.getExternalStorageDirectory();
            viewIntent1.setDataAndType(Uri.fromFile(file), "video/*");
            startActivity(Intent.createChooser(viewIntent1, null));
        }
    
        public void onClick5(View view) {
    
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            File file = Environment.getExternalStorageDirectory();
            viewIntent1.setDataAndType(Uri.fromFile(file), "zip/*");
            startActivity(Intent.createChooser(viewIntent1, null));
        }
    
        public void onClick6(View view) {
    
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            File file = Environment.getExternalStorageDirectory();
            viewIntent1.setDataAndType(Uri.fromFile(file), "text/*");
            startActivity(Intent.createChooser(viewIntent1, null));
        }
    
        public void onClick7(View view) {
    
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            File file = Environment.getExternalStorageDirectory();
            viewIntent1.setDataAndType(Uri.fromFile(file), "application/*");
            startActivity(Intent.createChooser(viewIntent1, null));
        }
    
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            switch (requestCode) {
                case 1: {
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        Toast.makeText(Bacon1.this, "WRITE_CONTACTS granted", Toast.LENGTH_SHORT)
                                .show();
     Intent o = new Intent(this,PlayListActivity.class);
                            startActivity(o);
    
                    } else {
    
                        Toast.makeText(Bacon1.this, "WRITE_CONTACTS Denied Please give permission from Settings->Apps->YourAppName>", Toast.LENGTH_SHORT)
                                .show();
    //Or here you can call setting screen programmatically
                    }
                    return;
                }
    
                // other 'case' lines to check for other
                // permissions this app might request
            }
        }
    
        }
    

    希望对你有帮助..

    【讨论】:

    • 什么都没变,还是一样!!
    • 我已经在两个不同的位置改变了意图调用。请看我在那里评论过..
    • 再一次,它没有改变任何东西。日志猫也没有。你在你的设备上测试过吗?
    • 改变意图调用的位置似乎不会产生任何影响
    猜你喜欢
    • 1970-01-01
    • 2018-03-18
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多