Unable to Use a Button - java

package com.example.one;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
TextView txt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton(){
button=(Button)findViewById(R.id.button1);//error line of code
txt1=(TextView)findViewById(R.id.txt);//error line of code
button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
txt1.setText("hello");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have marked the error code of lines but unable to resolve
the .xml is
<EditText
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txt"
android:layout_below="#+id/txt"
android:layout_marginLeft="18dp"
android:layout_marginTop="110dp"
android:text="Button -MYBUT" />
I just want to add a button and a textview in my app beacuse I'm a beginner Android Developer so unable to resolve my issue thanks in advance

Try to use findViewById after you inflate your menu. You are trying to find an element in a view that has not yet be attached to the activity.

This is just fine, check your imports. In android are 2 types of R present.
- R Generated, by your project builder
- R brought by android sdk
I guess that you imported R from android SDK, not generated by your project. That's why eclipse yells with errors at you.
The other situation is, that, the findViewById() method called OnCreateOptionsMenu() callback is trying to reach view that has not been created yet, and returns null.
EDIT. Problems with R might be also associated with weird, not visible and hard to find mistakes in your xml files. When you make huge mistake in some of your xml files, builder returns error and R will not be generated. But in some cases, builder cant find any of your mistakes, but is not generating new R class - this causes your R to be out of date.

I just want to know that about the explanation of this line
but = (Button) findViewById(R.id.but);
findViewById(Id) returns a View and you have to cast that View to whatever UI element you are using.
In order for findViewById(Id) to return a View, you have to assign an Id to each UI element (if you want to use it in java):
<Button
android:id="#+id/myButtonId"
[...]
/>
Now you can do this:
but = (Button) findViewById(R.id.myButtonId);
You should also use the onCreate() method to initialize your UI elements, and not onCreateOptionsMenu().

Eclipse is a Curse try to not use it guys I Prefer NetBeans I love NetBeans so much Hats off for NetBeans!
The Answer of my own Question:
it was a real time fault of my eclipse (a curse) not my fault. The Solution is to restart the IDE or to press the Save All from File Menu of IDE
Visit: https://stackoverflow.com/a/7453201/2277645

Related

Android studio Can't find button ID in java file

I've started working on a menu for my app and I'm trying to start an activity from a fragment. My fragment code looks like this;
public class Studies extends Fragment {
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Studies");
}
public void goToAttract(View v) {
Intent intent = new Intent(getActivity(), informaticainfo.class);
startActivity(intent);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.studies, container, false);
}}
I've got this working in a different project so I do not think that is the problem, my XML button to go to the next activity looks like this;
<Button
android:id="#+id/I"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="40dp"
android:background="#drawable/buttonshape"
android:onClick="goToAttract" <---- is this wrong?
android:paddingLeft="130sp"
android:paddingRight="130sp"
android:text="#string/informatica"
android:textColor="#ffffff" />
I've declared the new activity in the Manifest, but the XML file keeps giving this error at the line specified above;
Cannot resolve symbol 'goToAttract' less
Inspection info: Checks if the method specified in onClick XML attribute is declared in related activity.
Does the method I'm using above not work with fragments? Why can it not find the function I have working in another project?
Check this link [How exactly does the android:onClick XML attribute differ from setOnClickListener?
According to this that with the XML above, Android will look for the onClick method only in the current Activity. This is important to remember if you are using fragments, since even if you add the XML above using a fragment, Android will not look for the onClick method in the .java file of the fragment used to add the XML.
Declaring and assigning value to onClickin XML does not work in fragment, use OnClickListener programmatically instead of using onClick in XML.

ActionBar-PullToRefresh nothing happens

I'm trying to add ActionBar-PushToRefresh to my project. I've followed the instructions from Chris Banes Github there: Github ActionBar-PullToRefresh
Here is my view from which I want to enable the PullToRefresh feature:
<uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ptr_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your content, here we're using a ScrollView -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="PULLTOREFRESH THE VIEW"
android:id="#+id/textView1" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ScrollView>
</uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>
And here is my Activity to display the view:
package com.Test.pulltorefresh;
import android.app.Activity;
import android.os.Bundle;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.Options;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
public class MainActivity extends Activity {
private PullToRefreshLayout mPullToRefreshLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Now find the PullToRefreshLayout to setup
mPullToRefreshLayout = (PullToRefreshLayout) findViewById(R.id.ptr_layout);
// Now setup the PullToRefreshLayout
ActionBarPullToRefresh.from(this)
// Mark All Children as pullable
.allChildrenArePullable()
// Set the OnRefreshListener
//.listener(this)
// Finally commit the setup to our PullToRefreshLayout
.setup(mPullToRefreshLayout);
}
}
When I launch my app, I got the blank view but nothing happens when I'm trying to PushToRefresh my view. Can someone show me how to fix it, I know that I've missed something. Thanks.
I may be a bit partial but...
you could consider using this
Maybe you have already achieved the functionality, but in the code above, you have commented out the line which defines the action on pulling the view.
//.listener(this)
You should uncomment this line and define the listener method, which should be called when the view is pulled.

Bettering the Generic SeekBar Example

