Android Dev: ClassCastException Error - java

I created a custom component class which is just the RadioGroup class changed to extend RelativeLayout instead of LinearLayout when I try to run my app I get this error:
06-16 00:01:17.573: ERROR/AndroidRuntime(567): Caused by: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 00:01:17.573: ERROR/AndroidRuntime(567): at com.stickfigs.blockball.BlockBallLevelSelect.onCreate(BlockBallLevelSelect.java:38)
Line 38 refers to this line:
lsRadioGroupRelative = (RadioGroupRelative) findViewById(R.id.radioGroupRelative1);
Where that variable is defined as:
private RadioGroupRelative lsRadioGroupRelative;
Here is the layout.xml file being used in this part of the app:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_levelselect">
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.stickfigs.blockball.RadioGroupRelative
android:id="#+id/radioGroupRelative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="64px"
android:layout_marginTop="96px"
android:orientation="horizontal">
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton1"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="1">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton2"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="2"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton1"
android:layout_alignTop="#id/levelButton1">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton3"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="3"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton2"
android:layout_alignTop="#id/levelButton2">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton4"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="4"
android:layout_marginTop="32px"
android:layout_below="#id/levelButton1"
android:layout_alignLeft="#id/levelButton1">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton5"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="5"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton4"
android:layout_alignTop="#id/levelButton4">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton6"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="6"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton5"
android:layout_alignTop="#id/levelButton5">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton7"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="7"
android:layout_marginTop="32px"
android:layout_below="#id/levelButton4"
android:layout_alignLeft="#id/levelButton4">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton8"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="8"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton7"
android:layout_alignTop="#id/levelButton7">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton9"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="9"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton8"
android:layout_alignTop="#id/levelButton8">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton10"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="10"
android:layout_marginTop="32px"
android:layout_below="#id/levelButton7"
android:layout_alignLeft="#id/levelButton7">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton11"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="11"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton10"
android:layout_alignTop="#id/levelButton10">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton12"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="12"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton11"
android:layout_alignTop="#id/levelButton11">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton13"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="13"
android:layout_marginTop="32px"
android:layout_below="#id/levelButton10"
android:layout_alignLeft="#id/levelButton10">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton14"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="14"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton13"
android:layout_alignTop="#id/levelButton13">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton15"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="15"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton14"
android:layout_alignTop="#id/levelButton14">
</view>
</com.stickfigs.blockball.RadioGroupRelative>
<Button
android:id="#+id/levelButtonGo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="128px"
android:layout_marginTop="22px"
android:layout_below="#id/radioGroupRelative1"
android:text=""
android:background="#drawable/level_select_button_play"
android:onClick="goClicked"/>
</RelativeLayout>
</FrameLayout>
I'm not sure what is causing the error since I'm not trying to cast it to something it is not, what is going on?
UPDATE: Constructors:
/**
* {#inheritDoc}
*/
public RadioGroupRelative(Context context) {
super(context);
//setOrientation(VERTICAL);
init();
}
/**
* {#inheritDoc}
*/
public RadioGroupRelative(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray attributes = context.obtainStyledAttributes(
attrs, R.styleable.RadioGroupRelative, 0,
android.R.style.Widget_CompoundButton_RadioButton);
int value = attributes.getResourceId(R.styleable.RadioGroupRelative_android_checkedButton,
View.NO_ID);
if (value != View.NO_ID) {
mCheckedId = value;
}
attributes.recycle();
init();
}
private void init() {
mChildOnCheckedChangeListener = new CheckedStateTracker();
mPassThroughListener = new PassThroughHierarchyChangeListener();
super.setOnHierarchyChangeListener(mPassThroughListener);
}
UPDATE More stacktrace:
06-16 02:18:13.814: INFO/AndroidRuntime(638): NOTE: attach of thread 'Binder Thread #3' failed
06-16 02:18:13.964: INFO/ActivityManager(58): Start proc com.stickfigs.blockball for activity com.stickfigs.blockball/.BlockBallLevelSelect: pid=645 uid=10036 gids={}
06-16 02:18:14.584: DEBUG/AndroidRuntime(645): Shutting down VM
06-16 02:18:14.584: WARN/dalvikvm(645): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): FATAL EXCEPTION: main
06-16 02:18:14.654: ERROR/AndroidRuntime(645): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stickfigs.blockball/com.stickfigs.blockball.BlockBallLevelSelect}: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.os.Looper.loop(Looper.java:123)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at java.lang.reflect.Method.invoke(Method.java:521)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at dalvik.system.NativeStart.main(Native Method)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): Caused by: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at com.stickfigs.blockball.BlockBallLevelSelect.onCreate(BlockBallLevelSelect.java:38)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): ... 11 more
06-16 02:18:14.786: WARN/ActivityManager(58): Force finishing activity com.stickfigs.blockball/.BlockBallLevelSelect
06-16 02:18:15.326: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44ed9600 com.stickfigs.blockball/.BlockBallLevelSelect}
06-16 02:18:19.254: INFO/Process(645): Sending signal. PID: 645 SIG: 9
06-16 02:18:19.294: WARN/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#44ea5ab8
06-16 02:18:19.584: INFO/ActivityManager(58): Process com.stickfigs.blockball (pid 645) has died.
06-16 02:18:20.106: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
06-16 02:18:20.783: DEBUG/dalvikvm(217): GC_EXPLICIT freed 100 objects / 4192 bytes in 125ms

