Force close when setting onclick - java

I created a button and assigned it 1 by doing
remove.setId(0 + count);
Then I tried to put that button in my onCreate bundle to use it
Button remove = (Button)findViewById(1);
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
When I create the button remove in my oncreate it runs but as soon as I add my onclick listener I dont get any errors but when I run it it force closes
Log
12-12 13:42:06.172: D/AndroidRuntime(19502): Shutting down VM
12-12 13:42:06.172: W/dalvikvm(19502): threadid=1: thread exiting with uncaught exception (group=0x416bbd40)
12-12 13:42:06.178: E/AndroidRuntime(19502): FATAL EXCEPTION: main
12-12 13:42:06.178: E/AndroidRuntime(19502): Process: com.th3ramr0d.armytrooptotask, PID: 19502
12-12 13:42:06.178: E/AndroidRuntime(19502): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.th3ramr0d.armytrooptotask/com.th3ramr0d.armytrooptotask.MainActivity}: java.lang.NullPointerException
12-12 13:42:06.178: E/AndroidRuntime(19502): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
12-12 13:42:06.178: E/AndroidRuntime(19502): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
12-12 13:42:06.178: E/AndroidRuntime(19502): at android.app.ActivityThread.access$800(ActivityThread.java:139)
12-12 13:42:06.178: E/AndroidRuntime(19502): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
12-12 13:42:06.178: E/AndroidRuntime(19502): at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 13:42:06.178: E/AndroidRuntime(19502): at android.os.Looper.loop(Looper.java:136)
12-12 13:42:06.178: E/AndroidRuntime(19502): at android.app.ActivityThread.main(ActivityThread.java:5102)
12-12 13:42:06.178: E/AndroidRuntime(19502): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 13:42:06.178: E/AndroidRuntime(19502): at java.lang.reflect.Method.invoke(Method.java:515)
12-12 13:42:06.178: E/AndroidRuntime(19502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-12 13:42:06.178: E/AndroidRuntime(19502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-12 13:42:06.178: E/AndroidRuntime(19502): at dalvik.system.NativeStart.main(Native Method)
12-12 13:42:06.178: E/AndroidRuntime(19502): Caused by: java.lang.NullPointerException
12-12 13:42:06.178: E/AndroidRuntime(19502): at com.th3ramr0d.armytrooptotask.MainActivity.onCreate(MainActivity.java:45)
12-12 13:42:06.178: E/AndroidRuntime(19502): at android.app.Activity.performCreate(Activity.java:5248)
12-12 13:42:06.178: E/AndroidRuntime(19502): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
12-12 13:42:06.178: E/AndroidRuntime(19502): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
12-12 13:42:06.178: E/AndroidRuntime(19502): ... 11 more
Here is my full file
package com.th3ramr0d.armytrooptotask;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
public int count = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addTroopBtn = (Button) findViewById(R.id.addTroop);
addTroopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(count <= 5)
{
inputName(v);
}
else
{
}
}
});
Button remove = (Button)findViewById(1);
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
public void inputName(final View v){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
//alert.setTitle("Enter Name and Rank");
alert.setMessage("Please Enter A Name");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable name = input.getText();
addTroop(name);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
public void addTroop(Editable name){
LinearLayout mainPage = (LinearLayout) findViewById(R.id.manageTroopsMain);
if (count <= 5)
{
//CREATE NEW LINEAR LAYOUT
LinearLayout addTroopLayout = new LinearLayout(this);
//CREATE LAYOUT PARAMS FOR LAYOUT
LinearLayout.LayoutParams newLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newLayout.bottomMargin = 10;
//STYLE NEW LINEAR LAYOUT
addTroopLayout.setTag("addTroopLayout" + count);
addTroopLayout.setLayoutParams(newLayout);
addTroopLayout.setOrientation(LinearLayout.HORIZONTAL);
//CREATE NEW BUTTONS
Button newTroop = new Button(this);
Button remove = new Button(this);
Button change = new Button(this);
//CREATE LAYOUT PARAMS FOR BUTTONS
LinearLayout.LayoutParams newTroopParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 20f);
LinearLayout.LayoutParams rmvBtnParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
LinearLayout.LayoutParams chngNameParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
//STYLE NEW BUTTONS
newTroop.setText(name);
newTroop.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
newTroop.setLayoutParams(newTroopParam);
remove.setId(0 + count);
remove.setText("-");
remove.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
remove.setLayoutParams(rmvBtnParam);
change.setText("...");
change.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
change.setLayoutParams(chngNameParam);
//ADD VIEWS TO NEW LAYOUT
addTroopLayout.addView(newTroop);
addTroopLayout.addView(remove);
addTroopLayout.addView(change);
//ADD NEW LAYOUT TO mainPage LAYOUT
mainPage.addView(addTroopLayout);
//Increment Counter
count++;
}
}
}

