Fatal signal 11 (SIGSEGV) at 0x0019c12c (code=1) - java

1.)lam.java
public class lam extends Activity {
//MediaPlayer ourSong;
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.end);
//View title = getWindow().findViewById(android.R.id.title);
// View titleBar = (View) title.getParent();
// titleBar.setBackgroundColor(Color.BLACK);
overridePendingTransition(R.anim.a1,R.anim.a2);
//ourSong=MediaPlayer.create(lam.this, R.raw.rollver);
addListenerOnButton();
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.activity_open_translate,R.anim.activity_close_translate);
}
public void addListenerOnButton() {
final Context context = this;
Button tut1 = (Button) findViewById(R.id.button1);
Button tut2= (Button) findViewById(R.id.button2);
Button tut3 = (Button) findViewById(R.id.button3);
//Button tut4 = (Button) findViewById(R.id.button4);
Button tut6 = (Button) findViewById(R.id.button4);
Button tut5 = (Button) findViewById(R.id.button5);
tut1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
//marks
Intent i = new Intent(lam.this, MarksTask.class);
startActivity(i);
}
});
tut2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
Intent intent = new Intent(lam.this, timetable.class);
startActivity(intent);
}
});
tut3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
Intent intent = new Intent(context,further.class);
startActivity(intent);
overridePendingTransition(R.anim.a1,R.anim.a2);
}
});
/*tut4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
Intent intent = new Intent(context,bus.class);
startActivity(intent);
overridePendingTransition(R.anim.a1,R.anim.a2);
}
}); */
tut6.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
Intent intent = new Intent(context,P_menu.class);
startActivity(intent);
overridePendingTransition(R.anim.a1,R.anim.a2);
}
});
tut5.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
Intent intent = new Intent(context,S_lib.class);
startActivity(intent);
overridePendingTransition(R.anim.a1,R.anim.a2);
}
});
}
}
2.)end.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FDF3E7"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="200dp"
android:layout_marginTop="10dp"
android:background="#drawable/wd" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="10dp"
android:background="#3B3738" />
<Button
android:id="#+id/button2"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/ef" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="10dp"
android:background="#3B3738" />
<Button
android:id="#+id/button3"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/qs" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="10dp"
android:background="#3B3738" />
<Button
android:id="#+id/button5"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/library" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="10dp"
android:background="#3B3738" />
<Button
android:id="#+id/button4"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/buss" />
</LinearLayout>
</ScrollView>
i am getting the following error in log
A/libc(25871): Fatal signal 11 (SIGSEGV) at 0x0019c12c (code=1), thread 25918 (1234.xyz)
A/libc(25871): Fatal signal 11 (SIGSEGV) at 0x00001160 (code=1), thread 25871 (1234.xyz)
and my google play store error console shows following error-
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xyz.xyz/com.xyz.xyz.lam}: android.view.InflateException: Binary XML file line #49: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2239)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5047)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #49: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:623)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1947)
at com.xyz.xyz.lam.onCreate(lam.java:21)
at android.app.Activity.performCreate(Activity.java:5249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:597)
... 26 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:597)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:432)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.createFromResourceStream(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2136)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:319)
at android.content.res.Resources.getDrawable(Resources.java:710)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2116)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:319)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3558)
at android.widget.TextView.<init>(TextView.java:623)
at android.widget.Button.<init>(Button.java:107)
at android.widget.Button.<init>(Button.java:103)

Related

How to use onClick for multiple ImageViews for starting other activities