Related

When I try to run my app on the emulator, it's unfortunately saying app is stopped [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I am new on android. I have just made my first app. But as I run my my app on android emulator, it does not run. App compiled without any error, but on emulator it's showing that unfortunately app is stopped.
This is the java code:
package com.example.kartikeya;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
double x,y;
static int op=0;
double result;
TextView textView = new TextView(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this); // calling onClick() method
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
//Button button3 = (Button) findViewById(R.id.button3);
// button3.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(this);
}
private void setContentView() {
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
Button button = new Button(this);
button.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 9);
break;
case R.id.button2:
Button button2 = new Button(this);
button2.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 8);
break;
case R.id.button3:
Button button3 = new Button(this);
button3.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 7);
break;
case R.id.button4:
Button button4 = new Button(this);
button4.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 6);
break;
case R.id.button5:
Button button5 = new Button(this);
button5.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 5);
break;
case R.id.button6:
Button button6 = new Button(this);
button6.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 4);
break;
case R.id.button7:
Button button7 = new Button(this);
button7.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 3);
break;
case R.id.button8:
Button button8 = new Button(this);
button8.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 2);
break;
case R.id.button9:
Button button9 = new Button(this);
button9.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 1);
break;
case R.id.button10:
Button button10 = new Button(this);
button10.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 0);
break;
case R.id.button11:
Button button11 = new Button(this);
button11.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + '.');
break;
case R.id.button13:
x=Double.parseDouble((String)textView.getText());
op=1;
Button button13 = new Button(this);
button13.setBackgroundColor(Color.CYAN);
textView.setText(" ");
break;
case R.id.button14:
x=Double.parseDouble((String)textView.getText());
op=2;
Button button14 = new Button(this);
button14.setBackgroundColor(Color.CYAN);
textView.setText(" ");
break;
case R.id.button15:
x=Double.parseDouble((String)textView.getText());
op=3;
Button button15 = new Button(this);
button15.setBackgroundColor(Color.CYAN);
textView.setText(" ");
break;
case R.id.button16:
x=Double.parseDouble((String)textView.getText());
op=4;
Button button16 = new Button(this);
button16.setBackgroundColor(Color.CYAN);
textView.setText(" ");
break;
case R.id.button12:
y=Double.parseDouble((String) textView.getText());
Button button12 = new Button(this);
button12.setBackgroundColor(Color.CYAN);
switch(op)
{
case 1:
{
result=x+y;
break;
}
case 2:
{
result=x-y;
break;
}
case 3:
{
result=x*y;
break;
}
case 4:
{
result=x/y;
break;
}
default :
break;
}
if(result-Math.floor(result)==0)
{
result=Math.floor(result);
}
textView.setText(result+"");
break;
default:
break;
}
}
}
This is xml file
<?xml version="1.0" encoding="utf-8"?>
<Button
android:text="#string/button0"
android:id="#+id/button"
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:clickable="true"
android:contentDescription="#string/click1"
android:contextClickable="true" />
<TextView
android:layout_width="350dp"
android:layout_height="100dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/text1"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffffff" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:layout_marginLeft="2.5dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button7"
android:id="#+id/button3"
android:layout_alignTop="#+id/button13"
android:layout_toRightOf="#+id/button5" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button6"
android:id="#+id/button4"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button5"
android:id="#+id/button5"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:layout_toEndOf="#+id/button4"
android:layout_marginLeft="2.5dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button4"
android:id="#+id/button6"
android:layout_alignTop="#+id/button5"
android:layout_toRightOf="#+id/button5" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button3"
android:id="#+id/button7"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button10"
android:id="#+id/button8"
android:layout_below="#+id/button5"
android:layout_alignLeft="#+id/button5"
android:layout_alignStart="#+id/button5"
android:layout_marginTop="10dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button11"
android:id="#+id/button9"
android:layout_alignTop="#+id/button15"
android:layout_toRightOf="#+id/button8" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button12"
android:id="#+id/button10"
android:layout_below="#+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button13"
android:id="#+id/button11"
android:layout_below="#+id/button9"
android:layout_alignLeft="#+id/button8"
android:layout_alignStart="#+id/button8"
android:layout_marginTop="10dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button18"
android:id="#+id/button12"
android:layout_alignTop="#+id/button16"
android:layout_toRightOf="#+id/button11" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button14"
android:id="#+id/button13"
android:layout_above="#+id/button4"
android:layout_alignParentRight="true" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button15"
android:id="#+id/button14"
android:layout_alignTop="#+id/button6"
android:layout_alignParentRight="true" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button16"
android:id="#+id/button15"
android:layout_alignTop="#+id/button8"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button17"
android:id="#+id/button16"
android:layout_below="#+id/button9"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"/>
This is AndroidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This is the LogCat
12-21 22:15:23.620 1425-1425/com.example.kartikeya.calc D/dalvikvm: Not late-enabling CheckJNI (already on)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc D/AndroidRuntime: Shutting down VM
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb2fed180)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: FATAL EXCEPTION: main
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kartikeya.calc/com.example.kartikeya.calc.MainActivity}: java.lang.NullPointerException
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4424)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: Caused by: java.lang.NullPointerException
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.view.View.<init>(View.java:2696)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.view.View.<init>(View.java:2744)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.widget.TextView.<init>(TextView.java:449)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc `E/AndroidRuntime: at android.widget.TextView.<init>(TextView.java:442)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.widget.TextView.<init>(TextView.java:437)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at com.example.kartikeya.calc.MainActivity.<init>(MainActivity.java:14)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.Class.newInstanceImpl(Native Method)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1319)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:123) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4424) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 
12-21 22:15:24.140 1425-1431/com.example.kartikeya.calc I/dalvikvm: threadid=3: reacting to signal 3
12-21 22:15:24.140 1425-1431/com.example.kartikeya.calc I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt'
12-21 22:15:24.160 1425-1431/com.example.kartikeya.calc I/dalvikvm: threadid=3: reacting to signal 3
12-21 22:15:24.160 1425-1431/com.example.kartikeya.calc I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt'
12-21 22:15:27.801 1425-1425/? I/Process: Sending signal. PID: 1425 SIG: 9
Your problem is this line TextView textView = new TextView(this);
You are trying to call setText() on this view in your onClick methods, but this view hasn't been inflated. You have to inflate the view before you can manipulate it, just like you did with Button button = (Button) findViewById(R.id.button);
So in your onCreate() you would want to put in textView = (TextView) findViewById(R.id.your_text_view_id); and get rid of your TextView textView = new TextView(this); line -- just make it TextView textView;

Android: Unable to start activity: NullPointerException?

I am trying to open a New Activity within my application using an intent once a game on the previous activity is finished, like shown below. I am getting a NullPointerException, What am I doing wrong?
Code to open new activity:
//when no cards are left
if(totalcards==0){
//get the avg med and att values
getAverageMeditationValue();
getAverageAttentionValue();
if(getAverageMeditationValue() && getAverageAttentionValue()){
//add session detail to the SQLite DB
writeToDatabase();
//intent to open activty showing results of session
Intent t = new Intent(MemoryGame.this, com.example.brianapp.MemoryGameResults.class);
t.putExtra("singleScore", single);
// Start intent
startActivity(t);
//stop recording EEG data
device.close();
}
}
New activity:
public class MemoryGameResults extends Activity {
//declare vars
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
TextView tv5;
TextView tv6;
Session singleScore;
LoginDataBaseAdapter loginDataBaseAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memorygameresults);
initialiseVars();
setUpAllTextViews();
}
public void initialiseVars() {
// the single score from the the meditation class
singleScore = (Session) getIntent().getSerializableExtra(
"singleScore");
// Declaring textViews
tv1 = (TextView) findViewById(R.id.medresults1);
tv2 = (TextView) findViewById(R.id.medresults2);
tv3 = (TextView) findViewById(R.id.medresults3);
tv4 = (TextView) findViewById(R.id.medresults4);
tv5 = (TextView) findViewById(R.id.medresults5);
tv6 = (TextView) findViewById(R.id.medresults6);
}
/**
* Sets text for all textviews
* in activity
*/
public void setUpAllTextViews(){
// setting text for each text view
tv1.setText("Results of session:");
tv2.setText("Average Meditation Value: " + singleScore.getMeditation());
tv3.setText("Maximum Meditation Value: " + singleScore.getMax());
tv4.setText("Average Attention Value: " + singleScore.getAvgAttention());
tv5.setText("Maximum Attention Value: " + singleScore.getMaxAttention());
tv6.setText("Correct Answers: " + singleScore.getScore());
}
}
/**
* Method that ensures user is returned to main menu when they press back
* button
*/
#Override
public void onBackPressed() {
Intent t = new Intent(MemoryGameResults.this, com.example.brianapp.MainMenu.class);
startActivity(t);
}
}
When I try to do so I am getting the following error:
08-24 10:56:44.326: E/AndroidRuntime(17074): FATAL EXCEPTION: main
08-24 10:56:44.326: E/AndroidRuntime(17074): Process: com.example.brianapp, PID: 17074
08-24 10:56:44.326: E/AndroidRuntime(17074): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brianapp/com.example.brianapp.MemoryGameResults}: java.lang.NullPointerException
08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread.access$900(ActivityThread.java:161)
08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
08-24 10:56:44.326: E/AndroidRuntime(17074): at android.os.Handler.dispatchMessage(Handler.java:102)
08-24 10:56:44.326: E/AndroidRuntime(17074): at android.os.Looper.loop(Looper.java:157)
08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread.main(ActivityThread.java:5356)
08-24 10:56:44.326: E/AndroidRuntime(17074): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 10:56:44.326: E/AndroidRuntime(17074): at java.lang.reflect.Method.invoke(Method.java:515)
08-24 10:56:44.326: E/AndroidRuntime(17074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-24 10:56:44.326: E/AndroidRuntime(17074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-24 10:56:44.326: E/AndroidRuntime(17074): at dalvik.system.NativeStart.main(Native Method)
08-24 10:56:44.326: E/AndroidRuntime(17074): Caused by: java.lang.NullPointerException
08-24 10:56:44.326: E/AndroidRuntime(17074): at com.example.brianapp.MemoryGameResults.setUpAllTextViews(MemoryGameResults.java:323)
08-24 10:56:44.326: E/AndroidRuntime(17074): at com.example.brianapp.MemoryGameResults.onCreate(MemoryGameResults.java:51)
08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.Activity.performCreate(Activity.java:5426)
08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-24 10:56:44.326: E/AndroidRuntime(17074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
08-24 10:56:44.326: E/AndroidRuntime(17074): ... 11 more
Manifest sections relating to the two activities:
<activity
android:name="com.example.brianapp.MemoryGame"
android:label="Memory Game" >
<intent-filter>
<action android:name="com.example.brianapp.MemoryGame" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.brianapp.MemoryGameResults"
android:label="Memory Game Results" >
<intent-filter>
<action android:name="com.example.brianapp.MemoryGameResults" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
mathsgameresults.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/medresults1"
android:layout_width="290dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:textSize="20sp" >
</TextView>
<TextView
android:id="#+id/medresults2"
android:layout_width="290dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:layout_marginTop="10dp"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:textColor="#FFFFFF"
android:textSize="20sp" >
</TextView>
<TextView
android:id="#+id/medresults3"
android:layout_width="290dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#drawable/buttonshape3"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text=" "
android:textColor="#FFFFFF"
android:textSize="20sp" >
</TextView>
<TextView
android:id="#+id/medresults4"
android:layout_width="290dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#drawable/buttonshape2"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text=" "
android:textColor="#FFFFFF"
android:textSize="20sp" >
</TextView>
<TextView
android:id="#+id/medresults5"
android:layout_width="290dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:layout_marginTop="10dp"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:textColor="#FFFFFF"
android:textSize="20sp">
</TextView>
<TextView
android:id="#+id/medresults6"
android:layout_width="290dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#drawable/buttonshape3"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text=" "
android:textColor="#FFFFFF"
android:textSize="20sp" >
</TextView>
<Button
android:id="#+id/btnMathsResultsViewgraph"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:onClick="Graph8Handler"
android:text="View Graph "
android:textSize="20sp" />
</LinearLayout>
The problem is in these lines :
tv1 = (TextView) findViewById(R.id.medresults1);
tv2 = (TextView) findViewById(R.id.medresults2);
...
The textviews are not being initialized. Check your XML file(memorygameresults.xml) whether textviews with these IDs exist.

How to create a map activity in one activity on button click in other activity?

I am creating an android app for an University. I am trying to display a Location map in one activity by clicking a button in other activity. I have referred some links and worked, but I am getting error. Here is my code snippet and the Logcat. Please anybody help me in making this work.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.university"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<permission
android:name="com.example.university.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.university.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:glEsVersion="0x00000000" android:required="true" />
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDGPcb88FGrYilWxe41RbWog38pSgNfp5o" />
<application android:allowBackup="true" android:icon="#drawable/launcher"
android:theme="#style/Theme.AppCompat.Light" >
<activity android:name=".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=".About" android:label="#string/title_activity_about" >
</activity>
<activity android:name=".Chancellor" android:label="#string/title_activity_chancellor" >
</activity>
<activity android:name=".Prochancellor" android:label="#string/title_activity_prochancellor" >
</activity>
<activity android:name=".Programs" android:label="#string/title_activity_programs" >
</activity>
<activity android:name=".Contact" android:label="#string/title_activity_contact" >
</activity>
<activity android:name=".SplashActivity" android:label="#string/title_activity_splash" >
</activity>
<activity android:name=".Admissions" android:label="#string/title_activity_admissions" >
</activity>
<activity android:name=".Map" android:label="#string/title_activity_map" >
</activity>
</application>
</manifest>
THIS .JAVA AND .XML FILES FOR BUTTON AND ONCLICK FUNCTION TO WORK
Contact.java
package com.example.university;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class Contact extends ActionBarActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_contact.xml
setContentView(R.layout.activity_contact);
// Locate the button in activity_contact.xml
button = (Button)findViewById(R.id.button1);
// Capture button clicks
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(Contact.this,
Map.class);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.contact, 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.
switch (item.getItemId()) {
case R.id.about:
Intent a = new Intent(getApplicationContext(), About.class);
startActivity(a);
return true;
case R.id.chancellor:
Intent b = new Intent(getApplicationContext(), Chancellor.class);
startActivity(b);
return true;
case R.id.prochancellor:
Intent c = new Intent(getApplicationContext(), Prochancellor.class);
startActivity(c);
return true;
case R.id.programs:
Intent d = new Intent(getApplicationContext(), Programs.class);
startActivity(d);
return true;
case R.id.admissions:
Intent e = new Intent(getApplicationContext(), Admissions.class);
startActivity(e);
return true;
case R.id.contact:
Intent f = new Intent(getApplicationContext(), Contact.class);
startActivity(f);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_contact.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="#drawable/tiny_grid"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.university.Contact" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/campus1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_marginLeft="26dp"
android:layout_toRightOf="#+id/imageView1"
android:text="100 Feet Ring Road, BSK III Stage, Bangalore-560085"
android:textSize="11dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="+91 80 26721983"
android:textSize="11dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:text="+91 80 26722108"
android:textSize="11dp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:autoLink="email"
android:text="admissions#pes.edu"
android:textSize="11dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/textView1"
android:src="#drawable/campus2" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignTop="#+id/imageView2"
android:text=" Hosur Road Campus (1 Km before Electronic City), Bangalore - 560 100"
android:textSize="11dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/textView5"
android:text="+91 80 66186610"
android:textSize="11dp" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView6"
android:text="+91 80 66186611"
android:textSize="11dp" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/textView7"
android:autoLink="email"
android:text="admissions#pes.edu"
android:textSize="11dp" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView2"
android:layout_marginTop="25dp"
android:layout_toLeftOf="#+id/textView1"
android:src="#drawable/campus3" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_alignTop="#+id/imageView3"
android:text="National Highway 219, Kuppam, Andhra Pradesh - 517 425"
android:textSize="11dp" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView9"
android:layout_below="#+id/textView9"
android:text="085 70 256736"
android:textSize="11dp" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView10"
android:layout_below="#+id/textView10"
android:autoLink="email"
android:text="admissions#pes.edu"
android:textSize="11dp" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView3"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/textView1"
android:src="#drawable/campus4" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView11"
android:layout_alignTop="#+id/imageView4"
android:text="100 Feet Ring Road, BSK III Stage, Bangalore-560085"
android:textSize="11dp" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView12"
android:layout_below="#+id/textView12"
android:text="+91 80 26721983"
android:textSize="11dp" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView13"
android:layout_below="#+id/textView13"
android:text="+91 80 26722108"
android:textSize="11dp" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView14"
android:layout_below="#+id/textView14"
android:autoLink="email"
android:text="admissions#pes.edu"
android:textSize="11dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView4"
android:layout_alignParentBottom="true"
android:layout_marginLeft="16dp"
android:text="Button" />
</RelativeLayout>
THIS .JAVA NAD .XML FILES TO DISPLAY MAP
package com.example.university;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.widget.Toast;
public class Map extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
Log.v(">>>>>>>>>>>>>.", "successs");
}else{
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, 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.
switch (item.getItemId()) {
case R.id.about:
Intent a = new Intent(getApplicationContext(), About.class);
startActivity(a);
return true;
case R.id.chancellor:
Intent b = new Intent(getApplicationContext(), Chancellor.class);
startActivity(b);
return true;
case R.id.prochancellor:
Intent c = new Intent(getApplicationContext(), Prochancellor.class);
startActivity(c);
return true;
case R.id.programs:
Intent d = new Intent(getApplicationContext(), Programs.class);
startActivity(d);
return true;
case R.id.admissions:
Intent e = new Intent(getApplicationContext(), Admissions.class);
startActivity(e);
return true;
case R.id.contact:
Intent f = new Intent(getApplicationContext(), Contact.class);
startActivity(f);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Aactivity_map.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.university.Map" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Here is the Logcat when i click on the button in activity_contact.xml
07-26 02:22:16.012: I/Process(2244): Sending signal. PID: 2244 SIG: 9
07-26 02:22:20.062: D/dalvikvm(2268): GC_FOR_ALLOC freed 86K, 7% free 2998K/3200K, paused 221ms, total 269ms
07-26 02:22:20.292: I/dalvikvm-heap(2268): Grow heap (frag case) to 6.783MB for 3932172-byte allocation
07-26 02:22:20.372: D/dalvikvm(2268): GC_FOR_ALLOC freed 2K, 3% free 6835K/7044K, paused 79ms, total 79ms
07-26 02:22:21.552: D/gralloc_goldfish(2268): Emulator without GPU emulation detected.
07-26 03:10:59.626: D/dalvikvm(2376): GC_FOR_ALLOC freed 90K, 7% free 2997K/3204K, paused 191ms, total 202ms
07-26 03:10:59.876: I/dalvikvm-heap(2376): Grow heap (frag case) to 6.782MB for 3932172-byte allocation
07-26 03:11:00.126: D/dalvikvm(2376): GC_FOR_ALLOC freed 2K, 4% free 6835K/7048K, paused 248ms, total 248ms
07-26 03:11:07.206: D/gralloc_goldfish(2376): Emulator without GPU emulation detected.
07-26 03:11:09.626: D/dalvikvm(2376): GC_FOR_ALLOC freed 57K, 3% free 7068K/7260K, paused 31ms, total 36ms
07-26 03:11:09.896: I/Choreographer(2376): Skipped 58 frames! The application may be doing too much work on its main thread.
07-26 03:11:10.356: I/Choreographer(2376): Skipped 38 frames! The application may be doing too much work on its main thread.
07-26 03:11:21.527: D/dalvikvm(2376): GC_FOR_ALLOC freed 1266K, 17% free 7055K/8436K, paused 173ms, total 176ms
07-26 03:11:25.347: D/AndroidRuntime(2376): Shutting down VM
07-26 03:11:25.357: W/dalvikvm(2376): threadid=1: thread exiting with uncaught exception (group=0xb0cefb20)
07-26 03:11:25.397: E/AndroidRuntime(2376): FATAL EXCEPTION: main
07-26 03:11:25.397: E/AndroidRuntime(2376): Process: com.example.university, PID: 2376
07-26 03:11:25.397: E/AndroidRuntime(2376): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.university/com.example.university.Map}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.os.Handler.dispatchMessage(Handler.java:102)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.os.Looper.loop(Looper.java:136)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-26 03:11:25.397: E/AndroidRuntime(2376): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 03:11:25.397: E/AndroidRuntime(2376): at java.lang.reflect.Method.invoke(Method.java:515)
07-26 03:11:25.397: E/AndroidRuntime(2376): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-26 03:11:25.397: E/AndroidRuntime(2376): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-26 03:11:25.397: E/AndroidRuntime(2376): at dalvik.system.NativeStart.main(Native Method)
07-26 03:11:25.397: E/AndroidRuntime(2376): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-26 03:11:25.397: E/AndroidRuntime(2376): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Activity.setContentView(Activity.java:1929)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
07-26 03:11:25.397: E/AndroidRuntime(2376): at com.example.university.Map.onCreate(Map.java:20)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Activity.performCreate(Activity.java:5231)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-26 03:11:25.397: E/AndroidRuntime(2376): ... 11 more
07-26 03:11:25.397: E/AndroidRuntime(2376): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Fragment.instantiate(Fragment.java:597)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Fragment.instantiate(Fragment.java:561)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Activity.onCreateView(Activity.java:4778)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
07-26 03:11:25.397: E/AndroidRuntime(2376): ... 24 more
07-26 03:11:25.397: E/AndroidRuntime(2376): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path: DexPathList[[zip file "/data/app/com.example.university-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.university-2, /system/lib]]
07-26 03:11:25.397: E/AndroidRuntime(2376): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-26 03:11:25.397: E/AndroidRuntime(2376): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
07-26 03:11:25.397: E/AndroidRuntime(2376): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
07-26 03:11:25.397: E/AndroidRuntime(2376): at android.app.Fragment.instantiate(Fragment.java:583)
07-26 03:11:25.397: E/AndroidRuntime(2376): ... 27 more
You are doing a few things wrong as you probably didn't follow all the steps in using Google Maps on your application:
Google Maps API key is supposed to be inside of the application tags in your manifest.
You are missing the play services meta tag (also inside the application tags):
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Also make sure that you are compiling the play services with your project. If you're using Android-Studio, then the bottom of your build.gradle file should look like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services:5.0.77'
}

Basic Bluetooth application does not work

I want to realize data transfer with a remote Bluetooth server,Hence I began to explore androids Bluetooth functionality,I'm going step by step ,now I got stuck in a program. After loading the program to the emulator, it responds "Adil_Bluetooth_1st had stopped".
I provide you my files:
XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/acceleration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="X: Y: Z:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ProgressBar
android:id="#+id/progressBarX"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/acceleration"
android:layout_alignParentRight="true"
android:layout_below="#+id/acceleration"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="40dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/progressBarX"
android:layout_alignTop="#+id/acceleration"
android:text=" Noise Margin" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/progressBarX"
android:layout_below="#+id/progressBarX"
android:text="X-axis" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/progressBarZ"
android:layout_below="#+id/progressBarY"
android:text="Y-axis" />
<ProgressBar
android:id="#+id/progressBarY"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignRight="#+id/progressBarX"
android:layout_below="#+id/textView2"
android:layout_marginTop="72dp" />
<EditText
android:id="#+id/eText"
android:layout_width="50dp"
android:layout_height="45dp"
android:layout_alignLeft="#+id/textView1"
android:layout_alignTop="#+id/textView1"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/progressBarZ"
android:layout_alignTop="#+id/progressBarZ"
android:layout_marginTop="20dp"
android:text="Z-axis" />
<ProgressBar
android:id="#+id/progressBarZ"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/progressBarY"
android:layout_alignRight="#+id/progressBarY"
android:layout_below="#+id/textView3"
android:layout_marginTop="84dp" />
</RelativeLayout>
And this is my ActivityMain.java file:
package com.example.adil_bluetooth_1st;
import java.util.Set;
import javax.xml.datatype.Duration;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.bluetooth.*;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class Bluetooth extends Activity {
private BluetoothAdapter mBluetoothAdapter;
Integer REQ_BT_ENABLE=1;
private ArrayAdapter<String> mArrayAdapter;
Set<BluetoothDevice> pairedDevices;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
String Status;
if(mBluetoothAdapter==null)
{
Toast.makeText(this, "Device doesn't support Bluetooth", Toast.LENGTH_LONG).show();
}
/*
* checking whether Bluetooth is enabled ,if not enable it!
* */
if(mBluetoothAdapter.isEnabled())
{
String mydeviceadress=mBluetoothAdapter.getAddress();
String mydevicename=mBluetoothAdapter.getName();
Status="My Device Name is:"+mydevicename+"\nThe Adress is:"+mydeviceadress;
Toast.makeText(this, Status, Toast.LENGTH_LONG).show();
}
else
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQ_BT_ENABLE);
Toast.makeText(this, "Enabling Bluetooth!!", Toast.LENGTH_LONG).show();
}
/*
* Getting paired devices and saving them on an ArrayAdapter
* */
pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
/*Create a BroadcastReceiver for ACTION_FOUND
*
* */
final BroadcastReceiver Receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(Receiver, filter);
/*
* to make the device discoverable
* */
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bluetooth, menu);
return true;
}
}
LogCat messages:
> 04-04 16:41:58.051: E/BluetoothAdapter(1082): Bluetooth binder is null
> 04-04 16:41:58.111: D/AndroidRuntime(1082): Shutting down VM 04-04
> 16:41:58.111: W/dalvikvm(1082): threadid=1: thread exiting with
> uncaught exception (group=0x40a71930) 04-04 16:41:58.131:
> E/AndroidRuntime(1082): FATAL EXCEPTION: main 04-04 16:41:58.131:
> E/AndroidRuntime(1082): java.lang.RuntimeException: Unable to start
> activity
>
ComponentInfo{com.example.adil_bluetooth_1st/com.example.adil_bluetooth_1st.Bluetooth}:
> java.lang.NullPointerException 04-04 16:41:58.131:
> E/AndroidRuntime(1082): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
> 04-04 16:41:58.131: E/AndroidRuntime(1082): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
> 04-04 16:41:58.131: E/AndroidRuntime(1082): at
> android.app.ActivityThread.access$600(ActivityThread.java:141) 04-04
> 16:41:58.131: E/AndroidRuntime(1082): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
> 04-04 16:41:58.131: E/AndroidRuntime(1082): at
> android.os.Handler.dispatchMessage(Handler.java:99) 04-04
> 16:41:58.131: E/AndroidRuntime(1082): at
> android.os.Looper.loop(Looper.java:137) 04-04 16:41:58.131:
> E/AndroidRuntime(1082): at
> android.app.ActivityThread.main(ActivityThread.java:5041) 04-04
> 16:41:58.131: E/AndroidRuntime(1082): at
> java.lang.reflect.Method.invokeNative(Native Method) 04-04
> 16:41:58.131: E/AndroidRuntime(1082): at
> java.lang.reflect.Method.invoke(Method.java:511) 04-04 16:41:58.131:
> E/AndroidRuntime(1082): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
> 04-04 16:41:58.131: E/AndroidRuntime(1082): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-04
> 16:41:58.131: E/AndroidRuntime(1082): at
> dalvik.system.NativeStart.main(Native Method) 04-04 16:41:58.131:
> E/AndroidRuntime(1082): Caused by: java.lang.NullPointerException
> 04-04 16:41:58.131: E/AndroidRuntime(1082): at
> com.example.adil_bluetooth_1st.Bluetooth.onCreate(Bluetooth.java:38)
> 04-04 16:41:58.131: E/AndroidRuntime(1082): at
> android.app.Activity.performCreate(Activity.java:5104) 04-04
> 16:41:58.131: E/AndroidRuntime(1082): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
> 04-04 16:41:58.131: E/AndroidRuntime(1082): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
> 04-04 16:41:58.131: E/AndroidRuntime(1082): ... 11 more
Similar problem was answered here by Waqas
Your [Bluetooth] service might not be null because binding a service is an asynchronous method

java.lang.ClassCastException: android.widget.TextView. Why am I getting this?

for some reason, I am getting a ClassCastException error. I don't really know why.
Thanks in advance, I am a new programmer and any help would really help (no pun intended).
Code:
final EditText answerBox = (EditText) findViewById(R.id.answerBox);
final Button button = (Button) findViewById(R.id.button);
final TextView problem = (TextView) findViewById(R.id.problem);
final TextView status = (TextView) findViewById(R.id.status);
final TextView num = (TextView) findViewById(R.id.numerator);
final TextView denom = (TextView) findViewById(R.id.denominator);
button.setOnClickListener(new OnClickListener()
{
private String inputString;
public void onClick(View v)
{
inputString = problem.getText().toString();
int firstNumber = Integer.parseInt(inputString.substring(0,1));
int secondNumber = Integer.parseInt(inputString.substring(2,3));
int correct = firstNumber + secondNumber;
int input;
if(!answerBox.getText().toString().equals(""))
input = Integer.parseInt(answerBox.getText().toString());
else
input = -1;
if(input != -1)
{
if(input == correct)
{
status.setText("Nice! You are correct!");
denom.setText(Integer.parseInt(denom.getText().toString()) +1);
num.setText(Integer.parseInt(num.getText().toString()) +1);
}
else
{
status.setText("Sorry, but your answer was wrong.");
denom.setText(Integer.parseInt(denom.getText().toString()) +1);
}
}
int a = (int) (Math.random() * 10);
int b = (int) (Math.random() * 10);
String newProblem = Integer.toString(a) + "+" + Integer.toString(b);
problem.setText(newProblem.toString());
answerBox.setText("");
}
});
Once again, thanks.
Stuff from LogCat:
10-30 04:04:10.490: ERROR/AndroidRuntime(551): FATAL EXCEPTION: main
10-30 04:04:10.490: ERROR/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.benhsu.Addition/com.benhsu.Addition.AdditionActivity}: java.lang.ClassCastException: android.widget.TextView
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:507)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): Caused by: java.lang.ClassCastException: android.widget.TextView
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at com.benhsu.Addition.AdditionActivity.onCreate(AdditionActivity.java:24)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): ... 11 more
XML Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:id="#+id/problem" android:layout_height="wrap_content" android:text="#string/problemString"/>
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/answerBox">
<requestFocus></requestFocus>
</EditText>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/status" android:id="#+id/status"></TextView>
<Button android:text="Button" android:layout_height="wrap_content" android:id="#+id/button" android:layout_width="wrap_content"></Button>
<RelativeLayout android:id="#+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_weight="0.14">
<TextView android:layout_width="wrap_content" android:layout_alignParentLeft="true" android:layout_height="wrap_content" android:layout_alignBottom="#+id/textView1" android:text="#string/score" android:id="#+id/textView2" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignBaseline="#+id/textView1"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/numerator" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/textView2" android:layout_marginLeft="18dp" android:text="#string/numerator"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/fraction" android:id="#+id/fraction" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/numerator" android:layout_marginLeft="14dp"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/denominator" android:id="#+id/denominator" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/fraction" android:layout_marginLeft="19dp"></TextView>
</RelativeLayout>
</LinearLayout>
EDIT
It worked fine for me. In Eclipse, go to Project->Clean... and try again. It could be that your R file is out of sync.

Categories