My app is crashing and can't figure why - java

Ok so I am following mybringback's tutorial and where I am setting a background image with bitmap I get an error so I looked here and I used the code someone gave saw that I had no errors so I run the app and when I select an image it crashes
Here is my activity:
package com.odysseus.thebasics;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class TutorialThree extends Activity implements OnClickListener {
MediaPlayer buttonSound;
ImageView display;
int toPhone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
final MediaPlayer buttonSound = MediaPlayer.create(TutorialThree.this, R.raw.button_sound);
toPhone = R.drawable.image1;
display = (ImageView) findViewById(R.id.IVdisplay);
ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
Button setWall = (Button) findViewById(R.id.setBackround);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
setWall.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.IVimage1:
display.setImageResource(R.drawable.image5);
toPhone = R.drawable.image1;
break;
case R.id.IVimage2:
display.setImageResource(R.drawable.image2);
toPhone = R.drawable.image2;
break;
case R.id.IVimage3:
display.setImageResource(R.drawable.image3);
toPhone = R.drawable.image3;
break;
case R.id.IVimage4:
display.setImageResource(R.drawable.image4);
toPhone = R.drawable.image4;
break;
case R.id.setBackround:
InputStream decode = getResources().openRawResource(toPhone);
Bitmap background = BitmapFactory.decodeStream(decode); //this before was InputStream ....
WallpaperManagermyWallpaperManager=WallpaperManager.getInstance(getApplicationContext()); //error used to be here before i had getApplicationContext().setWallpaper(background);
try{
myWallpaperManager.setBitmap(background);
}catch(IOException e){
e.printStackTrace();
Toast error = Toast.makeText(TutorialThree.this, "Oops, couldn't set image", Toast.LENGTH_LONG);
error.show();
}
break;
}
buttonSound.start();
}
}
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/IVdisplay"
android:layout_width="200dp"
android:layout_height="159dp"
android:layout_gravity="center"
android:src="#drawable/image5" />
<Button
android:id="#+id/setBackround"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Image To Background" />
<HorizontalScrollView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/IVimage1"
android:layout_width="125dp"
android:layout_height="200dp"
android:padding="15dp"
android:src="#drawable/image5" />
<ImageView
android:id="#+id/IVimage2"
android:layout_width="125dp"
android:layout_height="200dp"
android:padding="15dp"
android:src="#drawable/image2" />
<ImageView
android:id="#+id/IVimage3"
android:layout_width="125dp"
android:layout_height="200dp"
android:padding="15dp"
android:src="#drawable/image3" />
<ImageView
android:id="#+id/IVimage4"
android:layout_width="125dp"
android:layout_height="200dp"
android:padding="15dp"
android:src="#drawable/image4" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
My logcat:
01-09 20:03:03.960: V/MediaPlayer(29999): getDuration
01-09 20:03:03.960: V/MediaPlayer(29999): getCurrentPosition
01-09 20:03:03.970: D/MediaPlayer(29999): start() mUri is null
01-09 20:03:03.980: I/MediaPlayer(29999): mContext.toString() = com.odysseus.thebasics.menu#422c3b50
01-09 20:03:03.980: I/MediaPlayer(29999): setLGSoundNormalizerOnOff(1)
01-09 20:03:03.980: V/MediaPlayer(29999): start
01-09 20:03:04.020: V/MediaPlayer(29999): message received msg=4, ext1=0, ext2=0
01-09 20:03:04.020: V/MediaPlayer(29999): Received seek complete
01-09 20:03:04.020: V/MediaPlayer(29999): All seeks complete - return to regularly scheduled program
01-09 20:03:04.020: V/MediaPlayer(29999): callback application
01-09 20:03:04.020: V/MediaPlayer(29999): back from callback
01-09 20:03:04.040: V/MediaPlayer(29999): getDuration
01-09 20:03:04.040: V/MediaPlayer(29999): getCurrentPosition
01-09 20:03:04.040: V/MediaPlayer(29999): isPlaying: 1
01-09 20:03:04.590: V/MediaPlayer(29999): message received msg=2, ext1=0, ext2=0
01-09 20:03:04.590: V/MediaPlayer(29999): playback complete
01-09 20:03:04.590: V/MediaPlayer(29999): callback application
01-09 20:03:04.590: V/MediaPlayer(29999): back from callback
01-09 20:03:04.590: E/MediaPlayer(29999): ##### MEDIA_PLAYBACK_COMPLETE
01-09 20:03:04.590: V/MediaPlayer(29999): getDuration
01-09 20:03:04.590: V/MediaPlayer(29999): getCurrentPosition
01-09 20:03:04.590: D/MediaPlayer(29999): start() mUri is null
01-09 20:03:06.650: D/dalvikvm(29999): GC_CONCURRENT freed 76K, 5% free 6944K/7303K, paused 20ms+4ms, total 86ms
01-09 20:03:06.650: D/dalvikvm(29999): WAIT_FOR_CONCURRENT_GC blocked 45ms
01-09 20:03:06.750: D/dalvikvm(29999): GC_FOR_ALLOC freed 679K, 14% free 6646K/7687K, paused 23ms, total 23ms
01-09 20:03:06.820: D/dalvikvm(29999): GC_CONCURRENT freed 196K, 10% free 6931K/7687K, paused 21ms+2ms, total 58ms
01-09 20:03:06.820: D/dalvikvm(29999): WAIT_FOR_CONCURRENT_GC blocked 40ms
01-09 20:03:06.820: V/MediaPlayer(29999): constructor
01-09 20:03:06.820: V/MediaPlayer(29999): setListener
01-09 20:03:06.820: V/MediaPlayer(29999): setDataSource(51, 3706, 13890)
01-09 20:03:06.840: V/MediaPlayer(29999): setVideoSurfaceTexture
01-09 20:03:06.840: V/MediaPlayer(29999): prepare
01-09 20:03:06.840: V/MediaPlayer(29999): message received msg=200, ext1=1, ext2=9120
01-09 20:03:06.840: W/MediaPlayer(29999): info/warning (1, 9120)
01-09 20:03:06.840: V/MediaPlayer(29999): callback application
01-09 20:03:06.840: V/MediaPlayer(29999): back from callback
01-09 20:03:06.890: V/MediaPlayer(29999): message received msg=5, ext1=0, ext2=0
01-09 20:03:06.890: V/MediaPlayer(29999): New video size 0 x 0
01-09 20:03:06.890: V/MediaPlayer(29999): callback application
01-09 20:03:06.890: V/MediaPlayer(29999): back from callback
01-09 20:03:06.890: V/MediaPlayer(29999): message received msg=1, ext1=0, ext2=0
01-09 20:03:06.890: V/MediaPlayer(29999): prepared
01-09 20:03:06.890: V/MediaPlayer(29999): signal application thread
01-09 20:03:06.890: V/MediaPlayer(29999): prepare complete - status=0
01-09 20:03:06.900: V/MediaPlayer(29999): callback application
01-09 20:03:06.900: V/MediaPlayer(29999): back from callback
01-09 20:03:06.910: I/MediaPlayer(29999): Info (1,9120)
01-09 20:03:10.300: W/dalvikvm(29999): threadid=1: thread exiting with uncaught exception (group=0x41c2e438)
01-09 20:03:10.330: E/AndroidRuntime(29999): FATAL EXCEPTION: main
01-09 20:03:10.330: E/AndroidRuntime(29999): java.lang.NullPointerException
01-09 20:03:10.330: E/AndroidRuntime(29999): at com.odysseus.thebasics.TutorialThree.onClick(TutorialThree.java:79)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.view.View.performClick(View.java:4101)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.view.View$PerformClick.run(View.java:17082)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.os.Handler.handleCallback(Handler.java:615)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.os.Looper.loop(Looper.java:137)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.app.ActivityThread.main(ActivityThread.java:4940)
01-09 20:03:10.330: E/AndroidRuntime(29999): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 20:03:10.330: E/AndroidRuntime(29999): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 20:03:10.330: E/AndroidRuntime(29999): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
01-09 20:03:10.330: E/AndroidRuntime(29999): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
01-09 20:03:10.330: E/AndroidRuntime(29999): at dalvik.system.NativeStart.main(Native Method)
You can run it yourself to see also I am new to android so please understand! Thanks in advance

