I am trying to accept a phrase and pass it to other class called Animation.And I am using a self defined class called Error to store if error happens and what word causes the error.But when the button representing the class is clicked it displays "unfortunately the application has stopped".Check out partial code of the class below. Thank you for your help in advance.
public class Convert extends Activity implements OnClickListener {
EditText inputConvert;
TextView correct;
String correction;
String[] message = getResources().getStringArray(R.array.basic_words);
final Context context = this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.convert);
View convert = findViewById(R.id.conv_button);
convert.setOnClickListener(this);
}
public void onClick(View v)
{
inputConvert=(EditText) findViewById(R.id.inputConvert);
String phrase = inputConvert.getText().toString();
if(v.getId()==R.id.conv_button)
{
Error obj1= new Error(false ,"initial");
obj1=check(phrase);
if(obj1.flag)
startAnim(phrase);
else
{
correction = build(obj1.word);
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.error);
Button dialogButton = (Button)dialog.findViewById(R.id.dialogButtonOK);
correct= (TextView) findViewById(R.id.error_content);
correct.setText(correction);
dialog.setTitle(R.string.help);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
}
public String build(String word)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("the word \"");
stringBuilder.append(word);
stringBuilder.append(" \" is not found please try to use a synomym");
String finalString = stringBuilder.toString();
return finalString;
}
public Error check(String word)
{
Error obj2 = new Error(false ,"initial");
obj2.word=word;
String[] words = word.split(" ");
for(int i=0; i< words.length; i++)
{
for(int j=0; i< message.length; j++)
{
if(words[i]==message[j])
obj2.flag=true;
break;
}
if(!obj2.flag)
obj2.word=words[i];
break;
}
return obj2;
}
public void startAnim(String phrase)
{
Intent j = new Intent(this, Animation.class);
j.putExtra("phrase",phrase);
startActivity(j);
}
Here is the code of the Animation activity.
public class Animation extends Activity
{
AnimationDrawable animation;
TextView errorMessage= (TextView)findViewById(R.id.testex);
#SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.animation);
Intent j= getIntent();
CharSequence message = j.getStringExtra("phrase");
String phrase=message.toString();
String[] words = phrase.split(" ");
animation = new AnimationDrawable();
int imid=0;
for(int i=0; i< words.length; i++)
{
imid=getResources().getIdentifier(words[i], "drawable", getPackageName());
animation.addFrame(getResources().getDrawable(imid), 700);
}
animation.setOneShot(false);
ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundDrawable(animation);
animation = (AnimationDrawable) img.getBackground();
// Start the animation (looped playback by default).
animation.start();
}
}
Logcat.
12-12 08:32:04.962: E/AndroidRuntime(2273): FATAL EXCEPTION: main
12-12 08:32:04.962: E/AndroidRuntime(2273): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.ihear/org.example.ihear.Convert}: java.lang.NullPointerException
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.os.Looper.loop(Looper.java:137)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.reflect.Method.invoke(Method.java:525)
12-12 08:32:04.962: E/AndroidRuntime(2273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-12 08:32:04.962: E/AndroidRuntime(2273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-12 08:32:04.962: E/AndroidRuntime(2273): at dalvik.system.NativeStart.main(Native Method)
12-12 08:32:04.962: E/AndroidRuntime(2273): Caused by: java.lang.NullPointerException
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
12-12 08:32:04.962: E/AndroidRuntime(2273): at org.example.ihear.Convert.(Convert.java:19)
12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.Class.newInstanceImpl(Native Method)
12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.Class.newInstance(Class.java:1130)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-12 08:32:04.962: E/AndroidRuntime(2273): ... 11 more
Caused by: java.lang.NullPointerException
at org.example.ihear.Convert.(Convert.java:19)
here is your real error.please check it.
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo:{....}
means that Android OS could not create your activity.
The most probable error is that you forgot to add it to your AndroidManifest. I suspect that it is related to the Animation activity.
Try to declare
<activity android:name="**YOUR.PACKAGE**.Animation" />
See : java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
If the declaration of the activity in the manifest was not the problem, try to inspect the constructor / onCreate methods of your Animation activity.
R.layout.convert is this layout right?
You can check it and make sure you have not used any element can not be found
Related
I'm wanting to let the user create an avatar (profile picture if you like) when they set up their user info. I've created a method for a single click/touch which would ask the user to take a picture and one for a long click which would ask the user to choose a picture from their gallery.
Below are my methods from the class file:
public void onLaunchCamera(View v) {
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strAvatarPrompt = "Take your picture to store as your avatar!";
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
}
});
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
String strAvatarPrompt = "Choose a picture to use as your avatar!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
return true;
}
});
}
And below is the XML associated with the ImageButton:
<ImageButton
android:id="#+id/ImageButton_Avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="#dimen/avatar_size"
android:minHeight="#dimen/avatar_size"
android:onClick="onLaunchCamera"
android:scaleType="fitXY"
android:src="#drawable/avatar"></ImageButton>
All it does is crash when I click on the ImageButton and I have no idea why. Any ideas?
Thanks
EDIT: Adding the logcat below (Sorry about the formatting. Couldn't work out how to get it all sorted properly:
[ 04-12 18:32:50.989 5901: 5901 D/ ]
HostConnection::get() New Host Connection established 0xb8a44530, tid 5901
04-12 18:32:51.039 5901-5901/cct.mad.lab D/OpenGLRenderer: Enabling debug mode 0
04-12 18:32:55.739 5901-5901/cct.mad.lab V/RenderScript: 0xb8c53300 Launching thread(s), CPUs 2
04-12 18:32:57.389 5901-5901/cct.mad.lab D/AndroidRuntime: Shutting down VM
04-12 18:32:57.389 5901-5901/cct.mad.lab W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb0d2db20)
04-12 18:32:57.399 5901-5901/cct.mad.lab E/AndroidRuntime: FATAL EXCEPTION: main
Process: cct.mad.lab, PID: 5901 java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at a android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at cct.mad.lab.SettingsActivity.onLaunchCamera(SettingsActivity.java:201)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
It looks like you might not have defined avatarButton, if you follow the Caused By path on the LogCat you see the bottom one is a NullPointerException.
Since I can't see the line numbers, the issue is happening on line 201--the only obvious null pointer I see in your code is avatarButton.
Based on what you want to do, you'll want to go about this a bit differently.
Remove the android:onClick="onLaunchCamera" from the XML.
in your onCreate() after you set the content view add the following:
View avatarButton = findViewById(R.id.ImageButton_Avatar);
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strAvatarPrompt = "Take your picture to store as your avatar!";
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
}
});
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
String strAvatarPrompt = "Choose a picture to use as your avatar!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
return true;
}
});
This allows you to set both a click and a longClick listener with more control. The way you had it, you were never really defining the onClick or onLongClick until you clicked on them the first time.
So I am programmatically adding buttons to an already existing RelativeLayout.
I get a fatal error where running this code without any error message.
My android app doesn't even open.
Though when I take out the if/else below it works fine.
Am I not allowed to use an if statement to populate a layout or am I implementing this the wrong way?
*Note that below code is only pseudo code. Everything works as expected when the if/else is commented out
**edit adding full code and logcat
protected void onCreate(Bundle savedInstanceState)
{
//load main screen
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayoutTop = (RelativeLayout)findViewById(R.id.RelativeLayout1);
boolean bool = getBool();
if(bool)
{
//some parameters for the sign in button
RelativeLayout.LayoutParams button1Param = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//make the new button and set attributes
Button button1 = new Button(this);
button1.setText("b1");
button1.setLayoutParams(button1Param);
button1.setId(1);
//create an on click listener to start the activity
button1.setOnClickListener(new OnClickListener() {
//on click execute activity
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), FirstActivity.class));
}
});
}
else
{
//some parameters for the status button
RelativeLayout.LayoutParams statusParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ToggleButton buttonStatus = new ToggleButton(this);
buttonStatus.setText("Status");
buttonStatus.setLayoutParams(statusParam);
buttonStatus.setId(2);
relativeLayoutTop.addView(buttonStatus);
}
}
Log is here:
07-16 10:33:52.900: E/AndroidRuntime(1648): FATAL EXCEPTION: main
07-16 10:33:52.900: E/AndroidRuntime(1648): Process: shout.locate, PID: 1648
07-16 10:33:52.900: E/AndroidRuntime(1648): java.lang.RuntimeException: Unable to start activity ComponentInfo{shout.locate/shout.locate.MainActivity}: java.lang.NullPointerException
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.os.Looper.loop(Looper.java:136)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 10:33:52.900: E/AndroidRuntime(1648): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 10:33:52.900: E/AndroidRuntime(1648): at java.lang.reflect.Method.invoke(Method.java:515)
07-16 10:33:52.900: E/AndroidRuntime(1648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 10:33:52.900: E/AndroidRuntime(1648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 10:33:52.900: E/AndroidRuntime(1648): at dalvik.system.NativeStart.main(Native Method)
07-16 10:33:52.900: E/AndroidRuntime(1648): Caused by: java.lang.NullPointerException
07-16 10:33:52.900: E/AndroidRuntime(1648): at shout.locate.MainActivity.initLayout(MainActivity.java:79)
07-16 10:33:52.900: E/AndroidRuntime(1648): at shout.locate.MainActivity.onCreate(MainActivity.java:57)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.Activity.performCreate(Activity.java:5231)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-16 10:33:52.900: E/AndroidRuntime(1648): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-16 10:33:52.900: E/AndroidRuntime(1648): ... 11 more
Try this:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayoutTop = (RelativeLayout)findViewById(R.id.RelativeLayout1);
if(someboolean)
{
//set layouparams
//new button1
//button1.setonclicklistener etc etc
//set new layoutparams
//new button2
//button2.setonclicklistener etc etc
}
else
{
//set layouparams
//new button3
//button.setonclicklistener etc etc
}
}
You should get your layout only after setContentView(R.layout.activity_main);
EDIT
You are not adding the button to layout in if statement
v.getContext() might be null.Use YourCurrentActivity.this instead.
The code:
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
display();
});
}
I am using AdMob interstitial Ad, but somehow when the line was run, there was java.lang.NullPointerException. The method is in a method for timer, as shown below:
public void progress(){
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
public void run(){
runOnUiThread(new Runnable() {
public void run(){
progress -= 1;
totalTime += 500 - (level - 1) * 50;
timeForLevel += 500 - (level - 1) * 50;
showTimeForLevel();
showTotalTime();
if(progress < 0){
timer.cancel();
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
display();
}
});
Intent loseIntent = new Intent(Game.this, Lose.class);
loseIntent.putExtra("level", level);
loseIntent.putExtra("score", score);
loseIntent.putExtra("time", totalTime);
loseIntent.putExtra("taps", totalTaps);
startActivityForResult(loseIntent, 0);
}
if(progress >= 0){
setGraphics();
}
po.getProgress(progress, scrWidth);
progressGraphics();
}
});
}
}, (long)(500 / Math.pow(1 + 0.2/1, level)), (long)(500 / Math.pow(1 + 0.2/1, level)));
}
What is causing this exception to happen? And how am I going to solve it?
LogCat Error:
05-17 17:43:51.571: E/AndroidRuntime(6588): FATAL EXCEPTION: main
05-17 17:43:51.571: E/AndroidRuntime(6588): java.lang.NullPointerException
05-17 17:43:51.571: E/AndroidRuntime(6588): at com.spamclicker.Game$2$1.run(Game.java:158)
05-17 17:43:51.571: E/AndroidRuntime(6588): at android.os.Handler.handleCallback(Handler.java:730)
05-17 17:43:51.571: E/AndroidRuntime(6588): at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 17:43:51.571: E/AndroidRuntime(6588): at android.os.Looper.loop(Looper.java:137)
05-17 17:43:51.571: E/AndroidRuntime(6588): at android.app.ActivityThread.main(ActivityThread.java:5493)
05-17 17:43:51.571: E/AndroidRuntime(6588): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 17:43:51.571: E/AndroidRuntime(6588): at java.lang.reflect.Method.invoke(Method.java:525)
05-17 17:43:51.571: E/AndroidRuntime(6588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
05-17 17:43:51.571: E/AndroidRuntime(6588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
05-17 17:43:51.571: E/AndroidRuntime(6588): at dalvik.system.NativeStart.main(Native Method)
I am facing error on setContentView(R.layout.home); this line.As I am facing this issue first time not having idea how to solve it .It seems it is desiging regard problem but dont know what it is
Here is my code
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockActivity;
public class DialerActivity extends SherlockActivity{
TextView number;
Button call;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.e("Dialer activity", "Dilaer Activity");
**setContentView(R.layout.home);**
number = (TextView)findViewById(R.id.displayNumber);
call = ((Button)findViewById(R.id.buttonDial));
}
}
here is xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<com.android.phone91.DialPadView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ffffff" />
<Button
android:id="#+id/buttonDial"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="20dp"
android:text="Call"
android:textColor="#android:color/white"
android:background="#drawable/button_background"
android:gravity="center" >
</Button>
</LinearLayout>
</ScrollView>
dialpadview:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DialPadView extends LinearLayout implements OnClickListener {
LinearLayout buttons[] = new LinearLayout[12];
Button clearNumber;
TextView display;
Vibrator vibe;
int text_length;
MediaPlayer mp;
MainActivity hactivity;
public DialPadView(Context context, AttributeSet attrs) {
super(context, attrs);
hactivity = (MainActivity) context;
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) hactivity.getBaseContext().getSystemService(infService);
li.inflate(R.layout.dialpad, this, true);
vibe = (Vibrator) hactivity.getBaseContext().getSystemService(Context.VIBRATOR_SERVICE);
this.display = (TextView) findViewById(R.id.displayNumber);
this.buttons[0] = (LinearLayout) findViewById(R.id.button0);
this.buttons[1] = (LinearLayout) findViewById(R.id.button1);
this.buttons[2] = (LinearLayout) findViewById(R.id.button2);
this.buttons[3] = (LinearLayout) findViewById(R.id.button3);
this.buttons[4] = (LinearLayout) findViewById(R.id.button4);
this.buttons[5] = (LinearLayout) findViewById(R.id.button5);
this.buttons[6] = (LinearLayout) findViewById(R.id.button6);
this.buttons[7] = (LinearLayout) findViewById(R.id.button7);
this.buttons[8] = (LinearLayout) findViewById(R.id.button8);
this.buttons[9] = (LinearLayout) findViewById(R.id.button9);
this.buttons[10] = (LinearLayout) findViewById(R.id.buttonPlus);
this.buttons[11] = (LinearLayout) findViewById(R.id.buttonPound);
for (LinearLayout button: this.buttons) {
button.setOnClickListener(this);
}
this.clearNumber = (Button) findViewById(R.id.buttonClearNumber);
this.clearNumber.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
text_length = display.length();
if (text_length > 0) {
//trim the last character off
String num = display.getText().toString();
display.setText(num.substring(0, num.length() - 1));
}
if (!((text_length - 1) > 0))
clearNumber.setVisibility(View.GONE);
}
});
this.clearNumber.setOnLongClickListener(new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
display.setText("");
clearNumber.setVisibility(View.GONE);
return true;
}
});
}
public void onClick(View v) {
//Clear display if a message is displayed before adding a number
vibe.vibrate(50);
Pattern pattern = Pattern.compile("[0-9*#]");
Matcher matcher = pattern.matcher(this.display.getText().toString());
if (!matcher.find()) {
this.display.setText("");
}
if (!(text_length > 15))
switch (v.getId()) {
case R.id.button0:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_0);
mp.start();
this.display.append("0");
break;
case R.id.button1:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_1);
mp.start();
this.display.append("1");
break;
case R.id.button2:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_2);
mp.start();
this.display.append("2");
break;
case R.id.button3:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_3);
mp.start();
this.display.append("3");
break;
case R.id.button4:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_4);
mp.start();
this.display.append("4");
break;
case R.id.button5:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_5);
mp.start();
this.display.append("5");
break;
case R.id.button6:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_6);
mp.start();
this.display.append("6");
break;
case R.id.button7:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_7);
mp.start();
this.display.append("7");
break;
case R.id.button8:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_8);
mp.start();
this.display.append("8");
break;
case R.id.button9:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_9);
mp.start();
this.display.append("9");
break;
case R.id.buttonPlus:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_star);
mp.start();
this.display.append("+");
break;
case R.id.buttonPound:
if (mp != null && mp.isPlaying())
mp.stop();
mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_hash);
mp.start();
this.display.append("#");
break;
}
text_length = display.getText().length();
if (text_length > 0)
clearNumber.setVisibility(View.VISIBLE);
}
}
Here is Logcat:
12-12 16:56:23.921: E/AndroidRuntime(25644): FATAL EXCEPTION: main
12-12 16:56:23.921: E/AndroidRuntime(25644): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.phone91/com.android.phone91.DialerActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.android.phone91.DialPadView
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.app.ActivityThread.access$600(ActivityThread.java:151)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.os.Looper.loop(Looper.java:155)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.app.ActivityThread.main(ActivityThread.java:5520)
12-12 16:56:23.921: E/AndroidRuntime(25644): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 16:56:23.921: E/AndroidRuntime(25644): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 16:56:23.921: E/AndroidRuntime(25644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
12-12 16:56:23.921: E/AndroidRuntime(25644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
12-12 16:56:23.921: E/AndroidRuntime(25644): at dalvik.system.NativeStart.main(Native Method)
12-12 16:56:23.921: E/AndroidRuntime(25644): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.android.phone91.DialPadView
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-12 16:56:23.921: E/AndroidRuntime(25644): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:324)
12-12 16:56:23.921: E/AndroidRuntime(25644): at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:119)
12-12 16:56:23.921: E/AndroidRuntime(25644): at com.actionbarsherlock.app.SherlockActivity.setContentView(SherlockActivity.java:218)
12-12 16:56:23.921: E/AndroidRuntime(25644): at com.android.phone91.DialerActivity.onCreate(DialerActivity.java:24)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.app.Activity.performCreate(Activity.java:5066)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
12-12 16:56:23.921: E/AndroidRuntime(25644): ... 11 more
12-12 16:56:23.921: E/AndroidRuntime(25644): Caused by: java.lang.reflect.InvocationTargetException
12-12 16:56:23.921: E/AndroidRuntime(25644): at java.lang.reflect.Constructor.constructNative(Native Method)
12-12 16:56:23.921: E/AndroidRuntime(25644): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
12-12 16:56:23.921: E/AndroidRuntime(25644): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
12-12 16:56:23.921: E/AndroidRuntime(25644): ... 24 more
12-12 16:56:23.921: E/AndroidRuntime(25644): Caused by: java.lang.ClassCastException: com.android.phone91.DialerActivity cannot be cast to com.android.phone91.MainActivity
12-12 16:56:23.921: E/AndroidRuntime(25644): at com.android.phone91.DialPadView.<init>(DialPadView.java:28)
12-12 16:56:23.921: E/AndroidRuntime(25644): ... 27 more
hactivity = (MainActivity) context;
The context passed to your DialPadView is a DialerActivity but you attempt to cast it to MainActivity.
Generally, views should not depend on the activity they're displayed in. If you need to write code like this, step back and rethink the design. In your case, you just need a Context, not a specific Activity - all the uses of hactivity are in places where you're using it as a Context.
My app currently boots directly into a layout, and the onCreate method is used to control that view, for instance responding to button presses and the likes. I wanted however to move to a different initial layout. I initially just moved all of the original control functionality to a new method, and changed the setContentView(R.layout.main); to match the new view, this however causes a crash. After some fiddling around I found that no matter what I change within this method I get a crash, even when commenting out minor method calls.
Here is my onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(new ArrayAdapter<String>(actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1, new String[] {
getString(R.string.lanc),
getString(R.string.lanb),
getString(R.string.lana),
}), this);
final Intent intent = new Intent("com.google.zxing.client.android.SCAN");
latituteField = (TextView) findViewById(R.id.latitude);
longitudeField = (TextView) findViewById(R.id.longtitude);
orgText = (TextView) findViewById(R.id.orgText);
accpointnameText = (TextView) findViewById(R.id.accpointnameText);
floorText = (TextView) findViewById(R.id.floorText);
setlocation();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("attempting get from input fields");
accpointText = (EditText) findViewById(R.id.accpointText);
passwordText = (EditText) findViewById(R.id.passwordText);
IDin = accpointText.getText().toString();
System.out.println(IDin);
Passwordin = passwordText.getText().toString();
System.out.println(Passwordin);
System.out.println("so far");
JSONstate = false;
new JSONDownloader().execute("https://apps.taskpixie.com/arSettings/?id="+IDin+"&password="+Passwordin+"");
}
});
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivityForResult(intent, 0);
}
});
}
Why does my app crash no matter what I remove? How can I change my initial layout?
If you need any more data I can provide it, thanks in advance.
For example, commenting out setlocation(); causes the log cat to spit out
08-31 14:15:47.120: E/AndroidRuntime(12326): FATAL EXCEPTION: main
08-31 14:15:47.120: E/AndroidRuntime(12326): java.lang.RuntimeException: Unable to resume activity {com.example.bilisattendancerecorder/com.example.bilisattendancerecorder.Main}: java.lang.NullPointerException
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.os.Looper.loop(Looper.java:137)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-31 14:15:47.120: E/AndroidRuntime(12326): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 14:15:47.120: E/AndroidRuntime(12326): at java.lang.reflect.Method.invoke(Method.java:525)
08-31 14:15:47.120: E/AndroidRuntime(12326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-31 14:15:47.120: E/AndroidRuntime(12326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-31 14:15:47.120: E/AndroidRuntime(12326): at dalvik.system.NativeStart.main(Native Method)
08-31 14:15:47.120: E/AndroidRuntime(12326): Caused by: java.lang.NullPointerException
08-31 14:15:47.120: E/AndroidRuntime(12326): at com.example.bilisattendancerecorder.Main.onResume(Main.java:299)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.Activity.performResume(Activity.java:5211)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
I would like to stress here that changing something so minute should not cause such drastic errors, and that no matter what I take away from this method, I receive errors.