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.
Related
package kdev.circles;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ca = (EditText)findViewById(R.id.ca);
final EditText cp = (EditText)findViewById(R.id.cp);
final EditText sv = (EditText)findViewById(R.id.sv);
final EditText r = (EditText)findViewById(R.id.radius);
final TextView log = (TextView)findViewById(R.id.log);
Button clear = (Button)findViewById(R.id.clear);
Button solve = (Button)findViewById(R.id.Solve);
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ca.setText("");
cp.setText("");
sv.setText("");
r.setText("");
}
});
solve.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if((r.toString()).equals("")) {
log.setText(log.getText() + "\n" + "error, no value entered");
}else{
sv.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText())) * Double.valueOf(String.valueOf(r.getText())) * Double.valueOf(String.valueOf(r.getText())) * 4 / 3 * 3.14159265));
cp.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText()))*3.14159265*2));
ca.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText()))*Double.valueOf(String.valueOf(r.getText()))*3.14159265));
log.setText(log.getText()+"\n"+"ca"+ca.getText()+"\n"+"sv"+sv.getText()+"\n"+"cp"+cp.getText());
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
xml file:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Solve"
android:id="#+id/Solve"
android:width="100dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/sv" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/radius"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="49dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignStart="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText3"
android:layout_below="#+id/editText2"
android:layout_alignStart="#+id/editText2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="circle's perimeter"
android:id="#+id/cp"
android:layout_above="#+id/editText2"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="circle's area"
android:id="#+id/ca"
android:layout_above="#+id/editText3"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="sphere's volume"
android:id="#+id/sv"
android:layout_alignBottom="#+id/editText3"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="radius"
android:id="#+id/textView3"
android:layout_alignBottom="#+id/radius"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear"
android:id="#+id/clear"
android:layout_alignBottom="#+id/Solve"
android:layout_alignStart="#+id/editText3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/log"
android:layout_below="#+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
Everything is working fine till i try opening it, i couldn't find the error, because my android virtual device wasn't connected to android studio properly. I am not sure what i should do. I did have another problem before but i realised that it was due to me using the findviewbyid function before the whole layout was loaded. I have solved that problem and tried a few different techniques to solve this problem but nothing worked.
When i try and run the app it just crashes, i am not sure why?
Any help will be usefull, thank you.
this is the logcat:
12-10 15:21:18.030 1907-1907/kdev.circles W/art﹕ Verification of java.lang.CharSequence android.support.v7.widget.ResourcesWrapper.getQuantityText(int, int) took 123.881364ms
12-10 15:21:18.520 1907-1907/kdev.circles D/AndroidRuntime﹕ Shutting down VM
12-10 15:21:18.520 1907-1907/kdev.circles E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: kdev.circles, PID: 1907
java.lang.RuntimeException: Unable to start activity ComponentInfo{kdev.circles/kdev.circles.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at kdev.circles.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-10 15:21:27.230 1907-1907/kdev.circles I/Process﹕ Sending signal. PID: 1907 SIG: 9
12-10 15:21:31.360 2153-2153/kdev.circles D/AndroidRuntime﹕ Shutting down VM
12-10 15:21:31.360 2153-2153/kdev.circles E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: kdev.circles, PID: 2153
java.lang.RuntimeException: Unable to start activity ComponentInfo{kdev.circles/kdev.circles.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
at kdev.circles.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
Check your view types in Java code and their matching id's in xml. They have to be of same type.
You have EditText in Java, but matching view id in xml is TextView
For instance:
final EditText ca = (EditText)findViewById(R.id.ca);
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="circle's area"
android:id="#+id/ca"
android:layout_above="#+id/editText3"
android:layout_alignParentStart="true" />
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;
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'
}
I am creating a VERY basic POS system for an assignment for class. I keep getting that dreaded nullpointerexception. I know the error is coming from line 72 in main activity but it is declared in both the main activity and in the xml.
Here is the logcat:
10-07 18:05:36.946: D/AndroidRuntime(1279): Shutting down VM
10-07 18:05:36.946: W/dalvikvm(1279): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-07 18:05:36.958: E/AndroidRuntime(1279): FATAL EXCEPTION: main
10-07 18:05:36.958: E/AndroidRuntime(1279): java.lang.RuntimeException: Unable to start activity ComponentInfo{hotchkissmobilesolutions.kudlerpos/hotchkissmobilesolutions.kudlerpos.MainActivity}: java.lang.NullPointerException
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.os.Looper.loop(Looper.java:137)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-07 18:05:36.958: E/AndroidRuntime(1279): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 18:05:36.958: E/AndroidRuntime(1279): at java.lang.reflect.Method.invoke(Method.java:525)
10-07 18:05:36.958: E/AndroidRuntime(1279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-07 18:05:36.958: E/AndroidRuntime(1279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-07 18:05:36.958: E/AndroidRuntime(1279): at dalvik.system.NativeStart.main(Native Method)
10-07 18:05:36.958: E/AndroidRuntime(1279): Caused by: java.lang.NullPointerException
10-07 18:05:36.958: E/AndroidRuntime(1279): at hotchkissmobilesolutions.kudlerpos.MainActivity.onCreate(MainActivity.java:72)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.Activity.performCreate(Activity.java:5133)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-07 18:05:36.958: E/AndroidRuntime(1279): ... 11 more
Here is the android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hotchkissmobilesolutions.kudlerpos"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="hotchkissmobilesolutions.kudlerpos.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>
</application>
</manifest>
here is the Main Activity:
package hotchkissmobilesolutions.kudlerpos;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
double dairy = 4.99, seafood = 15.99, wine = 20, meat = 5.99,
cheese = 2.99, fruit = 1.99, vegetable = 0.69;
double total, tax = 0.0725;
String total2;
Button btnDairy, btnSeafood, btnWine, btnMeat, btnFruit, btnVegetable,
btnTotal, btnClear;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDairy = (Button) findViewById(R.id.btnDairy);
btnSeafood = (Button) findViewById(R.id.btnSeafood);
btnWine = (Button) findViewById(R.id.btnWine);
btnMeat = (Button) findViewById(R.id.btnMeat);
btnFruit = (Button) findViewById(R.id.btnFruit);
btnVegetable = (Button) findViewById(R.id.btnVegetable);
btnClear = (Button) findViewById(R.id.btnClear);
btnDairy.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + dairy) + (total * tax);
System.out.println(total);
}
});
btnSeafood.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + seafood) + (total * tax);
System.out.println(total);
}
});
btnWine.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + wine) + (total * tax);
System.out.println(total);
}
});
btnMeat.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + meat) + (total * tax);
System.out.println(total);
}
});
btnFruit.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + fruit) + (total * tax);
System.out.println(total);
}
});
btnVegetable.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + vegetable) + (total * tax);
System.out.println(total);
}
});
btnTotal.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total2 = new Double(total).toString();
final TextView textView = (TextView) findViewById(R.id.textViewTotal);
textView.setText(total2);
System.out.println(total);
System.out.println(total2);
}
});
btnClear.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = 0;
total2 = new Double(total).toString();
final TextView textView = (TextView) findViewById(R.id.textViewTotal);
textView.setText(total2);
System.out.println(total);
}
});
}
and here is the activity_main.xml:
<?xml version="1.0" encoding="UTF-8"?>
<GridLayout
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:columnCount="6"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:rowCount="8"
tools:context=".MainActivity"
tools:targetApi="14" >
<TextView android:id="#+id/txtViewInstructions"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0"
android:text="#string/InstructionText"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button android:id="#+id/btnDairy"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="3"
android:onClick="onCLickDairy"
android:text="#string/Dairy" />
<Button android:id="#+id/btnWine"
android:layout_column="0"
android:layout_gravity="center_horizontal|top"
android:layout_row="3"
android:onClick="onCLickWine"
android:text="#string/Wine" />
<Button android:id="#+id/btnMeat"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="4"
android:onClick="onCLickMeat"
android:text="#string/Meat" />
<Button android:id="#+id/btnSeafood"
android:layout_column="0"
android:layout_gravity="center_horizontal|top"
android:layout_row="4"
android:onClick="onCLickSeafood"
android:text="#string/Seafood" />
<Button android:id="#+id/btnVegetable"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="5"
android:onClick="onCLickVegetable"
android:text="#string/Vegetable" />
<TextView android:id="#+id/txtViewTotalDue"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="7"
android:text="#string/TotalDue"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView android:id="#+id/textViewTotal"
android:layout_width="260dp"
android:layout_height="93dp"
android:layout_column="0"
android:layout_gravity="left|bottom"
android:layout_row="6"
android:text="#string/total"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button android:id="#+id/btnTotal"
android:layout_column="0"
android:layout_gravity="left|center_vertical"
android:layout_row="7"
android:onClick="onCLickTotal"
android:text="#string/TotalButton" />
<Button android:id="#+id/btnFruit"
android:layout_column="0"
android:layout_gravity="center_horizontal|top"
android:layout_row="5"
android:onClick="onCLickFruit"
android:text="#string/Fruit" />
<Button android:id="#+id/btnClear"
android:layout_column="0"
android:layout_gravity="left|bottom"
android:layout_row="7"
android:text="#string/ClearButton" />
</GridLayout>
Any help in finding the issue with the btnTotal line in the main activity.java would be so helpful. Thanks in advance!
You don't have a btnTotal so when you click on it, the value is null.
See:
btnDairy = (Button) findViewById(R.id.btnDairy);
btnSeafood = (Button) findViewById(R.id.btnSeafood);
btnWine = (Button) findViewById(R.id.btnWine);
btnMeat = (Button) findViewById(R.id.btnMeat);
btnFruit = (Button) findViewById(R.id.btnFruit);
btnVegetable = (Button) findViewById(R.id.btnVegetable);
btnClear = (Button) findViewById(R.id.btnClear);
//add this
btnTotal = (Button) findViewById(R.id.btnTotal);
No button for btnTotal. Add that button and you'll be fine. Simple mistake. Happens all the time. Good luck.
You did not initialise your total field, so it throws NPE
I know there are already numerous entries about how to solve a Fatal Exception, but none of them solved my problem. I am working on a very simple calculator that has nine buttons, an edit text, and a text view. The whole purpose of it is to simply be able to key in a number, hit a button with another number on it, and the product of those will appear in the text view.
<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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="#string/number" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1"
android:layout_marginLeft="16dp"
android:layout_marginTop="23dp"
android:text="#string/s1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_centerHorizontal="true"
android:text="#string/s2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_alignRight="#+id/textView1"
android:text="#string/s3" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="16dp"
android:text="#string/s4" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button4"
android:layout_alignBottom="#+id/button4"
android:layout_alignLeft="#+id/button2"
android:text="#string/s5" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:text="#string/s6" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button4"
android:layout_below="#+id/button4"
android:layout_marginTop="22dp"
android:text="#string/s7" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button7"
android:layout_alignBottom="#+id/button7"
android:layout_alignLeft="#+id/button5"
android:text="#string/s8" />
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button8"
android:layout_alignBottom="#+id/button8"
android:layout_alignLeft="#+id/button6"
android:text="#string/s9" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button8"
android:layout_below="#+id/button8"
android:layout_marginTop="70dp"
/>
</RelativeLayout>
I finally ironed out all the kinks, or so I thought, but now when I try to run the program it stops unexpectedly before even loading:
11-10 11:50:13.198: E/AndroidRuntime(451): FATAL EXCEPTION: main
11-10 11:50:13.198: E/AndroidRuntime(451): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.deitel.multiplicationtables/com.deitel.multiplicationtables.Main}: java.lang.InstantiationException: com.deitel.multiplicationtables.Main
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.os.Looper.loop(Looper.java:123)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-10 11:50:13.198: E/AndroidRuntime(451): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 11:50:13.198: E/AndroidRuntime(451): at java.lang.reflect.Method.invoke(Method.java:507)
11-10 11:50:13.198: E/AndroidRuntime(451): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-10 11:50:13.198: E/AndroidRuntime(451): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-10 11:50:13.198: E/AndroidRuntime(451): at dalvik.system.NativeStart.main(Native Method)
11-10 11:50:13.198: E/AndroidRuntime(451): Caused by: java.lang.InstantiationException: com.deitel.multiplicationtables.Main
11-10 11:50:13.198: E/AndroidRuntime(451): at java.lang.Class.newInstanceImpl(Native Method)
11-10 11:50:13.198: E/AndroidRuntime(451): at java.lang.Class.newInstance(Class.java:1409)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-10 11:50:13.198: E/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
11-10 11:50:13.198: E/AndroidRuntime(451): ... 11 more
11-10 11:55:13.367: I/Process(451): Sending signal. PID: 451 SIG: 9
I know it's probably some small over sight, but I can't find what it is that I have messed up on. Any assistance is appreciated!
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deitel.multiplicationtables"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
JAVA Code:
package com.deitel.multiplicationtables;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
//Implements the listener for an onclick event (implements View.onClickListener)
public abstract class Main extends Activity implements View.OnClickListener{
// creates a button
private Button bone, btwo, bthree, bfour, bfive, bsix, bseven, beight, bnine;
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//assigns the resource id of 1 - 9 to each button.
bone = (Button) findViewById(R.id.button1);
btwo = (Button) findViewById(R.id.button2);
bthree = (Button) findViewById(R.id.button3);
bfour = (Button) findViewById(R.id.button4);
bfive = (Button) findViewById(R.id.button5);
bsix = (Button) findViewById(R.id.button6);
bseven = (Button) findViewById(R.id.button7);
beight = (Button) findViewById(R.id.button8);
bnine = (Button) findViewById(R.id.button9);
//Adds the buttons to the onclicklistener
bone.setOnClickListener(this);
btwo.setOnClickListener(this);
bthree.setOnClickListener(this);
bfour.setOnClickListener(this);
bfive.setOnClickListener(this);
bsix.setOnClickListener(this);
bseven.setOnClickListener(this);
beight.setOnClickListener(this);
bnine.setOnClickListener(this);
}
//creates a method (or action) for when the button is clicked.
public void onclick(View view)
{
//Makes a variable for the entered number
Double amount = 0.0;
Double product = 0.0;
Double variable = 0.0;
// constants
final double one = 1.0;
final double two = 2.0;
final double three = 3.0;
final double four = 4.0;
final double five = 5.0;
final double six = 6.0;
final double seven = 7.0;
final double eight = 8.0;
final double nine = 9.0;
if (view.getId() == R.id.button1)
{
variable = one;
}
if (view.getId() == R.id.button2)
{
variable = two;
}
if (view.getId()== R.id.button3)
{
variable = three;
}
if (view.getId() == R.id.button4)
{
variable = four;
}
if (view.getId() == R.id.button5)
{
variable = five;
}
if (view.getId()== R.id.button6)
{
variable = six;
}
if (view.getId() == R.id.button7)
{
variable = seven;
}
if (view.getId() == R.id.button8)
{
variable = eight;
}
if (view.getId()== R.id.button9)
{
variable = nine;
}
//creates an editext and assigns the resource id of the xml edittext.
EditText number = (EditText)findViewById(R.id.editText1);
//Receives the input from the edittext, converts it to a double (number).
amount = Double.parseDouble(number.getText().toString());
//Calculates the product
product = variable * amount;
//Creates a textview object, assigns the xml r.id, and then changes the text to report the amount.
TextView t = (TextView)findViewById(R.id.textView2);
t.setText("Your product is: " + product);
}
}
Remove abstract keyword from your class and it should work
The log clearly says
Caused by: java.lang.InstantiationException: com.deitel.multiplicationtables.Main
check your Main class, it should not be abstract.