This code and minor variations thereof exist in many forums on the net. When I launch it on my Samsung Galaxy Tab 2 (Android 4.1.1), the bar appears, the thumb can be moved, the position is reported, etc. All good, EXCEPT, the thumb never disappears. Mine manifests as a little blue circle, which I can slide along a bar, but it leaves a trail of "thumbs" behind it: one at each of the possible integer locations in the range of the seekbar.
Is the code incomplete? Is there something we have to add to manually redraw the component?
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
public class FullscreenActivity
extends Activity
implements SeekBar.OnSeekBarChangeListener {
SeekBar seekBar;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
seekBar=(SeekBar)findViewById(R.id.seekbar);
textView=(TextView)findViewById(R.id.textview);
seekBar.setOnSeekBarChangeListener(this);
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// Notify that the progress level has changed.
textView.setText(textView.getText()+"\n"+"SeekBar now at the value of:"+progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// placeholder
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// placeholder
}
}
Here's the layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/FrameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SeekBar
android:id="#+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:max="31"
android:progress="15" />
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="270dp"
android:layout_marginTop="150dp"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" />
</FrameLayout>
Screenshot of the effect:
Update The effect is the same on a ZTE-BLADE running Android 2.2. Of course teh default graphic is different (the gray / orange seekbar with a more square "thumb") but the thumbs remain behind at all the points I've scrolled through.
The residual garbage clears if I change the screen orientation, which makes me think it just needs a redraw. Can anyone help?
So, I copied and pasted my example code line-at-a-time into a new project, running it as I went, hoping to find the line which failed.
Got the whole thing pasted in and it worked perfectly. No problems in the seekbar.
On comparing the projects, the only difference was that the AndroidManifest.XML file contained the line
android:theme="#style/FullscreenTheme"
in the activity section. So, I went back to my original (faulty) project and took this line out. Now the project works fine.
I have my answer: the code is complete and there's nothing we need to add to make it redraw.
I still don't understand why this line had that effect and now I have a horrible white background when I wanted black.

Errors implementing splash screen for webview

I'm trying to implement a splash screen for my webview android app, so that it will display until the page is fully loaded. I was using the code from the accepted answer here but am running into some errors that I'm not sure how to solve. I assume that I'm missing a library of some sort, but I have no idea what.
The errors I'm getting are:
View cannot be resolved to a variable
error: Error: No resource found that matches the given name (at 'src' with value #drawable/vert_loading')
The code where I'm getting the view errors is:
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
findViewById(R.id.webview).setVisibility(View.VISIBLE);
}
});
While my layout xml file is:
<ImageView
android:id="#+id/imageLoading1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="visible"
android:src="#drawable/vert_loading"
/>
My assumption is that I might be missing an import of some sort, but I wouldn't know which one. The ones I have are:
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Any help would be greatly appreciated.
Add an drawable folder in your project res from File->New->Folder and put your drawable vert_loading in res->drawable folder. then Clean your project from Project->Clean.

Password protecting my android app (the simple way)

I've built my first app, and I would like to password protect it. It's fine for me to store the password in the Java files and the method needs to be as simple as possible because i have no experience of java or even xml before this app. I've had a few attempts and failed so I was hoping someone can help me out.
I've created the layout with an EditText field:
<EditText
android:id="#+id/passwordedittext"
android:layout_width="200dp"
android:layout_height="50dp"
android:inputType="textPassword"
android:layout_marginTop="40dp"
android:layout_marginLeft="20dp">
<requestFocus />
and a submit button:
<Button
android:id="#+id/submitbutton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:background="#drawable/bgo"
android:clickable="true"
android:layout_gravity="right|center_horizontal"
android:layout_marginRight="20dp"/>
The Java file:
package com.berry;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class password extends Activity{
MediaPlayer mpbuttonclick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.password);
mpbuttonclick = MediaPlayer.create(this, R.raw.keypress);
Button sumbitButton = (Button) findViewById(R.id.submitbutton);
sumbitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
EditText passwordEditText = (EditText) findViewById(R.id.passwordedittext);
if(passwordEditText.getText().toString()=="MyPasswordHere"){
startActivity(new Intent("com.berry.intro"));
mpbuttonclick.start();
}}});
}}
This part:
if(passwordEditText.getText().toString()=="MyPasswordHere")
is incorrect. It should be
if(passwordEditText.getText().toString().equals("MyPasswordHere"))
When comparing primitive data types (like int, char, boolean) you can use ==, !=, etc.
When comparing objects (like String, Car, etc) you need to use the .equals() method.
See also this page.
It is in no way safe to check your password like that.
There are several ways to easily bypass your code
Calling the activity directly from another App
Reading the disassembled smali code to retrieve the password
Modifying the code using smali to always jump into the codeblock
Solutions available to solve these problems:
Obscure your code (Worst option, but might be enough in most cases)
Comparing the Hashed Password: Much more secure. But should be a salted Hash.
(There is also a more simple to understand explaination for the implementation)
Use a HTTP Request to a server of yours to hide the mechanism behind the password check. (But that requires your app to ask for Networking Permissions)
In the edit text field xml you can add
android:password="true"

Categories