Okay so I'm working on a project and I'm trying to use buttons with text inside of them in an activity, but eclipse can't find the id or chooser.xml
Here's myActivity class
package com.xxxxxxx;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.*;
public class MyActivity extends Activity {`
private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//chooser is underlined (chooser cannot be resolved or is not a field)
setContentView(R.layout.chooser);
//all ids are underlined(id cannot be resolved or is not a field)
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton3 = (Button) findViewById(R.id.button3);
mButton4 = (Button) findViewById(R.id.button4);
mButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity2.class);
startActivity(myIntent);
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activty3.class);
startActivity(myIntent);
}
});
mButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity4.class);
startActivity(myIntent);
}
});
mButton4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity5.class);
startActivity(myIntent);
}
});
}
}
Here's the chooser.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/button_1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_1" />
<Button
android:text="#string/button_2"
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_3"
/>
<Button
android:id="#+id/button_4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_4"
/>
</LinearLayout>
And here's the string resource xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Test App</string>
<string name="test_image">test.png</string>
<string name="button_1">button_1</string>
<string name="button_2">button_2</string>
<string name="button_3">button_3</string>
<string name="button_4">button_4</string>
</resources>
Your gen files most likely aren't generated yet. Try building it, it should make the lines go away.
1) You need to import your R class. import <android package>.R Ref: R.id cannot be resolved AND R cannot be resolved - Android error
2) Aren't your R.id.button1 supposed to be R.id.button_1 and so forth for other buttons?
Is chooser.xml in the correct folder (the default layout folder provided with the project)? Have you perhaps tried to go to Project and choose Clean? Everything looks okay in your code.
I know this is a very old post, though I still stumbled upon it in my search for a similar problem. When I built the android project, it automatically added:
import android.R;
When I used the advice above,
import <android package>.R;
and I still used R.id....., it still propagated an error. I removed
import android.R;
and no errors.
Related
I want to share an image in my application.
This is my layout file
activity_xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.dc.google.MainActivity">
<Button
android:id="#+id/share_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share on Google+"
/>
</RelativeLayout>
This is my java class file
MainActivity.java
package com.example.dc.google;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button shareButton = (Button) findViewById(R.id.share_button);
shareButton.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View v) {
// Launch the Google+ share dialog with attribution to your app.
Intent shareIntent = shareButton.setText("Welcome to Google+")
.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent,0);
}
});
}
}
Here the .setContentUrl shows an error saying the method can not be resolved.
I can't find the solution.
How can I resolve this error?
Need to read the documentation again...
You shouldn't setText on your button... You setText on a new PlusShare.Builder
Worry about sharing an image once you get code that will compile. (Hint: that documentation also shows sharing images).
Button shareButton = (Button) findViewById(R.id.share_button);
shareButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Launch the Google+ share dialog with attribution to your app.
Intent shareIntent = new PlusShare.Builder(MainActivity.this)
.setType("text/plain")
.setText("Welcome to the Google+ platform.")
.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent, 0);
}
});
I am trying to add custom fonts in my android app.
LoginActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.app.Activity;
import android.widget.TextView;
import android.graphics.Typeface;
public class LoginActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button login = (Button) findViewById(R.id.login);
Button signup = (Button) findViewById(R.id.signup);
TextView username = (TextView) findViewById(R.id.username);
Typeface custom_font = Typeface.createFromAsset(this.getAssets(), "fonts/smartwatch.ttf");
username.setTypeface(custom_font);
login.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent loginOnlyIntent = new Intent(view.getContext(), LoginOnlyActivity.class);
startActivity(loginOnlyIntent);
}
});
signup.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent signUpIntent = new Intent(view.getContext(), Signup.class);
startActivity(signUpIntent);
}
});
}
}
I am getting the error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.LoginActivity}: java.lang.NullPointerException
I have added assests folder in the src/main folder too.In the assests folder is fonts folder in which I have font file smartwatch.tff
I have checked almost every answer available on SO but nothing worked so far for me.
update:
After using Debugger, I found that
TextView username = (TextView) findViewById(R.id.username);
returns null and hence the NPE.
Adding layout file :
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="155.0dip"
android:paddingTop="200.0dip"
android:paddingRight="4.0dip"
android:paddingBottom="1.0dip"
android:layout_below="#+id/TextView02">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/UserName"
android:id="#+id/username"/>
<EditText
android:id="#+id/edit_phone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:inputType="number"
android:hint=" "/>
</LinearLayout>
Please help me why textview username is null
Your code works fine for me.
In LinearLayout you provided missing buttons, but I think you have added them in your project. In addition, I have error because you have omitted the XML namespace
xmlns:android="http://schemas.android.com/apk/res/android"
into Linear Layout.
Try to Invalidate and restart project, It 's strange the null pointer problem on TextView, You've done all right, unless the problem lies elsewhere.
Make sure you have created the Assets folder doing:
Right click on "app" folder, new --> Folder --> Assets Folder
this my xml file main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="#raw/christmas"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/by_kostas"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="83dp"
android:text="#string/by_kostas"
android:textSize="20sp"
android:textColor="#F2F3F4"/>
<ImageView
android:id="#+id/settings"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="nameOfMethod"
android:src="#raw/settings" />
</RelativeLayout>
and my java Main
package com.kostas.mytorch;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//start our layout
super.onCreate(savedInstanceState);
setContentView(R.layout.main);{
final ImageView diskView = (ImageView) findViewById(R.id.settings);
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//my codes
}
});
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// System.out.println("image clicked...");//in my logcat
startActivity(new Intent("com.kostas.standroid.settings"));
}
});
}
}
my problem is when i click in the settings icon, my program crashes instead of what i wanted to create a new layout (settings xml is just black page),can someone be kind enough to help me out
You can attach click listener to any view by two ways:
In xml, write onClick attribute of the view and pass the method
name you have implemented in the activity.
Example: android:onClick="someMethod" and in the activity code, declare a method
public void someMethod(View view)
{
// handle click here
}
In activity, use setOnClickListener() to the view.
For now, try this:
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// System.out.println("image clicked...");//in my logcat
startActivity(new Intent(Main.this, settings.class));
}
});
Also remove this line from xml: android:onClick="nameOfMethod"
Some time there is contextual problem. Try this.
Intent intent = new Intent(CurrentActivity.this, UpcomingActivity.class);
startActivity(intent);
Also don't forget to define class in Manifest file
try to remove
android:onClick="nameOfMethod"
from the xml layout. I believe this is the method that it's called when you click the settings button and it doesn't exist so it crashes.
I have a problem with my Android app. I have three Activities that all connect with one another with a Image button.
My problem is I can't go back on any of the pages, just forwards. I have been reading about the back button and how to keep its state and not kill (End) it, so the user can click back if needed too.
I am new to Android and I know there's a lot to learn. So thank you for your help.
Java code for first Activity:
package my.hope;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.content.Intent;
public class NewhopeActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView myImage = (ImageView) findViewById(R.id.imagebutton1);
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(NewhopeActivity.this, Act2.class);
startActivity(intent);
}
});
}
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#drawable/mint">
<ImageButton
android:id="#+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:background="#drawable/ic_launcher"
android:onClick="Act2"
android:src="#drawable/ic_launcher" />
</LinearLayout>
What happens when you click the back button on your device? Unless specified, it kills the current activity and brings you back to the previous one.
However, if you wish to specify exactly what would clicking the back button do, you can override the onBackPressed() method:
#Override
public void onBackPressed() {
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
Ok this may seem like a pointless example but if I can figure this out then the program I am trying to make will work. So I have two activities test and test two each with one button.
Test 1:
package thompson.cameron.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View button = findViewById(R.id.testButton);
button.setOnClickListener(this);
}
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
}
and test2
package thompson.cameron.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test2 extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
View test = findViewById(R.id.testButton);
test.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.testButton:
System.exit(1);
}
}
}
When I click the button on Test it is supposed to launch test2 however it is at this point I get an null pointer exception that I have narrowed down to test.setOnClickListener(this); line of code. Below are my two xml files for the layout. I can get the button to work when I only have one activity but as soon as I add a second activity with a different layout file it all falls apart
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST"/>
</LinearLayout>
main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST2 TEST2 TEST2"/>
</LinearLayout>
I'm still new at Android programming so thanks for all your help.
Open the Debug perspective in Eclipse
Choose the 'Breakpoints' view tab.
Select the 'Add Java Exception Breakpoint' and choose
NullPointerException.
Launch your activity, either by using 'Debug As...' or attaching
the debugger to a running instance via DDMS.
Execute the offending workflow. It will break on the line that
caused the NullPointerException.
In your test.java file give:
implements View.OnClickListener
Initialize your button as:
Button testButton = (Button) findViewById(R.id.testButton);
and inside your onClick method, check whether you are clicking button:
if(v == testButton) {
//give ur intent code
}
There are different ways to perform onClick functionality.
One is the above method which I have mentioned.
Another one is what ankit has mentioned.
Third way is through your layout.
Inside your layout for your button tag, you may give as:
<Button android:id="#+id/testButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Click" android:onClick="onTestButtonClick" />
And inside your class just mention the below details for button:
public void onTestButtonClick(View view) {
//give your intent code
}
You may refer to the link also:
http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html
Make sure that both activities are register at the application's manifest file.
As a side note never call System.exit in your code. You can call finish() to close an Activity and this will bring at the front the previous Activity on the stack.
The issue here is that you haven't typecasted your views to buttons.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; // Needed to add this import for the button casting below
public class Test extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// I have changed View to Button and then typecasted
// with the "(Button)" the return of findViewById
Button button = (Button) findViewById(R.id.testButton);
button.setOnClickListener(this);
}
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
}
Let me know if you have any issues with this. I just completed my first experiment through using the onClickListener implementation through the main class instead of individual anonymous listeners.
Andrew
I had the same problem, you may put the same content view that the button,
setContentView(R.layout.main); if the button is in that content view, in other case, you will put:
setContentView(R.layout.buttoncontentview);
View button = findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
setContentView(R.layout.main);
sorry for my bad english, but i'm spanish
Implement OnClickListener interface
and set button.setOnClickListener(this);
and override
public void onClick(View v) {
}
I think your buttons IDs need to be different in different activities. R.id.testButton would refer to only one button.
The final solution is that you may modify the AndroidManifest.xml file, i finally solved my error in this link How to register a new activity in AndroidManifest.xml?
You can try this.it may work.
package thompson.cameron.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test extends Activity{
private Button button;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = findViewById(R.id.testButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i=new Intent().setClass(Test.this,Test2.class);
startActivity(i);
}
});
}
}
First of all in main.xml and main2.xml chage the button's ids like in below code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST"/>
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST2 TEST2 TEST2"/>
</LinearLayout>
It throws nullpointerexception because of id confict with each other so in your java file use following code to find button.
In Activity 1
Button button = findViewById(R.id.testButton);
button.setOnClickListener(this);
and
In Activity 2
Button button = findViewById(R.id.testButton1);
button.setOnClickListener(this);