user3765415
user3765415

Reputation: 109

Start new Activity on a button click

I am very new to Android & Java and I know there are many questions and answer available on this site regarding starting a new activity on a button click. I read them and even I watched some videos on YouTube, but I don't know what am I doing wrong. I know it's very simple thing to you and hope you can help me out. Whenever I run I get an error as follows.

error: cannot find symbol class view cannot find symbol class intent. cannot find symbol class intent. Error:Execution failed for task ':app:compileDebugJava'.

Compilation failed; see the compiler error output for details.

I am using Android Studio (Beta) 0.8.2

Following is Java code

package com.abc.xyz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.abc.xyz.R;

public class AddNew extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.add_new, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void goo(View view)
    {
        Intent intent=new Intent (this, AddNewWeb.class);
        startActivity(intent);
    }

}

Following is the xml layout code

<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:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.abc.xyz.AddNew"
    android:baselineAligned="false">    

<Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2"
        android:layout_gravity="center_horizontal"
        android:onClick="goo"/>


</LinearLayout>

Is there anything that I should do in the other activity which I want to open?

Thanks in advance.

Upvotes: 1

Views: 234

Answers (5)

Mahesh Gawhane
Mahesh Gawhane

Reputation: 338

First clean your project from Build==>Clean Project and then use this code in your onCreate() method

Button button = (Button)findViewById(R.id.btn_id);
button.setOnClickListner(new OnClickListner)
{
   @override
   public OnClick(View v)
   {
      Intent intent = new Intent(getApplicationContex(), YourActivity.class);
      startActivity(intent);
      finish();
  }
}

Or simply implement OnClickListner and override the OnClick method and put this line in your onCreate()

Button button = (Button)findViewById(R.id.btn_id);
button.setOnClickListner(this);

if this code is not solves problem then check your JDK, if you are using JDK7 and project built at JDK8, so installe JDK8 and set path and java_home and run again, It's working fine....

Upvotes: 0

Sushant Sehgal
Sushant Sehgal

Reputation: 1

I found the solution to your problem. In fact, this is one thing which often slips out of your mind when you're dealing with new activities. In order to open a new activity, you need to add a permission statement in your AndroidManifest.xml file. Add the following line of code to your AndroidManifest.xml:- <activity android:name="com.abc.xyz.AddNewWeb"></activity>

Upvotes: 0

avismara
avismara

Reputation: 5149

I see that you are missing all the imports here. Try pressing Ctrl+Shift+o to get all the imports, if you are using Eclipse.

Also, see to it that your Activity is registered in your XML file. It needs to be registered in the Manifest file for your app to be able to recognize your Activity.

Upvotes: 2

lacrirra
lacrirra

Reputation: 98

You need to import some libraries for example import android.content.Intent; now Why don't you try this?

public class AddNew extends Activity implements OnClickListener{
.
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_new);
    View btn = new (View)findViewById(R.id.button2);
    btn.setOnClickListener(this);
}
.
.
.
@Override
public void onClick(View v){
   if (v.getId == R.id.button2){
      Intent intent=new Intent (this, AddNewWeb.class);
      startActivity(intent);
   }
}

}

Upvotes: 0

Carlos J
Carlos J

Reputation: 3045

You just need to add this to your class:

import android.view.Menu;
import android.view.MenuItem;

Place it next to the other imports. That should solve the problem

Upvotes: 0

Related Questions