Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 移动到代码后的Android xml不起作用_Java_Android_Xml_View - Fatal编程技术网

Java 移动到代码后的Android xml不起作用

Java 移动到代码后的Android xml不起作用,java,android,xml,view,Java,Android,Xml,View,将视图的XML代码直接重新创建为java代码后,它将停止正常工作 XML代码是: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"

将视图的XML代码直接重新创建为java代码后,它将停止正常工作

XML代码是:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.virviil.splashscreen.MainActivity">


    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.7" />
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.3" />
</TableLayout>
其想法是通过编程改变进度的垂直校准。 因此,在XML中它是有效的
在JAVA中,不是

唯一需要注意的是,在整个xml视图中使用LinearLayout而不是od TableLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.virviil.splashscreen.MainActivity">
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

....

....

将其放在LinearLayBurst xml中即可。因此,即使我将所有内容都更改为linearLayout,为什么java不能工作呢?TableLayout activity_main=(TableLayout)findViewById(R.id.activity);我根本不使用xml。这是一个什么都不做的点。我在linearLayout中添加了所有内容,但仍然不起作用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.virviil.splashscreen.MainActivity">
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

....