简体   繁体   中英

How do I fix this error - "Unknown entity "another""

Activity that I want to go

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
  
    <TextView
         android:text="hi"/>
</LinearLayout>

Main Activity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
        android:id="@+id/x"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="x"/>
</LinearLayout>

Java (has unknown entity error)

package com.mycompany.myapp2;

import android.app.*;
import android.os.*;
import android.view.*;
import android.content.*;

public class MainActivity extends Activity 
{

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

    public void x(View view) {

        Intent intent = new Intent(this, another.class);
        startActivity(intent);
    }
}

I'm trying to make an activity go to another activity, but for some reason it doesn't work. I was expecting it to work, like it should with everyone Please help it has error "unknown entity" and I don't know why.

Answer # 1

If you want to move to another activity

class AnotherActivity extends extends Activity {

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

you must extend your class "AnotherActivity.class" with "Activity" and override onCreate method

Answer # 2

public void x(View view) {
    setContentView(view)
    // OR
    setContentView(R.layout.another)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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