App stops working when pressing a button - java

So I am starting with app programming and I am trying to program my first test app.
The main pupose of the app is that it acts like a dice and gives a random number between 1 and 6. However when I push the roll button the app crashes
My mainActivity code is as follows:
package com.example.second;
import java.util.Random;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void roll(View v){
int min = 1;
int max = 7;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
EditText random = (EditText)findViewById(R.id.randomtxt);
random.setText(i1);
}
#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);
}
}
And the .xml code is
<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: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.second.MainActivity" >
<Button
android:id="#+id/roll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="106dp"
android:layout_marginTop="124dp"
android:onClick="roll"
android:text="#string/roll" />
<TextView
android:id="#+id/randomtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/roll"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/roll"
android:layout_marginTop="48dp"
android:text="#string/num1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
The error that I get is Emulator] setWindowSurfaceColorBuffer: bad color buffer handle 0, but I do not think this has anything to do with the crashing off the app.
And because I cannot find any problems (nor does SDK provide any) I have no idea why my app crashes.

You're trying to cast a TextView to an EditText. Either change your XML layout to define randomtxt as an <EditText>, or what you probably want is to just change this line:
EditText random = (EditText)findViewById(R.id.randomtxt);
to this:
TextView random = (TextView)findViewById(R.id.randomtxt);
You also probably want to change your setText line to:
random.setText(Integer.toString(i1));
Although there are many more options to cast you integer value to a String.

cast error textview to edittext and you cannot settext(integervalue) -> settext(String.valueof(integer))
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int min = 1;
int max = 6;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
random.setText(String.valueOf(i1));
}
});

Related

App to list Pdf in activity from sdcard

I am developing an app in android which requires a list activity to show all pdfs from sdcard..i found the below code and used it but it is not working my app is unfortunately closing.. can anyone tell me whats the error.
MainActivity.java
package com.example.vinita.listallpdfs;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import static com.example.vinita.listallpdfs.R.layout.activity_main;
public class MainActivity extends ListActivity {
ArrayAdapter adapter;
int clickCounter=0;
ArrayList listItems=new ArrayList();
private File[] imagelist;
String[] pdflist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
File images = Environment.getExternalStorageDirectory();
//ListView list=(ListView)findViewById(R.id.list);
imagelist = images.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return ((name.endsWith(".pdf")));
}
});
pdflist = new String[imagelist.length];
for (int i = 0; i < imagelist.length; i++) {
pdflist[i] = imagelist[i].getName();
}
Arrays.sort(imagelist);
Arrays.sort(pdflist);
this.setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, pdflist));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
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=".MainActivity"
android:background="#ff3db0ff"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:scrollbars="vertical"/>
</RelativeLayout>
Add this permission to your manifest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
hope it helps :D

How to change "layout_margintop"?

I did something along the lines of a button which generates random numbers to be interchanged with the coordinates of causing displacement of a button on the screen, but I do not know how you could replace generated the number of planes X and Y, please help, and these are the codes of my application, just I teach and never found adequate support for my state of knowledge.
translated by Google from Polish
<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: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.reflex.MainActivity" >
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="45dp"
android:text="Reflex!" />
</RelativeLayout>
package com.example.reflex;
import java.util.Random;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener
{
private Button generuj;
private int wynik1;
private int wynik2;
private final static int min = 1;
private final static int maxh = 109;
private final static int maxv = 239;
private Random randomGenerator;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generuj = (Button) findViewById(R.id.b1);
randomGenerator = new Random();
}
#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;
}
#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);
}
#SuppressLint("NewApi") #Override
public void onClick(View v)
{
if(v.getId() == R.id.b1)
{
int temp = randomGenerator.nextInt(maxh) + min;
int temp1 = randomGenerator.nextInt(maxv) + min;
generuj.setX(temp1);
generuj.setY(temp);
}
}
}
You need to add the on click event to your XML.
android:onclick="onClick"
In the button tag

Starting another activity - application crash