MediaPlayer buttonSound; // already declared but not initialized
ImageView display;
int toPhone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
final MediaPlayer buttonSound = MediaPlayer.create(TutorialThree.this, R.raw.button_sound);
// again declared and initialized. local to onCreate.
Change to
buttonSound = MediaPlayer.create(TutorialThree.this, R.raw.button_sound);
This
buttonSound.start();
is probably line 79 TutorialThree.java which causes NUllPointerException.

Related

Android: Uncaught exception causing fatal exception in main

Hi I am writing a simple app that interacts with androids camera API directly, I'm using this tutorial http://www.tutorialspoint.com/android/android_camera.htm
Could anyone show me where I am going wrong?
The log output:
01-09 23:42:56.901: D/ATRecorder(886): com.htc.autotest.dlib.RecordEngine in loader dalvik.system.DexClassLoader#40528bb8
01-09 23:42:57.161: D/AndroidRuntime(886): Shutting down VM
01-09 23:42:57.161: W/dalvikvm(886): threadid=1: thread exiting with uncaught exception (group=0x400205a0)
01-09 23:42:57.171: E/AndroidRuntime(886): FATAL EXCEPTION: main
01-09 23:42:57.171: E/AndroidRuntime(886): java.lang.RuntimeException: startPreview failed
01-09 23:42:57.171: E/AndroidRuntime(886): at android.hardware.Camera.startPreview(Native Method)
01-09 23:42:57.171: E/AndroidRuntime(886): at com.example.camera1.ShowCamera.surfaceCreated(ShowCamera.java:30)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.SurfaceView.updateWindow(SurfaceView.java:551)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.View.draw(View.java:6973)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.View.draw(View.java:6973)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-09 23:42:57.171: E/AndroidRuntime(886): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1997)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewRoot.draw(ViewRoot.java:1600)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewRoot.performTraversals(ViewRoot.java:1321)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewRoot.handleMessage(ViewRoot.java:1957)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.os.Looper.loop(Looper.java:150)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.app.ActivityThread.main(ActivityThread.java:4277)
01-09 23:42:57.171: E/AndroidRuntime(886): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 23:42:57.171: E/AndroidRuntime(886): at java.lang.reflect.Method.invoke(Method.java:507)
01-09 23:42:57.171: E/AndroidRuntime(886): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 23:42:57.171: E/AndroidRuntime(886): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 23:42:57.171: E/AndroidRuntime(886): at dalvik.system.NativeStart.main(Native Method)
I've followed it perfectly but I still get and uncaught exception in log-cat when I try to run it, here is the code in MainActivity:
package com.example.camera1;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Camera cameraObject;
private ShowCamera showCamera;
private ImageView pic;
public static Camera isCameraAvailiable(){
Camera object = null;
try {
object = Camera.open();
}
catch (Exception e){
}
return object;
}
private PictureCallback capturedIt = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
if(bitmap==null){
Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();
}
cameraObject.release();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pic = (ImageView)findViewById(R.id.imageView1);
cameraObject = isCameraAvailiable();
showCamera = new ShowCamera(this, cameraObject);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
public void snapIt(View view){
cameraObject.takePicture(null, null, capturedIt);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is the ShowCamera class:
package com.example.camera1;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holdMe;
private Camera theCamera;
public ShowCamera(Context context,Camera camera) {
super(context);
theCamera = camera;
holdMe = getHolder();
holdMe.addCallback(this);
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
theCamera.setPreviewDisplay(holder);
theCamera.startPreview();
} catch (IOException e) {
}
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
}
There may be different reasons for Camera.startPreview() to throw RuntimeException. For example, it could be by set of preview size not to one of supported preview sizes.
Try to catch a top level Exception in the surfaceCreated method (instead of IOException) and call e.getMessage() in the exception block to get more information on the type of error.

