【问题标题】:I get blank activity opened when starting new activity开始新活动时打开了空白活动
【发布时间】:2018-04-11 18:36:08
【问题描述】:

我正在使用this library 进行多张图片选择。用户选择完照片后,另一个活动 (current_location.class) 应该会开始。

这是我的代码:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OPEN_MEDIA_PICKER) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK && data != null) {
                selectionResult = data.getStringArrayListExtra("result");
                Intent intent = new Intent(getActivity(), currentLocation.class);
                intent.putExtra("selectedMedia", selectionResult);
                startActivity(intent);
            }
        }
    }

新活动已成功启动,但为空白!

下面是我的currentLocation.java活动:

public class currentLocation extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.current_location);
    }
}

这里是current_location.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_view">
    
</android.support.constraint.ConstraintLayout>

它应该有背景颜色,我也尝试添加按钮,但我总是启动空白活动

问题出在我开始新活动的方式上吗?还是从我将xml 布局设置为currentLocation 类的方式?

【问题讨论】:

  • 粘贴你的@drawable/bg_view文件
  • @Source 我们需要查看 bg_view 以确保我们知道为什么什么都没有显示。
  • @Yashasvi 我在我的片段背景中使用@drawable/bg_view 并且它有效,当我在 current_location.xml 上看到设计预览时,我可以看到应用的背景
  • @WadeWilson 背景在我的片段中工作,我可以在 current_location.xml 的预览中看到它。如果它与其他视图一起使用,文件不应该没问题吗?

标签: android xml android-studio android-intent android-activity


【解决方案1】:

尝试更改您的 onCreate 方法。 请改用以下内容:

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

【讨论】:

    【解决方案2】:

    也许没关系,但我会尝试两件事来降低出错的可能性。

    1. 如果您从活动结果数据意图中获得 StringArrayListExtra,我会在意图上使用 putStringArrayListExtra()

    2. 在创建新意图时,我会使用特定的活动/片段上下文。
      例如。新意图(className.this,currentLocation.class);
      或 new Intent(className.this.getActivity(), currentLocation.class);

    【讨论】:

      【解决方案3】:

      替换Intent intent = new Intent(getActivity(), currentLocation.class);
      与
      Intent intent = new Intent(this.getActivity(), currentLocation.class);


      还要检查 &lt;activity android:name=".currentLocation"&gt; 是否在清单中。

      【讨论】:

      • 当我尝试 'this' 而不是 'getActivity' 时,它会显示一个错误。我的清单中有
      • 错误是'cannot resolve constructor PostActivity, currentLocation'
      猜你喜欢
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 2012-03-04
      相关资源
      最近更新 更多