Android App crashes at startup - Java.lang.RuntimeException - java

My drawing app is crashing on start up when I try to add image Buttons outside of the table in the bottom linear layout. Can anyone point me in the right direction?
XML:
<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">
<com.sothree.slidinguppanel.SlidingUpPanelLayout
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFCCCCCC"
android:orientation="vertical"
tools:context=".MainActivity" >
<!-- Top Buttons -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/new_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/start_new"
android:src="#drawable/new_pic" />
<ImageButton
android:id="#+id/draw_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/brush"
android:src="#drawable/brush" />
<ImageButton
android:id="#+id/erase_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/erase"
android:src="#drawable/eraser" />
<ImageButton
android:id="#+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/save"
android:src="#drawable/save" />
</LinearLayout>
<!-- Custom View -->
<com.example.drawingfun.DrawingView
android:id="#+id/drawing"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FFFFFFFF"
android:scaleType="centerInside" />
</LinearLayout>
<!-- Color Palette -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:clickable="true">
<!-- Top Row -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TableRow>
<LinearLayout
android:id="#+id/paint_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background = "#drawable/colorbutton1"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton1"
android:tag="#FFE21A1A" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton2"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton2"
android:tag="#FFF2D820" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton3"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton3"
android:tag="#FF35EF22" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton4"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton4"
android:tag="#FF26D3EA" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton5"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton5"
android:tag="#FFF73E9B" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton6"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton6"
android:tag="#FFFFFFFF" />
</LinearLayout>
</TableRow>
<!-- Bottom Row -->
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton7"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton7"
android:tag="#FF7A4426"/>
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton8"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton8"
android:tag="#FFF74F1C"/>
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton9"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton9"
android:tag="#FF217C14" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton10"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton10"
android:tag="#FF312EBC"/>
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton11"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton11"
android:tag="#FF5D1F93" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton12"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton12"
android:tag="#FF000000"/>
</LinearLayout>
</TableRow>
</TableLayout>
<ImageButton
android:id="#+id/botDrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:gravity="center_horizontal"
android:background = "#drawable/sendbutton"
android:contentDescription="#string/drag"
android:src="#drawable/sendbutton"
android:tag="#FFE21A1A" />
</LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
</RelativeLayout>
Java:
package com.example.drawingfun;
import java.util.UUID;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableRow;
import android.widget.TableLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import com.sothree.slidinguppanel.SlidingUpPanelLayout.PanelSlideListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
/**
* This is demo code to accompany the Mobiletuts+ tutorial series:
* - Android SDK: Create a Drawing App
*
* Sue Smith
* August 2013
*
*/
public class MainActivity extends Activity implements OnClickListener {
//custom drawing view
private DrawingView drawView;
private View botDrag;
//buttons
private ImageButton currPaint, drawBtn, eraseBtn, newBtn, saveBtn;
//sizes
private float mediumBrush;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get drawing view
drawView = (DrawingView)findViewById(R.id.drawing);
//get bottom panel drag view
botDrag = (View)findViewById(R.id.botDrag);
//get the palette and first color button
LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
currPaint = (ImageButton)paintLayout.getChildAt(0);
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
//Send current brush size to color palette
//sizes from dimensions
mediumBrush = getResources().getInteger(R.integer.medium_size);
//draw button
drawBtn = (ImageButton)findViewById(R.id.draw_btn);
drawBtn.setOnClickListener(this);
//set initial size
drawView.setBrushSize(mediumBrush);
//erase button
eraseBtn = (ImageButton)findViewById(R.id.erase_btn);
eraseBtn.setOnClickListener(this);
//new button
newBtn = (ImageButton)findViewById(R.id.new_btn);
newBtn.setOnClickListener(this);
//save button
saveBtn = (ImageButton)findViewById(R.id.save_btn);
saveBtn.setOnClickListener(this);
SlidingUpPanelLayout layout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
layout.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow));
layout.setAnchorPoint(0.3f);
layout.setDragView(botDrag);
layout.setPanelSlideListener(new PanelSlideListener() {
#Override
public void onPanelSlide(View panel, float slideOffset) {
if (slideOffset < 0.2) {
if (getActionBar().isShowing()) {
getActionBar().hide();
}
} else {
if (!getActionBar().isShowing()) {
getActionBar().show();
}
}
}
#Override
public void onPanelExpanded(View panel) {
}
#Override
public void onPanelCollapsed(View panel) {
}
#Override
public void onPanelAnchored(View panel) {
}
});
}
#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;
}
//user clicked paint
public void paintClicked(View view){
//use chosen color
//set erase false
drawView.setErase(false);
drawView.setBrushSize(drawView.getLastBrushSize());
if(view!=currPaint){
ImageButton imgView = (ImageButton)view;
String color = view.getTag().toString();
drawView.setColor(color);
//update ui
imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
currPaint=(ImageButton)view;
}
}
#Override
public void onClick(View view){
if(view.getId()==R.id.draw_btn){
//draw button clicked
final Dialog brushDialog = new Dialog(this);
brushDialog.setTitle("Brush size:");
brushDialog.setContentView(R.layout.brush_seekbar);
brushDialog.setCancelable(true);
SeekBar brushSeekbar = (SeekBar) brushDialog.findViewById(R.id.brushSize_seekbar);
int brushSize = brushSeekbar.getProgress();
brushSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser)
drawView.setErase(false); //turn off eraser
drawView.setBrushSize(progress); //Change StrokeWidth
}
});
Button closeBrushBtn = (Button) brushDialog.findViewById(R.id.closeBrushBtn);
// if decline button is clicked, close the custom dialog
closeBrushBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
brushDialog.dismiss();
}
});
brushDialog.show();
//pass brush size to DrawingView
}
else if(view.getId()==R.id.erase_btn){
//switch to erase - choose size
final Dialog brushDialog = new Dialog(this);
brushDialog.setTitle("Erase size:");
brushDialog.setContentView(R.layout.brush_seekbar);
brushDialog.setCancelable(true);
SeekBar brushSeekbar = (SeekBar) brushDialog.findViewById(R.id.brushSize_seekbar);
brushSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser)
drawView.setErase(true);
drawView.setBrushSize(progress); //Change StrokeWidth
}
});
Button closeBrushBtn = (Button) brushDialog.findViewById(R.id.closeBrushBtn);
// if decline button is clicked, close the custom dialog
closeBrushBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
brushDialog.dismiss();
}
});
brushDialog.show();
}
else if(view.getId()==R.id.new_btn){
//new button
AlertDialog.Builder newDialog = new AlertDialog.Builder(this);
newDialog.setTitle("New drawing");
newDialog.setMessage("Start new drawing (you will lose the current drawing)?");
newDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
drawView.startNew();
dialog.dismiss();
}
});
newDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
newDialog.show();
}
else if(view.getId()==R.id.save_btn){
//save drawing
AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
saveDialog.setTitle("Save drawing");
saveDialog.setMessage("Save drawing to device Gallery?");
saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
//save drawing
drawView.setDrawingCacheEnabled(true);
//attempt to save
String imgSaved = MediaStore.Images.Media.insertImage(
getContentResolver(), drawView.getDrawingCache(),
UUID.randomUUID().toString()+".png", "drawing");
//feedback
if(imgSaved!=null){
Toast savedToast = Toast.makeText(getApplicationContext(),
"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
savedToast.show();
}
else{
Toast unsavedToast = Toast.makeText(getApplicationContext(),
"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}
drawView.destroyDrawingCache();
}
});
saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
saveDialog.show();
}
}
}
LogCat:
10-26 20:55:10.716: E/AndroidRuntime(883): FATAL EXCEPTION: main
10-26 20:55:10.716: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drawingfun/com.example.drawingfun.MainActivity}: java.lang.ClassCastException: android.widget.TableRow cannot be cast to android.widget.ImageButton
10-26 20:55:10.716: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-26 20:55:10.716: E/AndroidRuntime(883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-26 20:55:10.716: E/AndroidRuntime(883): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-26 20:55:10.716: E/AndroidRuntime(883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-26 20:55:10.716: E/AndroidRuntime(883): at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 20:55:10.716: E/AndroidRuntime(883): at android.os.Looper.loop(Looper.java:137)
10-26 20:55:10.716: E/AndroidRuntime(883): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 20:55:10.716: E/AndroidRuntime(883): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 20:55:10.716: E/AndroidRuntime(883): at java.lang.reflect.Method.invoke(Method.java:525)
10-26 20:55:10.716: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 20:55:10.716: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 20:55:10.716: E/AndroidRuntime(883): at dalvik.system.NativeStart.main(Native Method)
10-26 20:55:10.716: E/AndroidRuntime(883): Caused by: java.lang.ClassCastException: android.widget.TableRow cannot be cast to android.widget.ImageButton
10-26 20:55:10.716: E/AndroidRuntime(883): at com.example.drawingfun.MainActivity.onCreate(MainActivity.java:53)
10-26 20:55:10.716: E/AndroidRuntime(883): at android.app.Activity.performCreate(Activity.java:5133)
10-26 20:55:10.716: E/AndroidRuntime(883): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-26 20:55:10.716: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-26 20:55:10.716: E/AndroidRuntime(883): ... 11 more
Thanks for any help, I'm new to Android Development and pretty lost here.

Your bottom linear layout is inside the tablelayout. Or you are referring to imagebutton botDrag?

Related

Call another activity from button on camera Android

I'm implementing a Camera app on Android, I'd like to have a button on it which will lead the user to the developer website, or my website.
This button is placed right of the shutter button.
I was trying to call the webview activity from this button, but it's giving me errors and I'm confused on this case, because there are plenty of examples of calling an activity inside another one, but no from a camera app.
I don't know what am I doing wrong.
Here's the piece of code in MainActivity:
public class MainActivity extends Activity /**implements OnClickListener**/ {
ImageView image;
Activity context;
Preview preview;
Camera camera;
Button exitButton;
ImageView fotoButton;
Button webButton;
LinearLayout progressLayout;
String path = "/sdcard/KutCamera/cache/images/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
webButton = (Button) findViewById(R.id.imageView_world);
fotoButton = (ImageView) findViewById(R.id.imageView_foto);
exitButton = (Button) findViewById(R.id.button_exit);
image = (ImageView) findViewById(R.id.imageView_photo);
progressLayout = (LinearLayout) findViewById(R.id.progress_layout);
preview = new Preview(this,
(SurfaceView) findViewById(R.id.KutCameraFragment));
FrameLayout frame = (FrameLayout) findViewById(R.id.preview);
frame.addView(preview);
preview.setKeepScreenOn(true);
fotoButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
takeFocusedPicture();
} catch (Exception e) {
}
exitButton.setClickable(false);
fotoButton.setClickable(false);
webButton.setClickable(true);
progressLayout.setVisibility(View.VISIBLE);
}
});
webButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this, WebActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
}
Everytime I run it it throws me this error:
4-04 00:33:43.929 4237-4237/com.kut.kutcamera E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kut.kutcamera/com.kut.kutcamera.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
at com.kut.kutcamera.MainActivity.onCreate(MainActivity.java:56)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
The layout declaration of the world button is just Okay, I don't really think it's because of that, I guess there is something inside that camera method that doesn't allows me to properly make the call.
Can anybody shed some light on this?
Thanks in advance!
EDIT
activiy_main.xml:
<FrameLayout 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"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<SurfaceView
android:id="#+id/KutCameraFragment"
android:name="com.kut.camera.KutCameraFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<RelativeLayout
android:id="#+id/rel_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="1"
android:background="#android:color/black"
android:orientation="vertical"
android:padding="10dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textViewReferan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Photo"
android:textColor="#android:color/white"
android:textSize="16sp" />
<Button
android:id="#+id/button_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:text="Ok"
android:textColor="#2799CF" />
</RelativeLayout>
<LinearLayout
android:id="#+id/progress_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/islem_value_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..." />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:alpha="0.9"
android:background="#android:color/black"
android:padding="10dp" >
<ImageView
android:id="#+id/imageView_foto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/camera"
android:contentDescription="1" />
<ImageView
android:id="#+id/imageView_photo"
android:layout_width="80dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/fotocekicon"
android:contentDescription="2" />
<ImageView
android:id="#+id/imageView_world"
android:layout_width="80dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/world"
android:contentDescription="2" />
</RelativeLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mark3"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</FrameLayout>
</FrameLayout>
Hi I have update your activity class, just replace with my code let me know you still face the problem.
public class MainActivity extends Activity /**implements OnClickListener**/ {
ImageView image;
Activity context;
Preview preview;
Camera camera;
Button exitButton;
ImageView fotoButton;
ImageView webButton;
LinearLayout progressLayout;
String path = "/sdcard/KutCamera/cache/images/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
webButton = (ImageView) findViewById(R.id.imageView_world);
fotoButton = (ImageView) findViewById(R.id.imageView_foto);
exitButton = (Button) findViewById(R.id.button_exit);
image = (ImageView) findViewById(R.id.imageView_photo);
progressLayout = (LinearLayout) findViewById(R.id.progress_layout);
preview = new Preview(this,
(SurfaceView) findViewById(R.id.KutCameraFragment));
FrameLayout frame = (FrameLayout) findViewById(R.id.preview);
frame.addView(preview);
preview.setKeepScreenOn(true);
fotoButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
takeFocusedPicture();
} catch (Exception e) {
}
exitButton.setClickable(false);
fotoButton.setClickable(false);
webButton.setClickable(true);
progressLayout.setVisibility(View.VISIBLE);
}
});
webButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this, WebActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
}
Thank you