Dialog crashing when TextView set

I have the following code which is called by the onClick event of buttons contained within a ListView.
public void viewExerciseHistory(View view) {
// History button click handler
LinearLayout parentRow = (LinearLayout)view.getParent();
TextView exerciseNameTextView = (TextView)parentRow.getChildAt(0);
String exerciseName = (String)exerciseNameTextView.getText();
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.exercise_history_dialog);
dialog.setTitle(exerciseName);
TextView headerTextView = (TextView)view.findViewById(R.id.exercise_history_dialog_name_textview);
//headerTextView.setText("History");
Button closeDialogButton = (Button)view.findViewById(R.id.exercise_history_dialog_close_button);
//closeDialogButton.setOnClickListener(new OnClickListener() {
// #Override
// public void onClick(View v) {
// dialog.dismiss();
// }
//});
dialog.show();
}
The ListView row is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/exercise_history_dialog_name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textSize="15sp" />
<Button android:id="#+id/exercise_history_dialog_close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:typeface="monospace"
android:textSize="12sp"
android:textColor="#color/button_blue_text"
android:text="#string/button_text_close"
style="?android:attr/borderlessButtonStyle" />
</RelativeLayout>
The above code works fine and displays the dialog, but if uncomment out the headerTextView.setText("History") or the closeDialog.setOnClickListener it crashes with a NullPointerException.
Can anyone explain what I'm doing wrong?
EDIT LogCat:
10-14 21:36:19.755: W/IInputConnectionWrapper(26167): getTextBeforeCursor on inactive InputConnection
10-14 21:36:19.755: W/IInputConnectionWrapper(26167): getTextAfterCursor on inactive InputConnection
10-14 21:36:19.915: W/IInputConnectionWrapper(26167): getTextBeforeCursor on inactive InputConnection
10-14 21:36:19.945: W/IInputConnectionWrapper(26167): getTextAfterCursor on inactive InputConnection
10-14 21:36:20.435: D/AndroidRuntime(26167): Shutting down VM
10-14 21:36:20.435: W/dalvikvm(26167): threadid=1: thread exiting with uncaught exception (group=0x40e27930)
10-14 21:36:20.445: E/AndroidRuntime(26167): FATAL EXCEPTION: main
10-14 21:36:20.445: E/AndroidRuntime(26167): java.lang.IllegalStateException: Could not execute method of the activity
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$1.onClick(View.java:3599)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View.performClick(View.java:4204)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$PerformClick.run(View.java:17355)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Handler.handleCallback(Handler.java:725)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.os.Looper.loop(Looper.java:137)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-14 21:36:20.445: E/AndroidRuntime(26167): at dalvik.system.NativeStart.main(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): Caused by: java.lang.reflect.InvocationTargetException
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 21:36:20.445: E/AndroidRuntime(26167): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 21:36:20.445: E/AndroidRuntime(26167): at android.view.View$1.onClick(View.java:3594)
10-14 21:36:20.445: E/AndroidRuntime(26167): ... 11 more
10-14 21:36:20.445: E/AndroidRuntime(26167): Caused by: java.lang.NullPointerException
10-14 21:36:20.445: E/AndroidRuntime(26167): at com.example.workoutlog.ManageWorkouts.viewExerciseHistory(ManageWorkouts.java:269)
10-14 21:36:20.445: E/AndroidRuntime(26167): ... 14 more
It's crashing because this line:
TextView headerTextView = (TextView) findViewById(R.id.exercise_history_dialog_name_textview);
is actually returning null.
The findViewById method is looking for the view on the main activity's content view. Instead you want to use the findViewById method on the view object. Calling findViewById on the view object will allow you to get the fields you are after.
dialog.setContentView(R.layout.exercise_history_dialog);
TextView headerTextView = (TextView) dialog.findViewById(R.id.exercise_history_dialog_name_textview);
headerTextView.setText("History");
Try changing these
TextView headerTextView = (TextView)findViewById(R.id.exercise_history_dialog_name_textview);
Button closeDialogButton = (Button)findViewById(R.id.exercise_history_dialog_close_button);
to
TextView headerTextView = (TextView)dialog.findViewById(R.id.exercise_history_dialog_name_textview);
Button closeDialogButton = (Button)dialog.findViewById(R.id.exercise_history_dialog_close_button);
You have to tell which view you want to findViewById from