This is your problem:
Button remove = (Button)findViewById(1);
This should be something like this:
Button remove = (Button)findViewById(R.id.buttonRemove); // that should be a long int

The crash is likely happening from this line:
Button remove = (Button)findViewById(1);
You'll need to provide a valid ID for a Button within your inflated layout. A value of 1 is not meaningful.

Related

Android Application "Source Not Found" java.lang.runtimeexception

I switched from running my app on a phone to a nexus 10 tablet and now it says "unfortunately the application has stopped."
The tablet is running 4.4.2, my target api is set to 19 in the manifest.
When I run it in debug mode I get Source Not Found. I tried clicking edit source lookup path and adding my project and had no luck.
I've looked all over the internet and haven't had any luck solving this problem. Any suggestions would be a big help.
"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.looper.video/com.looper.video.MainActivity}: java.lang.NullPointerException"
Edit:
MainActivity:
package com.looper.video;
public class MainActivity extends Activity {
private VideoView video;
private MediaController ctlr;
final Uri firstVideoPath = Uri.parse("android.resource://com.looper.video/" + R.raw.wildlife);
final Uri secondVideoPath = Uri.parse("android.resource://com.looper.video/" + R.raw.wildlife1);
private String videosPath = Environment.getExternalStorageDirectory() + "/videos/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File folder = new File(videosPath);
File[] customFiles = folder.listFiles();
List<String> customFileNames = new ArrayList<String>();
for(File file : customFiles) {
customFileNames.add(file.getName());
}
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, customFileNames);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
Spinner spinner = (Spinner)findViewById(R.id.spinnerCustomFiles);
spinner.setAdapter(spinnerArrayAdapter);
video = (VideoView)findViewById(R.id.videoView1);
ctlr = new MediaController(this);
final Button firstVideoBtn = (Button) findViewById(R.id.firstVideoBtn);
firstVideoBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
playFirstVideo();
}
});
final Button secondVideoBtn = (Button) findViewById(R.id.secondVideoBtn);
secondVideoBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
playSecondVideo();
}
});
}
public void playFeatured(View v) {
try {
Spinner spinner = (Spinner)findViewById(R.id.spinnerCustomFiles);
String strPath = videosPath + spinner.getSelectedItem().toString();
Toast.makeText(this, strPath, Toast.LENGTH_LONG).show();
//Uri path = Uri.parse(videosPath + spinner.getSelectedItem().toString());
video.setVideoPath(strPath);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);
video.requestFocus();
video.start();
video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer vmp) {
playSecondVideo();
}
});
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
public void playFirstVideo() {
video.setVideoURI(firstVideoPath);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);
video.requestFocus();
video.start();
video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer vmp) {
playSecondVideo();
}
});
}
public void playSecondVideo() {
SharedPreferences sp = getSharedPreferences("schedule", Context.MODE_PRIVATE);
//SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
String startTime = sp.getString("startTime", "");
String endTime = sp.getString("endTime", "");
/*
//SimpleDateFormat parser = new SimpleDateFormat("HH:mm");
//Date dateStartTime = parser.parse(startTime);
//Date dateEndTime = parser.parse(endTime);
Date now = new Date();
try {
if (userDate.after(ten) && userDate.before(eighteen)) {
}
} catch (ParseException e) {
// Invalid date was entered
}
System.out.println("Now:"+now);
System.out.println("Start"+startTime);
System.out.println("End:"+endTime);
//if(now.before(dateEndTime) && now.after(dateStartTime)){
//disableAll();
//} else {
*
*/
video.setVideoURI(secondVideoPath);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);
video.requestFocus();
video.start();
video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer vmp) {
playSecondVideo();
}
});
//}
}
public void admin(View view)
{
//MySQLiteHelper db = new MySQLiteHelper(this);
//final String password = db.getUser(7).getPassword();
//db.close();
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText pass = new EditText(this);
alert.setView(pass);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = pass.getText().toString().trim();
SharedPreferences sp1 = getSharedPreferences("Login", 0);
//String username = sp1.getString("UserName", "");
String password = sp1.getString("Password", "");
if(value.equals(password)) {
Intent intent = new Intent(getBaseContext(),Admin.class);
startActivity(intent);
}
//Toast.makeText(getApplicationContext(), password + " " + value, Toast.LENGTH_SHORT).show();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
}
#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);
}
public void disableAll(){
Button firstVideoBtn = (Button)findViewById(R.id.firstVideoBtn);
firstVideoBtn.setEnabled(false);
Button secondVideoBtn = (Button)findViewById(R.id.secondVideoBtn);
secondVideoBtn.setEnabled(false);
Spinner spinner = (Spinner)findViewById(R.id.spinnerCustomFiles);
spinner.setEnabled(false);
Button btnPlay = (Button)findViewById(R.id.btnPlayFeatured);
btnPlay.setEnabled(false);
VideoView videoView = (VideoView)findViewById(R.id.videoView1);
videoView.setEnabled(false);
}
}
Full logcat:
D/dalvikvm(2817): GC_FOR_ALLOC freed 58K, 4% free 3441K/3568K, paused 20ms, total 20ms
10-08 15:23:17.965: D/dalvikvm(2817): GC_FOR_ALLOC freed 2K, 4% free 3505K/3632K, paused 7ms, total 7ms
10-08 15:23:17.975: I/dalvikvm-heap(2817): Grow heap (frag case) to 8.739MB for 5515216-byte allocation
10-08 15:23:17.985: D/dalvikvm(2817): GC_FOR_ALLOC freed <1K, 2% free 8891K/9020K, paused 7ms, total 7ms
10-08 15:23:18.055: D/AndroidRuntime(2817): Shutting down VM
10-08 15:23:18.055: W/dalvikvm(2817): threadid=1: thread exiting with uncaught exception (group=0x4155eba8)
10-08 15:23:18.055: E/AndroidRuntime(2817): FATAL EXCEPTION: main
10-08 15:23:18.055: E/AndroidRuntime(2817): Process: com.looper.video, PID: 2817
10-08 15:23:18.055: E/AndroidRuntime(2817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.looper.video/com.looper.video.MainActivity}: java.lang.NullPointerException
10-08 15:23:18.055: E/AndroidRuntime(2817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-08 15:23:18.055: E/AndroidRuntime(2817): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-08 15:23:18.055: E/AndroidRuntime(2817): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-08 15:23:18.055: E/AndroidRuntime(2817): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-08 15:23:18.055: E/AndroidRuntime(2817): at android.os.Handler.dispatchMessage(Handler.java:102)
10-08 15:23:18.055: E/AndroidRuntime(2817): at android.os.Looper.loop(Looper.java:136)
10-08 15:23:18.055: E/AndroidRuntime(2817): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-08 15:23:18.055: E/AndroidRuntime(2817): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 15:23:18.055: E/AndroidRuntime(2817): at java.lang.reflect.Method.invoke(Method.java:515)
10-08 15:23:18.055: E/AndroidRuntime(2817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-08 15:23:18.055: E/AndroidRuntime(2817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-08 15:23:18.055: E/AndroidRuntime(2817): at dalvik.system.NativeStart.main(Native Method)
10-08 15:23:18.055: E/AndroidRuntime(2817): Caused by: java.lang.NullPointerException
10-08 15:23:18.055: E/AndroidRuntime(2817): at com.looper.video.MainActivity.onCreate(MainActivity.java:52)
10-08 15:23:18.055: E/AndroidRuntime(2817): at android.app.Activity.performCreate(Activity.java:5231)
10-08 15:23:18.055: E/AndroidRuntime(2817): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 15:23:18.055: E/AndroidRuntime(2817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-08 15:23:18.055: E/AndroidRuntime(2817): ... 11 more

android app crashes when killing one activity and start another one

There is one button I set in Scene2.java.I want to use the button to get in other activities Scene3.java,GameOver.java Everything worked fine until its about to open the new activity,every time the app crashed there. I want to know if there're any mistake I made in the connection,which I mean the newIntent and getIntent inScene2.java GameOver.javaand Scene3.java
Scene2.java
package com.group5.littlered;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
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.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Scene2 extends Activity {
MediaPlayer bird;
MediaPlayer bgm;
int position = 0;
String[] conversation;
TextView frame;
ImageView conframe;
final String[] ListStr = { "Wake up and ask her", "Peek her secretly" };
int plot = 0;
#Override
public void onBackPressed() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_scene2);
Intent intent1 = getIntent();
conversation = getResources().getStringArray(R.array.scene2);
frame = (TextView) findViewById(R.id.textView1);
Button next = (Button) findViewById(R.id.wtf);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (position < 2) {
String sentence = conversation[position];
frame.setText(sentence + "");
position++;
} else {
if (plot < 1) {
AlertDialog choice = new AlertDialog.Builder(
Scene2.this).create();
choice.setTitle("Pick a choice");
choice.setMessage(" ");
choice.setButton("Get up and ask her what happened",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
plot = 1;
}
});
choice.setButton2("Peek her secretly",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
plot = 2;
position = 4;
}
});
choice.show();
} else {
if (plot < 2) {
if (position < 4) {
String sentence = conversation[position];
frame.setText(sentence + "");
position++;
} else {
Intent intent2 = new Intent(Scene2.this,
GameOver.class);
startActivity(intent2);
finish();
}
} else {
if (position < 6) {
String sentence = conversation[position];
frame.setText(sentence + "");
position++;
} else {
Intent intent3 = new Intent(Scene2.this,
Scene3.class);
startActivity(intent3);
finish();
}
}
}
}
}
});
// BGM
bgm = MediaPlayer.create(Scene2.this, R.raw.voyager);
bgm.setLooping(true);
bgm.start();
// bird
bird = MediaPlayer.create(Scene2.this, R.raw.bird);
bird.setLooping(false);
bird.start();
}
}
Scene3.java
package com.group5.littlered;
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.ImageView;
import android.widget.TextView;
public class Scene3 extends Activity {
int position = 0;
String[] conversation;
TextView frame;
ImageView conframe;
#Override
public void onBackPressed() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_scene3);
Intent intent3 = getIntent();
conversation = getResources().getStringArray(R.array.scene1);
frame = (TextView) findViewById(R.id.textView1);
Button next = (Button) findViewById(R.id.wtf);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (position < 6) {
String sentence = conversation[position];
frame.setText(sentence + "");
position++;
} else {
{
}
}
}
});
}
}
Again sorry for my poor ENGLISH, plz tell me what I need to post more to help you understand my problem.
my logcat
04-30 09:37:39.497: E/AndroidRuntime(4862): FATAL EXCEPTION: main
04-30 09:37:39.497: E/AndroidRuntime(4862): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.group5.littlered/com.group5.littlered.Scene3}: java.lang.NullPointerException
04-30 09:37:39.497: E/AndroidRuntime(4862): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-30 09:37:39.497: E/AndroidRuntime(4862): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-30 09:37:39.497: E/AndroidRuntime(4862): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-30 09:37:39.497: E/AndroidRuntime(4862): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-30 09:37:39.497: E/AndroidRuntime(4862): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 09:37:39.497: E/AndroidRuntime(4862): at android.os.Looper.loop(Looper.java:137)
04-30 09:37:39.497: E/AndroidRuntime(4862): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-30 09:37:39.497: E/AndroidRuntime(4862): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 09:37:39.497: E/AndroidRuntime(4862): at java.lang.reflect.Method.invoke(Method.java:525)
04-30 09:37:39.497: E/AndroidRuntime(4862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-30 09:37:39.497: E/AndroidRuntime(4862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-30 09:37:39.497: E/AndroidRuntime(4862): at dalvik.system.NativeStart.main(Native Method)
04-30 09:37:39.497: E/AndroidRuntime(4862): Caused by: java.lang.NullPointerException
04-30 09:37:39.497: E/AndroidRuntime(4862): at com.group5.littlered.Scene3.onCreate(Scene3.java:45)
04-30 09:37:39.497: E/AndroidRuntime(4862): at android.app.Activity.performCreate(Activity.java:5133)
04-30 09:37:39.497: E/AndroidRuntime(4862): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-30 09:37:39.497: E/AndroidRuntime(4862): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-30 09:37:39.497: E/AndroidRuntime(4862): ... 11 more
The line that is crashing is the line 45 of Scene3:
Button next = (Button) findViewById(R.id.wtf);
next.setOnClickListener(new View.OnClickListener() { // <-- THIS ONE
...
});
The cause is a NullPointerException. This means that the identifier "wtf" exists in R (this wouldn't compile otherwise) but is not found in the layer activity_scene3, as we wave the following statement line 38 of Scene3.onCreate():
setContentView(R.layout.activity_scene3); // and later on findViewById() returns `null`
You have to revisit this layout to ensure that the Button you are willing to access to actually exists, with the ID wtf.
Generally speaking, this is the danger in using a same ID in different layouts. This is prone to hide errors that would easily be found otherwise as this would just not compile.
Check your manifest file and add Scene3.java in it
<activity
android:name=".Scene3" >
</activity>
Always post question with exception, second this is may be you have not mention your other activity in manifest file like:
<activity
android:name=".Scene3">
</activity>
<activity
android:name=".GameOver">
</activity>

Android: Quiz App with if else statement

I'm a noob, and i need help developing a simple quiz app with if, else statement. I know i've got some problems with my code, but i don't know how to solve them. Even if it doesn't display any error, the app crashes when i try to open the activity called "FirstImageLogo".
An ImageButton should open it. Inside "FirstImageLogo" i put a TextView and a Button, where the user should write the correct answer. If he does, than the button should open a new activity; if he doesn't, than the button should open a Toast message. The message that the user should write down inside TextView in this case is "Facebook".
Unfortunately i have NO programming knowledge :(
Anyway, here's my "FirstImageLogo.class" code:
private EditText inputtxt;
private Button btnNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_image_logo);
inputtxt = (EditText) findViewById(R.id.text1);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View ContentView) {
// TODO Auto-generated method stub
String name;
name=inputtxt.getText().toString();
if (name.equalsIgnoreCase("facebook"))
{
Intent intent = new Intent (FirstImageLogo.this, GoodJob.class);
startActivity(intent);
}
else
{ Toast.makeText(getApplicationContext(), "Sorry, wrong answer. Try Again!", Toast.LENGTH_SHORT).show();
}
}
});
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first_image_logo, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
And here is the log file:
01-17 21:35:39.365: D/dalvikvm(19074): GC_FOR_ALLOC freed 68K, 1% free 17135K/17236K, paused 14ms, total 14ms
01-17 21:35:39.905: D/dalvikvm(19074): GC_FOR_ALLOC freed 21K, 1% free 17547K/17604K, paused 7ms, total 7ms
01-17 21:35:39.915: D/AndroidRuntime(19074): Shutting down VM
01-17 21:35:39.915: W/dalvikvm(19074): threadid=1: thread exiting with uncaught exception (group=0x41596ba8)
01-17 21:35:39.915: E/AndroidRuntime(19074): FATAL EXCEPTION: main
01-17 21:35:39.915: E/AndroidRuntime(19074): Process: com.example.appquiz, PID: 19074
01-17 21:35:39.915: E/AndroidRuntime(19074): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appquiz/com.example.appquiz.FirstImageLogo}: java.lang.NullPointerException
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.os.Handler.dispatchMessage(Handler.java:102)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.os.Looper.loop(Looper.java:136)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-17 21:35:39.915: E/AndroidRuntime(19074): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 21:35:39.915: E/AndroidRuntime(19074): at java.lang.reflect.Method.invoke(Method.java:515)
01-17 21:35:39.915: E/AndroidRuntime(19074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-17 21:35:39.915: E/AndroidRuntime(19074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-17 21:35:39.915: E/AndroidRuntime(19074): at dalvik.system.NativeStart.main(Native Method)
01-17 21:35:39.915: E/AndroidRuntime(19074): Caused by: java.lang.NullPointerException
01-17 21:35:39.915: E/AndroidRuntime(19074): at com.example.appquiz.FirstImageLogo.onCreate(FirstImageLogo.java:28)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.Activity.performCreate(Activity.java:5231)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-17 21:35:39.915: E/AndroidRuntime(19074): ... 11 more
Thank you so much for your time.
you never initialized btnNext. use the following
btnNext=(Button) findViewById(R.id.id_of_button);
then use setOnClickListener
You never instantiate your btnNext object.
Hence when doing btnNext.setOnClickListener, it throws a NullPointerException.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_image_logo);
inputtxt = (EditText) findViewById(R.id.text1);
btnNext = (Button) findViewById(R.id.btnNext);//add this line
The problem is here
btnNext.setOnClickListener(new View.OnClickListener()
btnNext is null because you haven't initialized it.
setContentView(R.layout.activity_first_image_logo);
inputtxt = (EditText) findViewById(R.id.text1);
btnNext = (Button) findViewById(R.id.idOfButton); // you need this
btnNext.setOnClickListener(new View.OnClickListener()

Getting Integer Value From a TextField

I'm trying to take an RGB value (not hexadecimal, the actual constant value, such as -16711936) from a textfield and display it in a new activity (to use it as an integer). I attempted to do this by converting the string to an integer, but it still ends up crashing the program. Here's the code:
FULL SOURCE:issue occurs in testvalue function
package com.example.seniordesign;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity { //1
//public final static String RGB_VALUE = "com.example.seniordesign.RGB_VALUE_USE";
TextView pixelcord, rgbvals, rgbnumvals, red, green, blue, RGB;
ImageView iv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) { //2
/* GATHER THE INFORMATION FROM THE LAYOUT TO ORGANIZE APP*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* SET ON CLICK LISTENER TO GET CLICK REPSONSE -- LAUNCH CAMERA AND TAKE PHOTO */
/* SET VARIABLES FOR USE FROM EACH VIEW */
iv = (ImageView) findViewById(R.id.imageView); /* USE IMAGE VIEW FIELD*/
pixelcord = (TextView)findViewById(R.id.pixelcord); /* USE TEXT FIELD FOR PIXEL */
rgbvals = (TextView)findViewById(R.id.rgbvals); /* USE TEXT FIELD FOR RGB VALUES*/
btn = (Button) findViewById(R.id.takePhoto);
/* SET INFORMATION OF WHAT TO DO UPON EACH CLIK*/
iv.setOnTouchListener(imgSourceOnTouchListener);
red = (TextView)findViewById(R.id.red);
blue = (TextView)findViewById(R.id.blue);
green = (TextView)findViewById(R.id.green);
RGB = (TextView)findViewById(R.id.RGB);
/* =====================================CAMERA BUTTON=====================================*/
btn.setOnClickListener(new OnClickListener() { //3
#Override
public void onClick(View v) { //4
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
} //*4
}); //*3
/* =======================================================================================*/
} /* END OF ON CREATE*/ //*2
/* DECLARATION OF IMG TOUCH FUNCTION*/
OnTouchListener imgSourceOnTouchListener = new OnTouchListener() { //5
#Override
public boolean onTouch(View view, MotionEvent event) { //6
float eventX = event.getX();
float eventY = event.getY();
float[] eventXY = new float[] { eventX, eventY};
Matrix invertMatrix = new Matrix();
((ImageView)view).getImageMatrix().invert(invertMatrix);
invertMatrix.mapPoints(eventXY);
int x = Integer.valueOf((int)eventXY[0]); /* POTENTIALLY REDUNDANT*/
int y = Integer.valueOf((int)eventXY[1]);
/* CHECK TO MAKE SURE VALUES ARE WITHIN BITMAP RANGE*/
/* SET TEXT FUNCTION TO THE FIELD USING SET TEXT METHOD*/
pixelcord.setText("X:" + String.valueOf(eventX) + "/ Y:" + String.valueOf(eventY) );
Bitmap bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();
int touchedRGB = bitmap.getPixel(x,y);
rgbvals.setText("Color Value" + "#" + Integer.toHexString(touchedRGB));
rgbvals.setTextColor(touchedRGB);
int rval = Color.red(touchedRGB);
int bval = Color.blue(touchedRGB);
int gval = Color.green(touchedRGB);
red.setText(String.valueOf(rval));
blue.setText(String.valueOf(bval));
green.setText(String.valueOf(gval));
RGB.setText(String.valueOf(touchedRGB));
return true;
} //*6
}; //*5
public void testvalue(View view) {
Intent intent = new Intent(this, TestValue.class);
TextView RGBval = (TextView) findViewById(R.id.RGB);
int rgb_val_use = Integer.parseInt((String) RGBval.getText().toString());
intent.putExtra("RGB_VALUE", (int)rgb_val_use);
startActivity(intent);
}
#Override
public void onActivityResult( int requestCode, int resultCode, Intent data)
{ //7
if(requestCode == 0)
{ //8
Bitmap theImage = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(theImage);
} //*8
} //*7
} //*1
and here is my logCat:
10-31 07:27:44.334: E/AndroidRuntime(9915): FATAL EXCEPTION: main
10-31 07:27:44.334: E/AndroidRuntime(9915): java.lang.IllegalStateException: Could not execute method of the activity
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.view.View$1.onClick(View.java:2144)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.view.View.performClick(View.java:2485)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.view.View$PerformClick.run(View.java:9089)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.os.Handler.handleCallback(Handler.java:587)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.os.Handler.dispatchMessage(Handler.java:92)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.os.Looper.loop(Looper.java:123)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.app.ActivityThread.main(ActivityThread.java:3806)
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.reflect.Method.invoke(Method.java:507)
10-31 07:27:44.334: E/AndroidRuntime(9915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-31 07:27:44.334: E/AndroidRuntime(9915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-31 07:27:44.334: E/AndroidRuntime(9915): at dalvik.system.NativeStart.main(Native Method)
10-31 07:27:44.334: E/AndroidRuntime(9915): Caused by: java.lang.reflect.InvocationTargetException
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.reflect.Method.invoke(Method.java:507)
10-31 07:27:44.334: E/AndroidRuntime(9915): at android.view.View$1.onClick(View.java:2139)
10-31 07:27:44.334: E/AndroidRuntime(9915): ... 11 more
10-31 07:27:44.334: E/AndroidRuntime(9915): Caused by: java.lang.NumberFormatException: unable to parse 'RGB' as integer
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.Integer.parse(Integer.java:383)
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.Integer.parseInt(Integer.java:372)
10-31 07:27:44.334: E/AndroidRuntime(9915): at java.lang.Integer.parseInt(Integer.java:332)
10-31 07:27:44.334: E/AndroidRuntime(9915): at com.example.seniordesign.MainActivity.testvalue(MainActivity.java:125)
10-31 07:27:44.334: E/AndroidRuntime(9915): ... 14 more
Is it just a syntax error or am I going about this incorrectly? I appreciate the help
NOTE: The value in the textfield is techinically a string I guess, this is the code I used to place it in the field (touchedRGB is an integer value).
RGB.setText(String.valueOf(touchedRGB));
If it makes a difference, this is the XML from this particular section
<TextView
android:id="#+id/RGB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/green"
android:layout_alignBottom="#+id/green"
android:layout_marginLeft="40dp"
android:layout_toRightOf="#+id/green"
android:text="RGB" />
The problem is, you have a string("RGB") in your textView. So you got a NumberFormatException.
Set an integet value in your textView and try again.
java.lang.NumberFormatException: unable to parse 'RGB' as integer
Edit: Could you try code below?
public void testvalue(View view) {
try{
TextView RGBval = (TextView) findViewById(R.id.RGB);
String valueStoredInRGBvalTextView = RGBval.getText().toString();
int rgb_val_use = Integer.parseInt(valueStoredInRGBvalTextView);
Intent intent = new Intent(this, TestValue.class);
intent.putExtra("RGB_VALUE", (int)rgb_val_use);
startActivity(intent);
} catch (Exception e) {
// Not touched yet
}
}
Edit2:
You are setting the integer touch value to RGB textView after first touch occurs. If you invoke your testvalue method before touching anywhere, you'll get this exception. Change your testvalue method as I wrote above. Your problem will be solved.
try this you cannot cast the int value to String like this -> (int)(rgb_val_use)
change it into new Integer.toString(rgb_val_use);
intent.putExtra("RGB_VALUE",new Integer.toString(rgb_val_use));
Integer rgb = Integer.getInteger((String) RGBval.getText(), null);
if (rgb == null) {
// input string is not integer
} else {
// use rgb to start new activity
}

how to show popup window in my main window?

hi friends i download one popup window sample source code...its working fine...when i run this application emulator screen display one button, if i click that button popup window shown in bottom.if i click again same button its dismiss the popup window...but,
i expect when my application open i need static popup window, no need this button. then i click softkey board(computer keyboard)f2 button, the popup window i want to dismiss...thats all if anyone know please help me......
This is my source code:
package popupTest.popupTest;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class popupTest extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}
logcat error:
08-23 16:38:23.771: ERROR/AndroidRuntime(433): FATAL EXCEPTION: main
08-23 16:38:23.771: ERROR/AndroidRuntime(433): java.lang.RuntimeException: Unable to start activity ComponentInfo{popupTest.popupTest/popupTest.popupTest.popupTest}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.os.Looper.loop(Looper.java:123)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at java.lang.reflect.Method.invoke(Method.java:521)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at dalvik.system.NativeStart.main(Native Method)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.ViewRoot.setView(ViewRoot.java:505)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.widget.PopupWindow.showAtLocation(PopupWindow.java:688)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at popupTest.popupTest.popupTest.onCreate(popupTest.java:49)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-23 16:38:23.771: ERROR/AndroidRuntime(433): ... 11 more
08-23 16:38:23.820: WARN/ActivityManager(59): Force finishing activity popupTest.popupTest/.popupTest
put the code outside onClick in onCreate ....and use setOnclickListener to remove ur popup..
I have edited your code and it will show the pop up on the start of the activity:
package popupTest.popupTest;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class popupTest extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Click Me");
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
public void run() {
// TODO Auto-generated method stub
popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
}
}, 1000);
//Use this to dismiss as per your need...
// popUp.dismiss();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
popUp.dismiss();
return false;
}
}

Categories