For the JAVA file:
public class UserDataInputActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data_input);
}
public void onGenericMenuClick(View view)
{
ImageView pastTests = (ImageView) findViewById(R.id.pasttests);
final Intent intent1 = new Intent(this, PastDiagnosticResult.class);
ImageView userData = (ImageView) findViewById(R.id.myinfo);
final Intent intent2 = new Intent(this, UserDataInputActivity.class);
ImageView currentTests = (ImageView) (findViewById(R.id.currenttest));
final Intent intent3 = new Intent(this, CurrentDiagnosticResultActivity.class);
pastTests.setOnClickListener(new OnClickListener(){
public void onClick(View view)
{
startActivity(intent1);
}});
userData.setOnClickListener(new OnClickListener(){
public void onClick(View view)
{
startActivity(intent2);
}});
currentTests.setOnClickListener(new OnClickListener(){
public void onClick(View view)
{
startActivity(intent3);
}});
}
}
I essentially have 3 icons (imageViews) on the bottom of my screen. My objective: to navigate to different screens as I click the different images on the menu. However, my java code does not work correctly. Although it runs, it crashes after I try to use Intents 2 and 3 consecutively. Is there a better way to do this?
Corresponding LOGCAT Error Output:
2018-11-11 02:22:02.224 7638-7638/com.example.owner.introductoryapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.owner.introductoryapplication, PID: 7638
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6312)
at android.view.View$PerformClick.run(View.java:24802)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6521)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6312) 
at android.view.View$PerformClick.run(View.java:24802) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.owner.introductoryapplication.CurrentDiagnosticResultActivity.onGenericMenuClick(CurrentDiagnosticResultActivity.java:28)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6312) 
at android.view.View$PerformClick.run(View.java:24802) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Edit #2 - XML File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2E2A27"
tools:context=".UserDataInputActivity">
<ImageView
android:id="#+id/header"
android:src="#drawable/companyicon"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#211F1E"
tools:layout_editor_absoluteY="4dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:text="#string/PersonalInfo"
android:textSize="30sp"
style="#style/TextHeaderAttr"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:background="#211F1E">
<ImageView
android:src="#drawable/diagnosisicon"
android:id="#+id/pasttests"
style="#style/IconStyle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
<ImageView
android:src="#drawable/profileicon"
android:id="#+id/myinfo"
style="#style/IconStyle"
android:tint="#A9A9A9"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
<ImageView
android:src="#drawable/resultsicon"
android:id="#+id/currenttest"
style="#style/IconStyle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
</LinearLayout>
<!-- CONTENT -->
</RelativeLayout>
 
You are not using onGenericMenuClick() as you should.
This is the common onClick() for all 3 views so you don't define inside it new listeners.
Change to this:
public void onGenericMenuClick(View view) {
Intent intent;
if(v.getId() == R.id.pasttests) {
intent = new Intent(this, PastDiagnosticResult.class);
} else if (v.getId() == R.id.myinfo) {
intent = new Intent(this, UserDataInputActivity.class);
} else if (v.getId() == R.id.currenttest) {
intent = new Intent(this, CurrentDiagnosticResultActivity.class);
}
startActivity(intent);
}
This is a case, where you should use Fragments instead Activities.
1.Replace three Activities with fragments.
2.Use FragmentManager to switch between fragments.
UserDataInputFragment userDataInputFragment = UserDataInputFragment.newInstance("","");
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment, userDataInputFragment, "userDataInputFragment")
.commit();
//here fragment in R.id.fragment is the container of Fragments