Android application crashes every time on startup

I started coding my first app in Android Studio, what it should do at this stage is that you click a button with the digit and it outputs it into the textfield. When i run this code the application crashes right on startup and I have no idea what's wrong.
MyActivity.java
package com.example.david.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.w3c.dom.Text;
public class MyActivity extends Activity {
EditText results;
private int number;
final EditText result = (EditText) findViewById(R.id.number);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
private void BtnPressed(int i) {
result.setText(Integer.toString(i));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, 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);
}
}
activity_my.xml
<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=".MyActivity">
<EditText
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/number"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="0"
android:background="#ffe7e7e7"
android:textSize="30sp"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:editable="true"
android:enabled="true"
android:numeric="integer|signed|decimal" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="1"
android:id="#+id/button"
android:layout_below="#+id/number"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_marginTop="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="2"
android:id="#+id/button2"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="3"
android:id="#+id/button3"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="4"
android:id="#+id/button4"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="5"
android:id="#+id/button5"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="6"
android:id="#+id/button6"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button5"
android:layout_toRightOf="#+id/button5"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="7"
android:id="#+id/button7"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="8"
android:id="#+id/button8"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button7"
android:layout_toRightOf="#+id/button7"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="9"
android:id="#+id/button9"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button8"
android:layout_toRightOf="#+id/button8"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="0"
android:id="#+id/button11"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_below="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="160dp"
android:layout_height="75dp"
android:text="="
android:id="#+id/button12"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button11"
android:layout_toRightOf="#+id/button11"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="+"
android:id="#+id/button13"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button3"
android:layout_toRightOf="#+id/button3"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="-"
android:id="#+id/button14"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button6"
android:layout_toRightOf="#+id/button6"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="*"
android:id="#+id/button15"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button9"
android:layout_toRightOf="#+id/button9"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="/"
android:id="#+id/button16"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button12"
android:layout_toRightOf="#+id/button12"
android:layout_marginLeft="10dp" />
Logcat:
07-24 16:30:07.894 26564-26564/com.example.david.calculator D/AndroidRuntime﹕ Shutting down VM
07-24 16:30:07.894 26564-26564/com.example.david.calculator W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4190aba8)
07-24 16:30:07.899 26564-26564/com.example.david.calculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.david.calculator, PID: 26564
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.david.calculator/com.example.david.calculator.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at com.example.david.calculator.MyActivity.<init>(MyActivity.java:18)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Thank you very much for all the replies.
            
