简体   繁体   English

ID无法解析或不是字段

[英]ids cannot be resolved or is not a field

I'm new to android development and I'm currently working on building a pretty basic application. 我是android开发的新手,目前正在构建一个非常基本的应用程序。 I want 'button1' to open a new activity. 我希望“ button1”打开一个新活动。 Looked around online and I can't work out what I'm doing wrong! 在网上四处张望,我无法找出我做错了什么!

Java: Java的:

package com.testtest.herro;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class FullscreenActivity extends Activity

{

Button button1, saved;

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

            button1 = (Button) findViewById (R.id.button1);
        };

};}

I'm getting the following errors on the public void onCreate method: 我在公共void onCreate方法上遇到以下错误:

  • Syntax errors on token ")", { expected after this token. 令牌“)”上的语法错误,{应该在此令牌之后。
  • Overrides android.app.Activity.onCreate 覆盖android.app.Activity.onCreate
  • Syntax errors on token ")", { expected after this token. 令牌“)”上的语法错误,{应该在此令牌之后。

The findViewById line has this error: findViewById行有此错误:

-ids cannot be resolved or is not a field. -ids无法解析或不是字段。

XML: XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
tools:context="com.testtest.herro.FullscreenActivity" >

<View
    android:layout_width="1dp"
    android:layout_height="30dp" >
</View>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:gravity="center"
    android:text="Idea Cloud"
    android:textSize="45sp"
    android:typeface="monospace" >
</TextView>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:gravity="center"
    android:text=""
    android:textSize="45sp"
    android:textStyle="bold" >
</TextView>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="bottom"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <View
        android:layout_width="1dp"
        android:layout_height="30dp" >
    </View>

    <Button
        android:layout_width="250sp"
        android:layout_height="100sp"
        android:layout_gravity="center"
        android:text="New"
        android:textSize="20sp"
        android:typeface="monospace"
        android:id="@+id/button1" >
    </Button>

    <View
        android:layout_width="1dp"
        android:layout_height="50dp" >
    </View>

    <Button
        android:layout_width="250sp"
        android:layout_height="100sp"
        android:layout_gravity="center"
        android:text="Saved"
        android:textSize="20sp"
        android:typeface="monospace"
        android:id="@+id/savedB" >
    </Button>
  </LinearLayout>

</LinearLayout>

Any help is greatly appreciated! 任何帮助是极大的赞赏! Thanks. 谢谢。

Change your function to this: 将功能更改为此:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen);
    button1 = (Button) findViewById (R.id.button1);
}

Also, to change activities make these two changes: 此外,要更改活动,请进行以下两项更改:

XML XML

<Button
        android:layout_width="250sp"
        android:layout_height="100sp"
        android:layout_gravity="center"
        android:text="New"
        android:textSize="20sp"
        android:typeface="monospace"
        android:id="@+id/button1"
        android:onClick="activityChange">
</Button>

Java Java的

public void activityChange(View view) {
     //Change Activities using an Intent
    Intent intent = new Intent(this, YOUR_CLASS_NAME.class);
    startActivity(intent);
}

See these references for an explanation. 有关说明,请参见这些参考。 This developer.android.com/training site will help you learn the basics the right way. 这个developer.android.com/training网站将帮助您以正确的方式学习基础知识。

http://developer.android.com/reference/android/widget/Button.html http://developer.android.com/training/basics/firstapp/starting-activity.html http://developer.android.com/reference/android/widget/Button.html http://developer.android.com/training/basics/firstapp/starting-activity.html

Your super call is outside the method. 您的super调用不在方法范围内。 This: 这个:

public void onCreate(Bundle savedInstanceState) 
super.onCreate(savedInstanceState);
{
    ...
}

should be: 应该:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    ...
}

In general, if you start seeing lots of errors, it's worth looking at the first line containing an error - that's probably the cause. 通常,如果您开始看到很多错误,那么值得一看的是包含错误的第一行-这可能是原因。 Also, save and compile often , so you can tell where you've made a mistake and fix it quickly. 另外,请经常保存和编译,这样您就可以知道在哪里出错了,并快速修复。

Also note that you don't need semi-colons after method or class declarations - I would remove them as a matter of style. 另请注意,在方法或类声明之后不需要分号-我会出于风格考虑将其删除。

Ideally, use @Override too, to get the compiler to check that you're really overriding a method: 理想情况下,也可以使用@Override来使编译器检查您是否真的重写了方法:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM