Fatal Exception : Main on android app - android

I have a problem with my project. If you can to solve this problem please help me..
This is main code
import android.app.Activity;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.util.FloatMath;
import android.util.Log;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.lang.Math;
import com.kalkulator.R;
public class MainActivity extends Activity implements OnClickListener {
private String str1="",str2="",str3="",op="";
private Float num1,num2,num3,num4,num5;
private long a,b=1;
EditText ed;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ed=(EditText)findViewById(R.id.EditText01);
View[] keys=new View[30];
keys[0]= findViewById(R.id.button1);
keys[1]= findViewById(R.id.button2);
keys[2]= findViewById(R.id.button3);
for(int i=0;i<keys.length;i++)
{
keys[i].setOnClickListener(this);
}
ed.setText("");
}
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
str3=ed.getText().toString();
if(str3.contains("E"))
{
Toast.makeText(getApplicationContext(),"Factorial limit is upto 25", Toast.LENGTH_SHORT).show();
break;
}
a=Long.parseLong(str3);
if(a>25)
{
Toast.makeText(getApplicationContext(),"Factorial limit is upto 25", Toast.LENGTH_SHORT).show();
break;
}
for(long i=1;i<=a;i++)
{
b*=i;
}
ed.setText(String.valueOf(b));
b=1;
break;
......
And this is the xml code
<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" >
<EditText
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:cursorVisible="false"
android:gravity="right"
android:textStyle="italic" >
</EditText>
<TableLayout
android:id="#+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/EditText01"
android:layout_below="#+id/EditText01"
android:layout_marginTop="28dp"
android:stretchColumns="*" >
<TableRow
android:id="#+id/TableRow02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="n!" />
.......
I don't know why, but when i run this project on AVD_for_3_7_FWVGA_slide, i have notification on log cat FATAL EXCEPTION : Main.
Thanks

//setContentView(R.layout.main); must not be commented, and you should consider moving the orientation to your AndroidManifest file
Also,
keys[0]= findViewById(R.id.button1);
keys[1]= findViewById(R.id.button2);
keys[2]= findViewById(R.id.button3);
for(int i=0;i<keys.length;i++)
{
keys[i].setOnClickListener(this);
}
ed.setText("");
You're calling findViewById on the keys values with index > which gets you a NullPointerException.

Related

How to add and view child objects in Firebase (Android)?

I am having a similar issue in which I have my files as follow in which I want to be able to view all child items and add new child items.
I understand that you will have to modify the code in the Java file, but I am having trouble understanding how to manipulate my own code and I tried to follow the confusing documentation provided by Firebase.
Below is the structure of my database followed by my code. Any help would be greatly appreciated.
The only problem I have now is how to properly populate the listview with the values of the following structure of my database.
It is as follows: Messages --> Message --> value of which has concatenated string
I hope this isn't confusing
Here is my MainActivity.java
package com.example.sean.messengerappsean;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;
import com.firebase.client.collection.ArraySortedMap;
import com.firebase.client.realtime.util.StringListReader;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//TextView mTextFieldCondition;
EditText editUsername, editValue;
ArrayAdapter arrayAdapter;
Button btnSend;
Firebase mRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
//mTextFieldCondition = (TextView) findViewById(R.id.textViewCondition);
editUsername = (EditText) findViewById(R.id.editUsername);
editValue = (EditText) findViewById(R.id.editValue);
btnSend = (Button) findViewById(R.id.btnSend);
mRef = new Firebase("https://androidmessagetest.firebaseio.com/"); //CAN USE THE CODE HERE TO PUBLISH TO SPECIFIC LOCATION OR DB
//TRYING TO SET LISTVIEW
final ListView listView = (ListView) findViewById(R.id.listview);
final ArrayList<String> msgsArray = new ArrayList<String>(); //USE THIS FOR POPULATING THE VIEW
final ValueEventListener valueEventListener = mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//----------CONDITION FOR LISTING DATA----------------
//MIGHT NEED TO PUT VALUES INTO ARRAY AND READ THEM OUT IN ORDER OF PUBLISH
//String text = dataSnapshot.child("Message").child("Enter Username3").getValue(String.class);
//mTextFieldCondition.setText(text);
//POPULATE LISTVIEW
for (DataSnapshot Datasnapshot : dataSnapshot.getChildren()) {
msgsArray.add(Datasnapshot.child("Messages").child("Message").getKey());
}
ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, msgsArray);
listView.setAdapter(arrayAdapter);
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//NEED TO DYNAMICALLY CHANGE VALUE TO USERNAME: MESSAGE
String textoutput = String.valueOf(editUsername.getText());
String MessageText = String.valueOf(editValue.getText());
//FIXED MESSAGE ADD CHILD AND HOW TO ADD VALUE TO MESSAGE
//TRY TO USE BELOW CODE FOR MESSAGES LATER ON
mRef.child("Messages").child("Message").push().setValue(textoutput + ": " + MessageText);
}
});
//ADD FUNCTION TO LOOK FOR DATA
}
}
Here is my XML file activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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.sean.messengerappsean.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--<TextView
android:id="#+id/textViewCondition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/mTextFieldCondition"
android:layout_centerHorizontal="true"
android:layout_marginBottom="89dp"
android:text="Condition" />-->
//ENTER IN CODE BELOW FOR VIEWING OUTPUT OF DATA
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
<Button
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Send" />
<EditText
android:id="#+id/editValue"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/btnSend"
android:layout_toLeftOf="#+id/btnSend"
android:layout_toStartOf="#+id/btnSend"
android:backgroundTint="#color/colorPrimaryDark"
android:inputType="textMultiLine"
android:text="Enter Message Here" />
<EditText
android:id="#+id/editUsername"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/btnSend"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="bottom"
android:background="#color/colorAccent"
android:inputType="textCapWords"
android:text="Enter Username" />
</RelativeLayout>
These imports are for classes in the legacy SDK:
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;
These are for the new SDK:
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
You cannot mix the two SDKs. You should use the new SDK only. Remove this statement from your build.gradle dependencies:
compile 'com.firebase:firebase-client-android:2.x.x'
and make the approriate code changes to use the new SDK. Documentation and examples are here. There is also an Upgrade Guide.