You are setting your EditText in your main class and not in your classes onCreate method. You can't find a view if the activity hasn't been created yet.
public class MyActivity extends Activity {
EditText results;
private int number;
final EditText result = (EditText) findViewById(R.id.number);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
Should become
public class MyActivity extends Activity {
EditText results;
private int number;
private EditText result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
result = (EditText) findViewById(R.id.number);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
You were getting a NullPointerException in your log, this should fix it.
You should move
final EditText result = (EditText) findViewById(R.id.number);
inside onCreate(...) after setContentView(..)
Corrected Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
EditText result = (EditText) findViewById(R.id.number);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
Check your code:
final EditText result = (EditText) findViewById(R.id.number);
This should come after setContentView() method just like with the button.
You should do this:
final EditText result = (EditText) findViewById(R.id.number);
in your onCreate after the setContentView.

my program force closes . FATAL EXCEPTION :main please see LogCat and code below

the logcat shows something like button does not exist,but i couldn't find any mistake there.
The app force closes when i click the flashlight button in the main activity page.
I am new to android programming. please help.
LogCat:
02-04 21:36:29.182: E/AndroidRuntime(798): FATAL EXCEPTION: main
02-04 21:36:29.182: E/AndroidRuntime(798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iamtheonewhoknocks.toolkit/com.iamtheonewhoknocks.toolkit.FlashlightActivity}: java.lang.IllegalArgumentException: Button does not exist
02-04 21:36:29.182: E/AndroidRuntime(798): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-04 21:36:29.182: E/AndroidRuntime(798): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-04 21:36:29.182: E/AndroidRuntime(798): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-04 21:36:29.182: E/AndroidRuntime(798): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-04 21:36:29.182: E/AndroidRuntime(798): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 21:36:29.182: E/AndroidRuntime(798): at android.os.Looper.loop(Looper.java:123)
02-04 21:36:29.182: E/AndroidRuntime(798): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-04 21:36:29.182: E/AndroidRuntime(798): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 21:36:29.182: E/AndroidRuntime(798): at java.lang.reflect.Method.invoke(Method.java:507)
02-04 21:36:29.182: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-04 21:36:29.182: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-04 21:36:29.182: E/AndroidRuntime(798): at dalvik.system.NativeStart.main(Native Method)
02-04 21:36:29.182: E/AndroidRuntime(798): Caused by: java.lang.IllegalArgumentException: Button does not exist
02-04 21:36:29.182: E/AndroidRuntime(798): at com.android.internal.app.AlertController.setButton(AlertController.java:288)
02-04 21:36:29.182: E/AndroidRuntime(798): at android.app.AlertDialog.setButton(AlertDialog.java:161)
02-04 21:36:29.182: E/AndroidRuntime(798): at com.iamtheonewhoknocks.toolkit.FlashlightActivity.onCreate(FlashlightActivity.java:42)
02-04 21:36:29.182: E/AndroidRuntime(798): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-04 21:36:29.182: E/AndroidRuntime(798): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-04 21:36:29.182: E/AndroidRuntime(798): ... 11 more
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#151515"
android:orientation="horizontal"
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:focusable="true"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/flashButton"
android:layout_width="60dip"
android:layout_height="60dip"
android:background="#drawable/flashlight"
android:contentDescription="#string/flashlighticon"
android:drawableTop="#drawable/flashlight" />
<ImageButton
android:id="#+id/convertorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/flashButton"
android:layout_centerHorizontal="true"
android:background="#drawable/converter"
android:contentDescription="#string/convertericon" />
<ImageButton
android:id="#+id/mglassButton"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_alignLeft="#+id/compassButton"
android:layout_alignTop="#+id/mirrorButton"
android:background="#drawable/mglass"
android:contentDescription="#string/mglassicon" />
<ImageButton
android:id="#+id/bubbleButton"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_alignLeft="#+id/convertorButton"
android:layout_below="#+id/flashButton"
android:layout_marginTop="87dp"
android:background="#drawable/bubble"
android:contentDescription="#string/bubbleicon" />
<ImageButton
android:id="#+id/compassButton"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_above="#+id/mirrorButton"
android:layout_alignLeft="#+id/flashButton"
android:background="#drawable/compass"
android:contentDescription="#string/compassicon" />
<ImageButton
android:id="#+id/mirrorButton"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_alignLeft="#+id/bubbleButton"
android:layout_below="#+id/bubbleButton"
android:layout_marginTop="83dp"
android:background="#drawable/mirror"
android:contentDescription="#string/mirroricon" />
<ImageButton
android:id="#+id/timerButton"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_above="#+id/bubbleButton"
android:layout_alignLeft="#+id/calcButton"
android:background="#drawable/timer"
android:contentDescription="#string/timericon" />
<ImageButton
android:id="#+id/calcButton"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_above="#+id/mirrorButton"
android:layout_alignParentRight="true"
android:background="#drawable/calculator"
android:contentDescription="#string/calcicon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/flashButton"
android:layout_below="#+id/flashButton"
android:layout_marginTop="19dp"
android:gravity="center"
android:text="#string/flashlighticon"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/compassButton"
android:layout_below="#+id/compassButton"
android:layout_marginTop="17dp"
android:gravity="center"
android:text="#string/compassicon"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView5"
android:layout_alignBottom="#+id/textView5"
android:layout_alignRight="#+id/calcButton"
android:gravity="center"
android:text="#string/calcicon"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/mglassButton"
android:layout_below="#+id/mglassButton"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="#string/mglassicon"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView7"
android:layout_alignBottom="#+id/textView7"
android:layout_alignLeft="#+id/mirrorButton"
android:layout_alignRight="#+id/textView5"
android:gravity="center"
android:text="#string/mirroricon"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/timerButton"
android:layout_alignRight="#+id/timerButton"
android:gravity="center"
android:text="#string/timericon"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/convertericon"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignRight="#+id/textView2"
android:gravity="center"
android:text="#string/bubbleicon"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
MainActivity.java
package com.iamtheonewhoknocks.toolkit;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flashlightButton();
calculatorButton();
bubbleButton();
}
private void flashlightButton()
{ ImageButton flButton= (ImageButton) findViewById(R.id.flashButton);
flButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,FlashlightActivity.class));
}
});
}
private void calculatorButton()
{ ImageButton flButton= (ImageButton) findViewById(R.id.calcButton);
flButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,CalculatorActivity.class));
}
});
}
private void bubbleButton()
{ ImageButton flButton= (ImageButton) findViewById(R.id.bubbleButton);
flButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,BubbleActivity.class));
}
});
}
/*#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;
} */
}
activity_flashlight.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
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=".FlashlightActivity" >
<ImageButton
android:id="#+id/btnSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/flashlight_off"
android:contentDescription="#null" />
</RelativeLayout>
FlashlightActivity.java
package com.iamtheonewhoknocks.toolkit;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
//import android.view.View.OnClickListener;
public class FlashlightActivity extends Activity {
ImageButton switchButton;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashlight);
switchButton = (ImageButton) findViewById(R.id.btnSwitch);
// First check if device is supporting flashlight or not
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(FlashlightActivity.this)
.create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton(0,"OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});
alert.show();
return;
}
// get the camera
getCamera();
// displaying button image
toggleButtonImage();
// Switch button click event to toggle flash on/off
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
}
// Get the camera
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
// Turning On flash
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
// playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
}
// Turning Off flash
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
// playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
// changing button/switch image
toggleButtonImage();
}
}
/*
* Toggle switch button images
* changing image states to on / off
* */
private void toggleButtonImage(){
if(isFlashOn){
switchButton.setImageResource(R.drawable.flashlight_on);
}else{
switchButton.setImageResource(R.drawable.flashlight_off);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
// on pause turn off the flash
turnOffFlash();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onResume() {
super.onResume();
// on resume turn on the flash
if(hasFlash)
turnOnFlash();
}
#Override
protected void onStart() {
super.onStart();
// on starting the app get the camera params
getCamera();
}
#Override
protected void onStop() {
super.onStop();
// on stop release the camera
if (camera != null) {
camera.release();
camera = null;
}
}
/*
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.flashlight, menu);
return true;
} */
}
manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iamtheonewhoknocks.toolkit"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.iamtheonewhoknocks.toolkit.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.iamtheonewhoknocks.toolkit.FlashlightActivity"
android:label="#string/title_activity_flashlight" >
</activity>
<activity
android:name="com.iamtheonewhoknocks.toolkit.CalculatorActivity"
android:label="#string/title_activity_calculator" >
</activity>
<activity
android:name="com.iamtheonewhoknocks.toolkit.TimerActivity"
android:label="#string/title_activity_timer" >
</activity>
<activity
android:name="com.iamtheonewhoknocks.toolkit.BubbleActivity"
android:label="#string/title_activity_bubble" >
</activity>
</application>
</manifest>
You should change this section
alert.setButton(0,"OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});
to
alert.setButton(DialogInterface.BUTTON_POSITIVE,"OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});
The error appears because the value 0 is unknown to the method you're using.
Take a look at what the doc says,
This method was deprecated in API level 3.
Use setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) with BUTTON_POSITIVE
so instead of passing the int (0) directly, you should use DialogInterface.BUTTON_POSITIVE. Also, you can directly use
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});