Unfortunately, Simple app is stopped [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 5 years ago.
I'm new to android development and im making a simple calculator app... Whenever i'm running the code in the android emulator it's showing me Unfortunately, SimpleApp has Stopped.... I'm Using Android Studio to write the code Please help me...
Following is my code....
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.simpleapp.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="337dp"
android:layout_height="58dp"
android:contextClickable="false"
android:cursorVisible="false"
android:fontFamily="serif"
android:longClickable="true"
android:text="Calculator"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#android:color/holo_red_dark"
android:textSize="36sp"
android:visibility="visible"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="31dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="194dp"
android:layout_height="43dp"
android:text="Number 1: "
android:textColor="#android:color/black"
android:textSize="25sp"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="140dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="194dp"
android:layout_height="44dp"
android:text="Number 2: "
android:textColor="#android:color/black"
android:textSize="25sp"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="217dp" />
<EditText
android:id="#+id/number2"
android:layout_width="99dp"
android:layout_height="43dp"
android:ems="10"
android:hint="Number 2"
android:inputType="number|numberSigned|numberDecimal"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textIsSelectable="true"
android:textSize="20sp"
tools:layout_editor_absoluteX="262dp"
tools:layout_editor_absoluteY="217dp" />
<EditText
android:id="#+id/number1"
android:layout_width="99dp"
android:layout_height="43dp"
android:ems="10"
android:hint="Number 1"
android:inputType="number|numberSigned|numberDecimal"
android:linksClickable="false"
android:longClickable="true"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="20sp"
tools:layout_editor_absoluteX="262dp"
tools:layout_editor_absoluteY="140dp" />
<TextView
android:id="#+id/result"
android:layout_width="175dp"
android:layout_height="40dp"
tools:layout_editor_absoluteX="176dp"
tools:layout_editor_absoluteY="430dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="103dp"
android:layout_height="40dp"
android:text="Result: "
android:textColor="#android:color/black"
android:textSize="25sp"
tools:layout_editor_absoluteX="59dp"
tools:layout_editor_absoluteY="433dp" />
<Button
android:id="#+id/btnAdd"
android:layout_width="152dp"
android:layout_height="49dp"
android:text="+"
android:textColor="#android:color/black"
android:textSize="30sp"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="292dp" />
<Button
android:id="#+id/btnSub"
android:layout_width="152dp"
android:layout_height="49dp"
android:text="-"
android:textColor="#android:color/black"
android:textIsSelectable="false"
android:textSize="30sp"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="292dp" />
<Button
android:id="#+id/btnDiv"
android:layout_width="152dp"
android:layout_height="49dp"
android:text="/"
android:textColor="#android:color/black"
android:textSize="25sp"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="362dp" />
<Button
android:id="#+id/btnMul"
android:layout_width="152dp"
android:layout_height="49dp"
android:text="*"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="25sp"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="362dp" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.myapp.simpleapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText number1= (EditText) findViewById(R.id.number1);
EditText number2= (EditText) findViewById(R.id.number2);
Button add= (Button) findViewById(R.id.btnAdd);
Button sub= (Button) findViewById(R.id.btnSub);
Button div= (Button) findViewById(R.id.btnDiv);
Button mul= (Button) findViewById(R.id.btnMul);
TextView txtresult= (TextView) findViewById(R.id.result);
double num1,num2,result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1=Double.parseDouble(number1.getText().toString());
num2=Double.parseDouble(number2.getText().toString());
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result=num1+num2;
txtresult.setText(Double.toString(result));
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result=num1-num2;
txtresult.setText(Double.toString(result));
}
});
div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result=num1/num2;
txtresult.setText(Double.toString(result));
}
});
mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result=num1*num2;
txtresult.setText(Double.toString(result));
}
});
}
}
LogCat
04-03 12:25:38.913 3927-3927/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:38.913 3927-3927/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:39.372 3952-3952/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:39.373 3952-3952/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:49.078 4097-4097/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:49.078 4097-4097/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:49.647 4122-4122/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:49.648 4122-4122/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:58.059 3666-3666/com.myapp.simpleapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.simpleapp, PID: 3666
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.myapp.simpleapp.MainActivity$4.onClick(MainActivity.java:70)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-03 12:25:58.079 1163-1163/? E/EGL_emulation: tid 1163: eglCreateSyncKHR(1299): error 0x3004 (EGL_BAD_ATTRIBUTE)
04-03 12:26:02.452 1810-2202/system_process E/InputDispatcher: channel '21dd8534 com.myapp.simpleapp/com.myapp.simpleapp.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
04-03 12:26:02.581 1175-1175/? E/audio_hw_generic: Error opening input stream format 1, channel_mask 0010, sample_rate 16000
The most common cause of this problem is a NullPointerException at runtime.
Steps to follow:
Observe the Stack trace on Android Studio as shown below:
You can then analyze the Stack trace, and zero in on what exactly caused the exception, and the line that caused it. Alternatively, you can post the screenshot of your stack trace so it can be easier to answer the question.
Use findViewById inside onCreate and use Double.parse only after values have been entered in the edittexts.
Try this code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText number1 = (EditText) findViewById(R.id.number1);
final EditText number2 = (EditText) findViewById(R.id.number2);
Button add = (Button) findViewById(R.id.btnAdd);
Button sub = (Button) findViewById(R.id.btnSub);
Button div = (Button) findViewById(R.id.btnDiv);
Button mul = (Button) findViewById(R.id.btnMul);
final TextView txtresult = (TextView) findViewById(R.id.result);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double num1, num2, result;
num1 = Double.parseDouble(number1.getText().toString());
num2 = Double.parseDouble(number2.getText().toString());
result = num1 + num2;
txtresult.setText(Double.toString(result));
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double num1, num2, result;
num1 = Double.parseDouble(number1.getText().toString());
num2 = Double.parseDouble(number2.getText().toString());
result = num1 - num2;
txtresult.setText(Double.toString(result));
}
});
div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double num1, num2, result;
num1 = Double.parseDouble(number1.getText().toString());
num2 = Double.parseDouble(number2.getText().toString());
result = num1 / num2;
txtresult.setText(Double.toString(result));
}
});
mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double num1, num2, result;
num1 = Double.parseDouble(number1.getText().toString());
num2 = Double.parseDouble(number2.getText().toString());
result = num1 * num2;
txtresult.setText(Double.toString(result));
}
});
}
}