Android Studio, adding multiple images on click

JAVA:
package com.example.scott.testapplication;
import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class HomeScreen extends Activity {
RelativeLayout Backround;
Button InitButton;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Backround = (RelativeLayout) findViewById(R.id.Backround);
InitButton = (Button) findViewById(R.id.InitButton);
image = (ImageView) findViewById(R.id.image);
InitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*insert code here*/
}
});
}
}
In order to insert the image, i'm thinking that I need to use a command like ImageView image = new (image), and then use another command to draw it on the display. I dont know which commands I should use.
XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Backround"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ImageView
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:scaleType="fitXY"
android:src="#drawable/DESERT2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Initiate"
android:id="#+id/InitButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical" />
</RelativeLayout>
I would like to create the image on the event of the click of a button, I also created the ImageView in the xml file. Not sure if I should change something there.
you can use
image.setImageResource(R.drawable.yourDesiredImageName);

Android Activity not Responding

I have 3 activities in my app and when user press next button, next activity is shown to user, Now everything works fine except when it reaches the last activity i.e. when user presses next on 2nd last activity, an error message is shown that app has stopped working and there is no error in LogCat, following is the .java file of my Final Activity
package com.example.first;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class FinalActivity extends ActionBarActivity {
TextView name,address,phone,email,dob,matg,mati,interg,interi,graddeg,gradi,cgpa,skills;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
name=(TextView)findViewById(R.id.fname);
address=(TextView)findViewById(R.id.faddress);
phone=(TextView)findViewById(R.id.fphone);
email=(TextView)findViewById(R.id.femail);
dob=(TextView)findViewById(R.id.fdob);
matg=(TextView)findViewById(R.id.matricgrade);
mati=(TextView)findViewById(R.id.matricinst);
interg=(TextView)findViewById(R.id.intergrade);
interi=(TextView)findViewById(R.id.interinst);
graddeg=(TextView)findViewById(R.id.graddegree);
cgpa=(TextView)findViewById(R.id.gradcgpa);
skills=(TextView)findViewById(R.id.iskills);
Intent in=getIntent();
UserBO bo=new UserBO();
bo.name=in.getStringExtra("name");
bo.address=in.getStringExtra("address");
bo.email=in.getStringExtra("email");
bo.phone=in.getStringExtra("phone");
bo.dob=in.getStringExtra("dob");
bo.mg=in.getStringExtra("mgrade");
bo.mi=in.getStringExtra("minst");
bo.ig=in.getStringExtra("igrade");
bo.ii=in.getStringExtra("iinst");
bo.gg=in.getStringExtra("gdeg");
bo.gi=in.getStringExtra("ginst");
bo.cgpa=in.getStringExtra("cgpa");
bo.skills=in.getStringExtra("skills");
name.setText("Name : "+bo.name);
address.setText("Address : "+bo.address);
email.setText("Email : "+bo.email);
phone.setText("Phone : "+bo.phone);
dob.setText("DOB : "+bo.dob);
matg.setText("Matric Grade : "+bo.mg);
mati.setText("Institution : "+bo.mi);
interg.setText("Inter Grade : "+bo.ig);
interi.setText("Institution : "+bo.ii);
graddeg.setText("Graduation Degree : "+bo.gg);
gradi.setText("Institution : "+bo.gi);
skills.setText("Skills : "+bo.skills);
}
}
Here is the xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.first.FinalActivity"
tools:ignore="MergeRootFrame" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/faddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/fphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/femail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/fdob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/matricgrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/matricinst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/intergrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/interinst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/graddegree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/gradinst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/gradcgpa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/iskills"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/hobbies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</ScrollView>
</FrameLayout>
Here i'm calling the final activity
package com.example.first;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class Activity2 extends ActionBarActivity {
Button next3;
EditText s1,s2,s3,s4,s5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
next3=(Button)findViewById(R.id.next3);
s1=(EditText)findViewById(R.id.sk1);
s2=(EditText)findViewById(R.id.sk2);
s3=(EditText)findViewById(R.id.sk3);
s4=(EditText)findViewById(R.id.sk4);
s5=(EditText)findViewById(R.id.sk5);
next3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=getIntent();
UserBO bo=new UserBO();
bo.name=in.getStringExtra("name");
bo.address=in.getStringExtra("address");
bo.email=in.getStringExtra("email");
bo.phone=in.getStringExtra("phone");
bo.dob=in.getStringExtra("dob");
bo.mg=in.getStringExtra("mgrade");
bo.mi=in.getStringExtra("minst");
bo.ig=in.getStringExtra("igrade");
bo.ii=in.getStringExtra("iinst");
bo.gg=in.getStringExtra("gdeg");
bo.gi=in.getStringExtra("ginst");
bo.cgpa=in.getStringExtra("cgpa");
bo.skills+=s1.getText().toString()+","+s1.getText().toString()+","+s2.getText().toString()+","+s3.getText().toString()+","+s4.getText().toString()+","+s5.getText().toString();
Intent i=new Intent(getApplicationContext(),FinalActivity.class);//this,same
System.out.println(bo.name);
i.putExtra("name", bo.name);
i.putExtra("address", bo.address);
i.putExtra("email", bo.email);
i.putExtra("phone", bo.phone);
i.putExtra("dob", bo.dob);
i.putExtra("mgrade", bo.mg);
i.putExtra("minst", bo.mi);
i.putExtra("igrade", bo.ig);
i.putExtra("iinst", bo.ii);
i.putExtra("gdeg", bo.gg);
i.putExtra("ginst", bo.gi);
i.putExtra("cgpa", bo.cgpa);
i.putExtra("skills", bo.skills);
startActivity(i);
}
});
}
Here is Activity1
package com.example.first;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class Activity1 extends ActionBarActivity {
Button next2;
EditText matGrade,matInst,iGrade,iInst,gDegree,gInst,gCgpa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
next2=(Button)findViewById(R.id.next2);
matGrade=(EditText)findViewById(R.id.mgrade);
matInst=(EditText)findViewById(R.id.minst);
iGrade=(EditText)findViewById(R.id.igrade);
iInst=(EditText)findViewById(R.id.iinst);
gDegree=(EditText)findViewById(R.id.gdegree);
gInst=(EditText)findViewById(R.id.ginst);
gCgpa=(EditText)findViewById(R.id.cgpa);
next2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=getIntent();
UserBO bo=new UserBO();
bo.name=in.getStringExtra("name");
bo.address=in.getStringExtra("address");
bo.email=in.getStringExtra("email");
bo.phone=in.getStringExtra("phone");
bo.dob=in.getStringExtra("dob");
bo.mg=matGrade.getText().toString();
bo.mi=matGrade.getText().toString();
bo.ig=iGrade.getText().toString();
bo.ii=iGrade.getText().toString();
bo.gg=gDegree.getText().toString();
bo.gi=gDegree.getText().toString();
bo.cgpa=gDegree.getText().toString();
Intent i=new Intent(getApplicationContext(),Activity2.class);//this,same
i.putExtra("name", bo.name);
i.putExtra("address", bo.address);
i.putExtra("email", bo.email);
i.putExtra("phone", bo.phone);
i.putExtra("dob", bo.dob);
i.putExtra("mgrade", bo.mg);
i.putExtra("minst", bo.mi);
i.putExtra("igrade", bo.ig);
i.putExtra("iinst", bo.ii);
i.putExtra("gdeg", bo.gg);
i.putExtra("ginst", bo.gi);
i.putExtra("cgpa", bo.cgpa);
startActivity(i);
}
});
}
}
Here is the main Activity
package com.example.first;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
Button next1;
EditText name,address,email,phone,date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next1=(Button)findViewById(R.id.next1);
name=(EditText)findViewById(R.id.name);
address=(EditText)findViewById(R.id.address);
email=(EditText)findViewById(R.id.email);
phone=(EditText)findViewById(R.id.phone);
date=(EditText)findViewById(R.id.date);
next1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
UserBO bo=new UserBO();
bo.name=name.getText().toString();
bo.address=address.getText().toString();
bo.email=email.getText().toString();
bo.phone=phone.getText().toString();
bo.dob=date.getText().toString();
Intent i=new Intent(getApplicationContext(),Activity1.class);//this,same
i.putExtra("name", bo.name);
i.putExtra("address", bo.address);
i.putExtra("email", bo.email);
i.putExtra("phone", bo.phone);
i.putExtra("dob", bo.dob);
startActivity(i);
}
});
}
}
Please help me find the mistake
I think you have null pointer exception. Maybe one of your controls is missed in your xml layout. For example you are finding "next1" in your code and using its setOnClickListener method while it is not defined in you xml layout so its object is null.
I recommend you put the content of onCreate method in a try-catch and debug your application.