Android Application crashes on Startup

I'm new to Android development and working on a drawing app. The app was working until yesterday when I started changing the interface in the activity_main.xml
If anyone could help me figure out where the problem is, or show me how to find these problems in the future it would be much appreciated!
CatLog:
0-25 02:51:33.967: E/AndroidRuntime(853): FATAL EXCEPTION: Thread-84
10-25 02:51:33.967: E/AndroidRuntime(853): android.database.sqlite.SQLiteException: unknown error (code 14): Could not open database
10-25 02:51:33.967: E/AndroidRuntime(853): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
10-25 02:51:33.967: E/AndroidRuntime(853): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
10-25 02:51:33.967: E/AndroidRuntime(853): at android.content.ContentProviderProxy.query(ContentProviderNative.java:385)
10-25 02:51:33.967: E/AndroidRuntime(853): at android.content.ContentResolver.query(ContentResolver.java:414)
10-25 02:51:33.967: E/AndroidRuntime(853): at android.content.ContentResolver.query(ContentResolver.java:357)
10-25 02:51:33.967: E/AndroidRuntime(853): at com.android.inputmethod.latin.UserBinaryDictionary.loadDictionaryAsync(UserBinaryDictionary.java:189)
10-25 02:51:33.967: E/AndroidRuntime(853): at com.android.inputmethod.latin.ExpandableBinaryDictionary.generateBinaryDictionary(ExpandableBinaryDictionary.java:313)
10-25 02:51:33.967: E/AndroidRuntime(853): at com.android.inputmethod.latin.ExpandableBinaryDictionary.syncReloadDictionaryInternal(ExpandableBinaryDictionary.java:403)
10-25 02:51:33.967: E/AndroidRuntime(853): at com.android.inputmethod.latin.ExpandableBinaryDictionary.access$500(ExpandableBinaryDictionary.java:47)
10-25 02:51:33.967: E/AndroidRuntime(853): at com.android.inputmethod.latin.ExpandableBinaryDictionary$AsyncReloadDictionaryTask.run(ExpandableBinaryDictionary.java:435)
MainActivity.java:
package com.example.strip;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import java.util.UUID;
import android.provider.MediaStore;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
//custom drawing view
private DrawingView drawView;
//paint color
private ImageButton currPaint, drawBtn, eraseBtn, newBtn, saveBtn;
//brush size
private float smallBrush, mediumBrush, largeBrush;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get drawing view
drawView = (DrawingView)findViewById(R.id.drawing);
//get palette and first color button
LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
currPaint = (ImageButton)paintLayout.getChildAt(0);
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
//get brush sizes
smallBrush = getResources().getInteger(R.integer.small_size);
mediumBrush = getResources().getInteger(R.integer.medium_size);
largeBrush = getResources().getInteger(R.integer.large_size);
//draw button
drawBtn = (ImageButton)findViewById(R.id.draw_btn);
drawBtn.setOnClickListener(this);
//set initial brush size
drawView.setBrushSize(mediumBrush);
//set erase button, listen for click
eraseBtn = (ImageButton)findViewById(R.id.erase_btn);
eraseBtn.setOnClickListener(this);
//set NEW button, listen for click
newBtn = (ImageButton)findViewById(R.id.new_btn);
newBtn.setOnClickListener(this);
//set SAVEIMAGE button and listen for click
saveBtn = (ImageButton)findViewById(R.id.save_btn);
saveBtn.setOnClickListener(this);
}//end of onCreate
#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;
}
//user clicked paint
public void paintClicked(View view){
//use chosen color, set erase to false, and use last brush size
if(view!=currPaint){
drawView.setErase(false);
drawView.setBrushSize(drawView.getLastBrushSize());
ImageButton imgView = (ImageButton)view;
String color = view.getTag().toString();
drawView.setColor(color);
//update ui
imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
currPaint=(ImageButton)view;
}
}
#Override
public void onClick(View view)
{
if(view.getId()==R.id.draw_btn){
//draw button clicked
final Dialog brushDialog = new Dialog(this);
brushDialog.setTitle("Brush Size:");
brushDialog.setContentView(R.layout.brush_chooser);
//listen for clicks on size buttons
ImageButton smallBtn = (ImageButton)brushDialog.findViewById(R.id.small_brush);
smallBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
drawView.setErase(false);
drawView.setBrushSize(smallBrush);
drawView.setLastBrushSize(smallBrush);
brushDialog.dismiss();
}
});
ImageButton mediumBtn = (ImageButton)brushDialog.findViewById(R.id.medium_brush);
mediumBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
drawView.setErase(false);
drawView.setBrushSize(mediumBrush);
drawView.setLastBrushSize(mediumBrush);
brushDialog.dismiss();
}
});
ImageButton largeBtn = (ImageButton)brushDialog.findViewById(R.id.large_brush);
largeBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
drawView.setErase(false);
drawView.setBrushSize(largeBrush);
drawView.setLastBrushSize(largeBrush);
brushDialog.dismiss();
}
});
brushDialog.show();
}
else if(view.getId()==R.id.erase_btn){
//switch to erase, choose size
final Dialog brushDialog = new Dialog(this);
brushDialog.setTitle("Eraser Size:");
brushDialog.setContentView(R.layout.brush_chooser);
//listen for clicks on size buttons
ImageButton smallBtn = (ImageButton)brushDialog.findViewById(R.id.small_brush);
smallBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
drawView.setErase(true);
drawView.setBrushSize(smallBrush);
brushDialog.dismiss();
}
});
ImageButton mediumBtn = (ImageButton)brushDialog.findViewById(R.id.medium_brush);
mediumBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
drawView.setErase(true);
drawView.setBrushSize(mediumBrush);
brushDialog.dismiss();
}
});
ImageButton largeBtn = (ImageButton)brushDialog.findViewById(R.id.large_brush);
largeBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
drawView.setErase(true);
drawView.setBrushSize(largeBrush);
brushDialog.dismiss();
}
});
brushDialog.show();
}
else if(view.getId()==R.id.new_btn){
//NEW button
//verify user wants new drawing
AlertDialog.Builder newDialog = new AlertDialog.Builder(this);
newDialog.setTitle("New drawing");
newDialog.setMessage("Start new drawing? (You will lose your current drawing)");
newDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
drawView.startNew();
dialog.dismiss();
}
});
newDialog.setNegativeButton("Canvel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
newDialog.show();
}
else if(view.getId()==R.id.save_btn){
//save drawing
AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
saveDialog.setTitle("Save drawing");
saveDialog.setMessage("Save drawing to device Gallery?");
saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
//save drawing
drawView.setDrawingCacheEnabled(true);
String imgSaved = MediaStore.Images.Media.insertImage(
getContentResolver(), drawView.getDrawingCache(),
UUID.randomUUID().toString()+".png", "drawing");
if(imgSaved!=null){
Toast savedToast = Toast.makeText(getApplicationContext(),
"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
savedToast.show();
}
else{
Toast unsavedToast = Toast.makeText(getApplicationContext(),
"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}
drawView.destroyDrawingCache();
}
});
saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
saveDialog.show();
}
}
}
DrawingView.java:
package com.example.strip;
import android.view.View;
import android.content.Context;
import android.util.AttributeSet;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.TypedValue;
import android.util.Log;
public class DrawingView extends View
{
//drawing path
private Path drawPath;
//drawing and canvas paint
private Paint drawPaint, canvasPaint;
//initial color
private int paintColor = 0xFF660000;
//canvas
private Canvas drawCanvas;
//canvas bitmap
private Bitmap canvasBitmap;
//erase flag
private boolean erase=false;
public void startNew(){
drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
invalidate();
}
public void setErase(boolean isErase){
//set erase true or false
erase=isErase;
if(erase) drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
else drawPaint.setXfermode(null);
}
public DrawingView(Context context, AttributeSet attrs)
{
super(context, attrs);
setupDrawing();
}
private void setupDrawing(){
//get drawing area setup for interaction
drawPath = new Path();
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(brushSize);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint(Paint.DITHER_FLAG);
brushSize = getResources().getInteger(R.integer.medium_size);
lastBrushSize = brushSize;
}
public void setBrushSize(float newSize){
//update size
float pixelAmount = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
newSize, getResources().getDisplayMetrics());
brushSize=pixelAmount;
drawPaint.setStrokeWidth(brushSize);
}
public void setLastBrushSize(float lastSize){
lastBrushSize=lastSize;
}
public float getLastBrushSize(){
return lastBrushSize;
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
//view given size
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
#Override
protected void onDraw(Canvas canvas){
//draw view
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
#Override
public boolean onTouchEvent(MotionEvent event){
//detect user touch
float touchX = event.getX();
float touchY = event.getY();
//respond to down, move and up events
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawPath.lineTo(touchX, touchY);
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
break;
default:
return false;
}
//redraw
invalidate();
return true;
}
public void setColor(String newColor){
//set color
invalidate();
paintColor = Color.parseColor(newColor);
drawPaint.setColor(paintColor);
}
private float brushSize, lastBrushSize;
int squareDim = 1000000000;
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int h = this.getMeasuredHeight();
int w = this.getMeasuredWidth();
int curSquareDim = Math.min(w,h);
if(curSquareDim < squareDim)
{
squareDim = curSquareDim;
}
Log.d("MyApp", "h "+h+"w "+w+"squareDim "+squareDim);
setMeasuredDimension(squareDim, squareDim);
}
}
Activity_Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DemoActivity">
<com.sothree.slidinguppanel.SlidingUpPanelLayout
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<!-- Top Buttons -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:orientation="horizontal" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*" android:stretchColumns="*" android:background="#ffffff">
<TableRow
android:layout_height="50dp"
android:layout_width="fill_parent"
android:gravity="center_horizontal">
<TextView
android:layout_height="50dp"
android:text="Last Panel"
android:layout_weight="1" android:background="#FFD3D3D3"
android:textColor="#FFF"
android:padding="10dp" android:gravity="center"/>
<TextView
android:layout_height="50dp"
android:text="Current Panel"
android:layout_weight="1" android:background="#FFAA213A"
android:textColor="#FFF"
android:padding="10dp" android:gravity="center"/>
</TableRow>
</TableLayout>
</LinearLayout>
<!-- Custom View -->
<com.example.strip.DrawingView
android:id="#+id/drawing"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:background="#FFFFFFFF"
android:scaleType="centerInside" />
</LinearLayout>
<!-- Color Palette -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginTop="18dp"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:background = "#drawable/palettebutton"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/palettebutton"
android:tag="#FFE21A1A" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="3dp"
android:layout_marginRight="4dp"
android:layout_marginLeft="6dp">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background = "#drawable/colorbutton1"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton1"
android:tag="#FFE21A1A" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton2"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton2"
android:tag="#FFF2D820" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton3"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton3"
android:tag="#FF35EF22" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton4"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton4"
android:tag="#FF26D3EA" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton5"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton5"
android:tag="#FFF73E9B" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton6"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton6"
android:tag="#FFFFFFFF" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="6dp">
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton7"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton7"
android:tag="#FF7A4426"/>
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton8"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton8"
android:tag="#FFF74F1C"/>
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton9"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton9"
android:tag="#FF217C14" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton10"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton10"
android:tag="#FF312EBC"/>
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton11"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton11"
android:tag="#FF5D1F93" />
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:layout_margin="4dp"
android:background="#drawable/colorbutton12"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/colorbutton12"
android:tag="#FF000000"/>
</TableRow>
</TableLayout>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:gravity="center_horizontal"
android:background = "#drawable/sendbutton"
android:contentDescription="#string/paint"
android:onClick="paintClicked"
android:src="#drawable/sendbutton"
android:tag="#FFE21A1A" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/new_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/start_new"
android:src="#drawable/new_pic" />
<ImageButton
android:id="#+id/draw_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/brush"
android:src="#drawable/brush" />
<ImageButton
android:id="#+id/erase_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/erase"
android:src="#drawable/eraser" />
<ImageButton
android:id="#+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/save"
android:src="#drawable/save" />
</LinearLayout>
</LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
</RelativeLayout>
If I can provide anything else please let me know.
Thank you for the help!
10-25 02:51:33.967: E/AndroidRuntime(853): android.database.sqlite.SQLiteException: unknown error (code 14): Could not open database
This shows error that program is not able to access Database. Check DB connection and try to debug. Problem lies there.

java.lang.NullPointerException on onClickListener

Can you see if there is anything wrong with my code?
I tried to create dialog box when the button is pressed.
I don't see something wrong here, But the logcat shows java.lang.NullPointerException in this line "agree.setOnClickListener(new OnClickListener() {"
package com.sociyo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //Lock Orientation
requestWindowFeature(Window.FEATURE_NO_TITLE); //Hide Action menu
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Load main activity
//viewPager default page
ViewPagerAdapter adapter = new ViewPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
//textView clickable
Button agree = (Button)findViewById(R.id.btnTerms);
agree.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);
tpDialog.setTitle("Terms and Policy");
tpDialog.setMessage(R.string.action_settings)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
MainActivity.this.finish();
}
});
}
});
}
#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;
}
}
And this is the complete log cat :
E/AndroidRuntime(13076): FATAL EXCEPTION: main
E/AndroidRuntime(13076): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sociyo/com.sociyo.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(13076): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
E/AndroidRuntime(13076): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
E/AndroidRuntime(13076): at android.app.ActivityThread.access$600(ActivityThread.java:137)
E/AndroidRuntime(13076): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
E/AndroidRuntime(13076): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(13076): at android.os.Looper.loop(Looper.java:213)
E/AndroidRuntime(13076): at android.app.ActivityThread.main(ActivityThread.java:4793)
E/AndroidRuntime(13076): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(13076): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(13076): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime(13076): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/AndroidRuntime(13076): at dalvik.system.NativeStart.main(Native Method)
**E/AndroidRuntime(13076): Caused by: java.lang.NullPointerException**
E/AndroidRuntime(13076): at com.sociyo.MainActivity.onCreate(MainActivity.java:34)
E/AndroidRuntime(13076): at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime(13076): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime(13076): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
E/AndroidRuntime(13076): ... 11 more
My activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
And my activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent"
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:orientation="vertical" >
<TextView
android:id="#+id/tvRegisterTitle"
style="#style/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/register_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/etNameReg"
style="#style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spMlmList"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/name_hint"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/etEmailReg"
style="#style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etNameReg"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/email_hint"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/etPasswordReg"
style="#style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/etEmailReg"
android:layout_below="#+id/etEmailReg"
android:layout_centerVertical="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/password_hint"
android:inputType="textPassword" />
<Spinner
android:id="#+id/spMlmList"
style="#style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvRegisterTitle"
android:layout_below="#+id/tvRegisterTitle"
android:layout_marginTop="15dp"
android:entries="#array/mlm_list" />
<Button
android:id="#+id/btnRegister"
style="#style/buttonBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/etPasswordReg"
android:layout_below="#+id/chkAgree"
android:layout_marginTop="15dp"
android:text="#string/register_text" />
<CheckBox
android:id="#+id/chkAgree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etPasswordReg"
android:text="#string/agree_text"
android:layout_marginTop="15dp"
style="#style/checkbox"/>
<Button
android:id="#+id/btnTerms"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/chkAgree"
android:layout_alignBottom="#+id/chkAgree"
android:layout_toRightOf="#+id/tvRegisterTitle"
android:layout_marginLeft="10dp"
android:text="#string/terms_policy"
style="#style/smallLink"/>
</RelativeLayout>
Yes the button doesnt exist at that point.
Your R.id.btnTermsR.id.btnTerms is defined in activity_register.xml, but that layout is never inflated. or not accessible at that point :)
Its the button that has the Problem, not the OnClickListener. A nullpointer within the Listener would appear the moment you click the button, not while attaching it.
// Before Replace
AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);
// After Replace
AlertDialog.Builder tpDialog = new AlertDialog.Builder(MainActivity.this);
AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);
change this line too
AlertDialog.Builder tpDialog = new AlertDialog.Builder(MainActivity.this);

Categories