Android: Using array adapter to display a list array

I'm not getting a direct error in Eclipse.. but when trying to run this on my phone it doesn't open the activity and then my phone resets. I might be using the array adapter wrong.. but here's my code files:
Java File
package creativecoders.periodictable;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AM extends Activity {
private ListView amList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.am);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
#Override
public void onStart() {
super.onStart();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.AM, android.R.layout.simple_list_item_1);
amList.setAdapter(adapter);
}
public void onPause() {
super.onPause();
finish();
}
public void onStop() {
super.onStop();
finish();
}
public void onDestroy() {
super.onDestroy();
finish();
}
}
XML Layout File
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
String File
<string-array name="AM">
<item> ONE </item>
<item> TWO </item>
<item> THREE </item>
</string-array>
EDIT: Log file:
04-20 23:10:07.660: D/AndroidRuntime(284): Shutting down VM
04-20 23:10:07.660: W/dalvikvm(284): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-20 23:10:07.670: E/AndroidRuntime(284): FATAL EXCEPTION: main
04-20 23:10:07.670: E/AndroidRuntime(284): java.lang.RuntimeException: Unable to start activity ComponentInfo{creativecoders.periodictable/creativecoders.periodictable.AM}: java.lang.NullPointerException
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.os.Looper.loop(Looper.java:123)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-20 23:10:07.670: E/AndroidRuntime(284): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 23:10:07.670: E/AndroidRuntime(284): at java.lang.reflect.Method.invoke(Method.java:521)
04-20 23:10:07.670: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-20 23:10:07.670: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-20 23:10:07.670: E/AndroidRuntime(284): at dalvik.system.NativeStart.main(Native Method)
04-20 23:10:07.670: E/AndroidRuntime(284): Caused by: java.lang.NullPointerException
04-20 23:10:07.670: E/AndroidRuntime(284): at creativecoders.periodictable.AM.onStart(AM.java:28)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.Activity.performStart(Activity.java:3781)
04-20 23:10:07.670: E/AndroidRuntime(284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
04-20 23:10:07.670: E/AndroidRuntime(284): ... 11 more
you did not initialize the ListView. in onCreate()
amList = (ListView)findViewById(R.id.listView1);

Android.widget textView to android.widget.button

I am new to Android. I am trying to build this tiny app however every time I add the code in bold:
d = (Button) findViewById (R.id.tvDis);
the app crashes and if I remove this line, everything works fine. I am runing this on android 2.2 api 8 version and nexus s emulator.
package com.maximusstudios.numtowords;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
/**Called when the activity is first created.*/
int counter;
Button add, sub;
TextView d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById (R.id.bAdd);
**d = (Button) findViewById (R.id.tvDis);**
sub = (Button) findViewById(R.id.bSub);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your Total is 0"
android:textSize="20dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDis"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bAdd"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bSub"
/>
</LinearLayout>
Error list:
02-10 18:00:21.452: D/AndroidRuntime(910): Shutting down VM
02-10 18:00:21.452: W/dalvikvm(910): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-10 18:00:21.472: E/AndroidRuntime(910): FATAL EXCEPTION: main
02-10 18:00:21.472: E/AndroidRuntime(910): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.maximusstudios.numtowords/com.maximusstudios.numtowords.MainActivity}: java.lang.ClassCastException: android.widget.TextView
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.os.Looper.loop(Looper.java:123)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-10 18:00:21.472: E/AndroidRuntime(910): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 18:00:21.472: E/AndroidRuntime(910): at java.lang.reflect.Method.invoke(Method.java:521)
02-10 18:00:21.472: E/AndroidRuntime(910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-10 18:00:21.472: E/AndroidRuntime(910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-10 18:00:21.472: E/AndroidRuntime(910): at dalvik.system.NativeStart.main(Native Method)
02-10 18:00:21.472: E/AndroidRuntime(910): Caused by: java.lang.ClassCastException: android.widget.TextView
02-10 18:00:21.472: E/AndroidRuntime(910): at com.maximusstudios.numtowords.MainActivity.onCreate(MainActivity.java:23)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-10 18:00:21.472: E/AndroidRuntime(910): ... 11 more
Change to
d = (TextView) findViewById (R.id.tvDis);
Your casting was wrong. With your code, when you do (Button), you are trying to cast the TextView that is returned by findViewById() into a Button, which doesn't work due to them being incompatible types.
You define d as a TextView and try to cast it to a Button
Should be
d = (TextView) findViewById (R.id.tvDis);
The error you're getting is a ClassCastException

writing content in an Excel sheet

I am developing an application in android and I have a requirement to read and write from an excel file stored in sdcard.I am using jxl library for this purpose. But I am getting some errors with file operation.
ext = Environment.getExternalStorageDirectory().getAbsolutePath()+"/hai/dynamic.xls";
setOutputFile(ext);
try {
write();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setOutputFile(String inputFile) {
this.inputfile = inputFile;
}
public void write() throws IOException, WriteException {
File file = new File(inputfile);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);
workbook.write();
workbook.close();
}
Then I am getting this following error in logcat
01-09 12:43:12.824: W/System.err(12321): java.io.FileNotFoundException:/mnt/sdcard/hai/dynamic.xls (Permission denied)
01-09 12:43:12.884: W/System.err(12321): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
01-09 12:43:12.884: W/System.err(12321): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
01-09 12:43:12.904: W/System.err(12321): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
01-09 12:43:12.904: W/System.err(12321): at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
01-09 12:43:12.904: W/System.err(12321): at jxl.Workbook.createWorkbook(Workbook.java:301)
01-09 12:43:12.904: W/System.err(12321): at com.fis.excel.WriteAcivity.write(WriteAcivity.java:59)
01-09 12:43:12.904: W/System.err(12321): at com.fis.excel.WriteAcivity.onCreate(WriteAcivity.java:40)
01-09 12:43:12.904: W/System.err(12321): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-09 12:43:12.904: W/System.err(12321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-09 12:43:12.916: W/System.err(12321): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-09 12:43:12.916: W/System.err(12321): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-09 12:43:12.924: W/System.err(12321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-09 12:43:12.924: W/System.err(12321): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 12:43:12.924: W/System.err(12321): at android.os.Looper.loop(Looper.java:123)
01-09 12:43:12.924: W/System.err(12321): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-09 12:43:12.924: W/System.err(12321): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 12:43:12.944: W/System.err(12321): at java.lang.reflect.Method.invoke(Method.java:507)
01-09 12:43:12.944: W/System.err(12321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 12:43:12.944: W/System.err(12321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 12:43:12.944: W/System.err(12321): at dalvik.system.NativeStart.main(Native Method)
//add use permission in your manifest
WRITE_EXTERNAL_STORAGE
Allows an application to write to external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Categories