【问题标题】:onCreate() is not called未调用 onCreate()
【发布时间】:2018-02-10 10:17:02
【问题描述】:

这是我正在使用的代码

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

    showVoiceText.setText("I am working");
}

但它似乎没有被调用...它从不显示“我正在工作”

这是我的 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.phoenix.coreai.HomeActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Core A.I."
    android:id="@+id/showVoiceOutput"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enable voice control"
    tools:layout_editor_absoluteX="88dp"
    tools:layout_editor_absoluteY="167dp" />

</android.support.constraint.ConstraintLayout>

这不是我的主要活动,我的主要活动只是播放一个小 mp4 文件的介绍

这是主要的活动代码

package com.example.phoenix.coreai;


import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    VideoView view;
    private static int SPLASH_TIME_OUT = 2700;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view = (VideoView) findViewById(R.id.videoView);
        videoPlay(view);
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run(){
                Intent homeIntent = new Intent(MainActivity.this, HomeActivity.class);
                startActivity(homeIntent);
                finish();
            }
        },SPLASH_TIME_OUT);
    }

    public void videoPlay(View v){

        String videoPath = "android.resource://com.example.phoenix.coreai/" + R.raw.spashscreen;
        Uri uri = Uri.parse(videoPath);
        view.setVideoURI(uri);
        view.start();
    }
}

这是主要活动的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context="com.example.phoenix.coreai.MainActivity">


    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginTop="-27dp"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="0dp">

    </VideoView>


</LinearLayout>

我无法通过我的手机调试应用程序,它有一个自定义 rom,android studio 无法识别它

假设我想从 onCreate 调用这个 void

private void musicScan(){
    File f = new File(path);
    File file[] = f.listFiles();
    fileArray = new String[file.length];
    for (int i = 0; i < file.length; ++i){
        fileArray[i] = file[i].getName();
    }
}

它似乎没有执行我正在调用的 void

【问题讨论】:

  • 你能声明一个变量吗TextView showVoiceText;
  • 我们真的可以用这么少的信息提供帮助,您是否尝试调试以查看是否通过这部分?那是什么活动?你的主要活动是什么?是一样的吗?你在叫另一个吗?你的 XML 文件怎么样?它有设置文本的正确元素吗?
  • 你能发布整个java文件吗..
  • @Canato 我编辑了我的帖子
  • 你有没有初始化showVoiceText

标签: java android void oncreate


【解决方案1】:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    TextView showVoiceText = (TextView) findViewById(R.id.showVoiceOutput);

    showVoiceText.setText("I am working");
}

[如果第一个“TextView”已经在其他地方声明,你可能需要删除它,但是因为我们没有完整的代码...]

但仍然很高兴知道您的主要活动如何开始/调用此活动

【讨论】:

  • @DrunkProgrammer 您可以尝试使用手机进行调试。但我需要再问一次,你能用 Main Activity 代码编辑吗?
  • 我添加了主要活动
  • 那么,您确定 Home Activity 显示在视频之后吗?您能否将整个代码用于家庭活动(我之前应该问过)。而且您确实需要找到一种调试方法,即使使用手机也是如此。 @DrunkProgrammer
  • 我认为这不是我的代码的问题...我与媒体播放器发生冲突错误
  • 删除视频并调用 startActivity 进行测试
【解决方案2】:

我认为问题在于您声明了一个全局 TextView 并将其命名为 showVoiceText 但您从未对其进行初始化

解决方案是在setContentView 之后初始化它,如下所示:

showVoiceText=(TextView) findViewById(R.id.showVoiceOutput);

【讨论】:

  • 可能是背景颜色与字体颜色相同....换句话说,它正在工作,但颜色的相似性使它感觉不到
猜你喜欢
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多