Java Unable to start activity NullPointerException similar questions don't solve this [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
Hello I am trying to start activity from one to activity to other one but i get error
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bsine_000.baseinas/com.example.bsine_000.baseinas.registracija}: java.lang.NullPointerException
here is the code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button etprisijungti;
EditText etelpastas, etslaptazodis;
TextView reglink;
saugykla saugykla;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etprisijungti = (Button) findViewById(R.id.etprisijungti);
etelpastas = (EditText) findViewById(R.id.etelpastas);
etslaptazodis = (EditText) findViewById(R.id.etslaptazodis);
reglink = (TextView) findViewById(R.id.reglink);
reglink.setOnClickListener(this);
etprisijungti.setOnClickListener(this);
saugykla = new saugykla(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.etprisijungti:
vartotojas vartotojas = new vartotojas(null,null);
saugykla.saugoti(vartotojas);
saugykla.vartotojas_prisijunges(true);
startActivity(new Intent(this, pagrindinis.class));
break;
case R.id.reglink:
startActivity(new Intent(this, registracija.class));
break;
}
}
}
code where app crashes is
case R.id.reglink:
startActivity(new Intent(this, registracija.class));
break;
first case works fine code is exactly the same here is registracija class:
public class registracija extends AppCompatActivity implements View.OnClickListener {
Button etregistruotis;
EditText etelpastas, etslaptazodis;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registracija);
etregistruotis = (Button) findViewById(R.id.reglink)
etelpastas = (EditText) findViewById(R.id.etelpastas);
etslaptazodis = (EditText) findViewById(R.id.etslaptazodis);
etregistruotis.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.etregistruotis:
String elpastas = etelpastas.getText().toString();
String slaptazdodis = etslaptazodis.getText().toString();
break;
}
}
}
Logcat :
05-25 10:45:36.401 23721-23721/com.example.bsine_000.baseinas W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41241438)
05-25 10:45:36.411 23721-23721/com.example.bsine_000.baseinas E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bsine_000.baseinas/com.example.bsine_000.baseinas.registracija}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.bsine_000.baseinas.registracija.onCreate(registracija.java:23)
at android.app.Activity.performCreate(Activity.java:5203)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4960)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
activity main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="El.pastas"/>
<EditText
android:id="#+id/etelpastas"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="slaptazodis"/>
<EditText
android:id="#+id/etslaptazodis"
android:inputType="textPassword"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/etprisijungti"
android:text="Prisijungti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:enabled="true" />
<TextView
android:id="#+id/reglink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold"
android:padding="10dp"
android:layout_gravity="center_horizontal"
android:text="Registruotis"
android:clickable="true"
android:linksClickable="true" />
Activity registracija
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="El.pastas"/>
<EditText
android:id="#+id/etelpastas"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="slaptazodis"/>
<EditText
android:id="#+id/etslaptazodis"
android:inputType="textPassword"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/etregistruotis"
android:text="Registruotis"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Try this:
In your MainActivity replace this code:
startActivity(new Intent(this, registracija.class));
With this code:
startActivity(new Intent(MainActivity.this, registracija.class));
Hope it helps, if it didn't please post the code of activity_registracija.xml.
Edit: Reglink doesn't exist in your activity_registracija.xml.
public class registracija extends AppCompatActivity implements
View.OnClickListener {
Button etregistruotis;
EditText etelpastas, etslaptazodis;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registracija);
etregistruotis=(Button)findViewById(R.id. etregistruotis);
etelpastas = (EditText) findViewById(R.id.etelpastas);
etslaptazodis = (EditText) findViewById(R.id.etslaptazodis);
etregistruotis.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String elpastas = etelpastas.getText().toString();
String slaptazdodis = etslaptazodis.getText().toString();
break;
}
}
please intialise the button on onCreate().
as you are making click on that same button no need to giva the id again in registracija.java.ithink this will resolve your error
As said in one comment, etregistruotis variable is not initialized. You need to do it like you do to the other two variables above and then only call the setOnClickListener.
If it still crashes after that please update your answer with the code you are using.
Bonus : your activity class starts with a lower case letter. By convention it should start with a capital letter.
Update: Reglink doesn't exist in the activity xml

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 App crashes at startup - Java.lang.RuntimeException

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?

Categories