debug error : confirm perspective switch

when i click on button add debug error
and opens confirm perspective switch dialog box
shows error in dis line " EditText add = (EditText) d1.findViewById(R.id.add); "
what is the mistake in my code??
xml page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question1" >
</TextView>
<EditText
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<EditText
android:id="#+id/answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<Button
android:id="#+id/registerques"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="registerques" >
</Button>
</LinearLayout>
java class
showing error in editext line
package quesansw.the1;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class Memo extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Dialog d1 = new Dialog(this);
Window window = d1.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
d1.setTitle("Register Questions");
d1.setContentView(R.layout.memo);
d1.show();
Button view1 = (Button) d1.findViewById(R.id.view);
Button add = (Button) d1.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText add = (EditText) d1.findViewById(R.id.add);
EditText view = (EditText) d1.findViewById(R.id.view);
System.out.println(add.toString());
System.out.println(view.toString());
}
});
view1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), View.class);
startActivity(intent);
}
});
}
}
Probably da error in the code is that you never defined an id "add" - you have #+id/TextView01 in the xml "page" (you mean file - and you must tell us which file) but no #+id/add

Runtime Android exception after pressing a button to go to next Activity

I have this code:
package com.problemio;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ProblemioActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addProblemButton = (Button)findViewById(R.id.add_problem_button);
Button browseProblemsButton = (Button)findViewById(R.id.browse_problems_button);
Button searchProblemsButton = (Button)findViewById(R.id.search_problems_button);
Button myProblemsButton = (Button)findViewById(R.id.my_problems_button);
addProblemButton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
Intent myIntent = new Intent(ProblemioActivity.this, AddProblemActivity.class);
ProblemioActivity.this.startActivity(myIntent);
}
});
}
}
It compiles fine and displays the addProblemButton button, but when that button is clicked, the system gives a runtime exception.
Here is the AddProblemActivity class:
package com.problemio;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AddProblemActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//TextView text = (TextView) dialog.findViewById(R.id.addProblemText);
//text.setText(R.string.addProblemText);
TextView tv = new TextView(this);
tv.setText("Please Add a Problem");
setContentView(tv);
}
}
and here is the layout main.xml
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First, add the problem you want to solve!"
/>
<TextView
android:id="#+id/add_problem_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add a Problem You Want To See Solved"
/>
<Button
android:id="#+id/add_problem_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add a Problem"
/>
<Button
android:id="#+id/browse_problems_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Browse Problems"
/>
<Button
android:id="#+id/search_problems_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search Problems"
/>
<Button
android:id="#+id/my_problems_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="View My Problems"
/>
</LinearLayout>
any idea what might be going wrong? By the way, I can't seem to locate the stack trace of the exception. Where should I look for that in Eclipse? All it currently shows me is AndroidRuntimeException - dalvik.system.NativeStart.main
Thanks!!
The only problem I can think of is that your Activity "AddProblemActivity" is not register in the manifest.
See the logs under LogCat...you will find it in Window >Show View >Android > LogCat

Categories

Resources