At the beginning I notice that I'm novice at Android development.
I made application from http://developer.android.com/training/basics/firstapp/starting-activity.html and it worked well (second activity too). I want to make small change: in second activity I want to display "Your message:" text and below message from input from first activity. I did it my way and now when I tap Send button I have an application crash.
My second activity xml code:
<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: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.example.lislav.firstandroidapp.DisplayMessageActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/header_my_message"
android:textSize="40px"/>
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70px"/>
</RelativeLayout>
and second activity java code:
package com.example.lislav.firstandroidapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textViewMessage = (TextView) findViewById(R.id.message);
textViewMessage.setText(message);
setContentView(R.layout.activity_display_message);
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I think it's something wrong with onCreate function.. But I don't know what. Sorry, if there are any obvious mistakes..
You must call findViewById method only after calling setContentView. Just put setContentView right after calling of super.onCreate.
By the way read about logcat tool. This is logger which you should use always facing any problem.

how to place the textview exactly where the user Long Presses in Eclipse for Android

I am learning to code in Eclipse for Android. I googled and understood that View.OnLongClickListener can be used to call a method when the user long presses in the application. However, I am stuck at coming at a solution to make the TextView to shift it based on the exact location where the user long presses.
I have implemented the condition to check for a long press from the user. Any idea on how to make the TextVIew appear where the user long presses? I want the TextView to appear exactly where the user long presses.
findViewById(R.id.myimage).setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
showNameInCustomPosition();// Method to call to when user long presses
return false;
}
});
Update: I am not getting the text to move, but when I click on the edge of the screen, the text is partially displayed that it goes outside the scope of the screen. Can anyone suggest how to avoid this? This is my Layout properties now.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myimage"
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"
android:background="#drawable/shrinivas"
android:onClick="onClick"
android:clickable="true"
tools:context="com.example.hello.MainActivity" >
<TextView
android:id="#+id/shownamecenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/my_text2" />
<TextView
android:id="#+id/shownamecustom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_gravity="fill"
android:text="#string/my_text" />
</RelativeLayout>
The one with id shownamecustom is the one causing provblems.
Try this, below code is MainActivity which implements OnTouchListener
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnTouchListener {
TextView move_textview;
RelativeLayout mainLayout;
int x,y; //used to get coordinates
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (RelativeLayout)findViewById(R.id.mainlayout);
move_textview = (TextView)findViewById(R.id.textView1);
mainLayout.setOnTouchListener(this);//listener to get touch coordinates
mainLayout.setOnLongClickListener(new OnLongClickListener() { //listen when user long click
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
setAbsoluteLocation(move_textview,x,y); //passing textview, x & y param to the method where the textview wanna move
return false;
}
});
}
//methode use to move the text view
private void setAbsoluteLocation(TextView tview, int x, int y)
{
RelativeLayout.LayoutParams alp = (RelativeLayout.LayoutParams) tview.getLayoutParams();
alp.leftMargin = x-50;
alp.topMargin = y-50;
tview.setLayoutParams(alp);
}
#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;
}
#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);
}
//Overide onTouch method used to get touch coordinates
#Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
x = (int)event.getRawX();
y = (int)event.getRawY();
return false;
}
}
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainlayout"
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.sample.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="86dp"
android:layout_marginTop="171dp"
android:text="Movable" />
</RelativeLayout>

Image not getting displayed in emulator

I am implementing a simple page similar to any other app, a 'header image' and content below.
I've placed a wedhead.png file manually into all the drawable folders that I want to display.
The layout I wish to implement displays correctly on the graphical editor, but not on the emulator. The emulator comes out blank. Emulator is ARM CPU based.
Emulator screen -
Here's my code
XML File:
<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: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.wwmk1.MainActivity" >
<ImageView
android:id="#+id/img1"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginBottom="19dp"
android:contentDescription="header"
android:fadingEdge="vertical"
android:src="#drawable/wedhead"
android:visibility="visible" />
</RelativeLayout>
The Main activity java file
package com.example.wwmk1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView)findViewById(R.id.img1);
}
#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;
}
#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);
}
}
What place did I go wrong?
Please help. Thanks

Categories

Resources