Unable to set data from sqlite to Listview - java

this code works
public class listActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
testAdapter mDbHelper = new testAdapter(this);
mDbHelper.open();
Cursor c = mDbHelper.getAllData();
String[] from = new String[] { "name", "title" };
CursorAdapter dataSource = new SimpleCursorAdapter(this,
R.layout.list_row, c, from, new int[] { R.id.name,
R.id.title});
setListAdapter(dataSource);
mDbHelper.close();
}
But when I try to set the adapter to a listview in one of my layout it just crashes
public class listActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
testAdapter mDbHelper = new testAdapter(this);
mDbHelper.open();
Cursor c = mDbHelper.getAllData();
String[] from = new String[] { "name", "title" };
CursorAdapter dataSource = new SimpleCursorAdapter(this,
R.layout.list_row, c, from, new int[] { R.id.name,
R.id.title});
ListView itemList = (ListView)findViewById(R.id.itemsList);
itemList.setAdapter(dataSource); // line 29 where it crashes
mDbHelper.close();
}
and this is what LogCat throws out
04-13 20:58:09.331: E/DataAdapter(929): opened db
04-13 20:58:09.451: D/AndroidRuntime(929): Shutting down VM
04-13 20:58:09.451: W/dalvikvm(929): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-13 20:58:09.521: E/AndroidRuntime(929): FATAL EXCEPTION: main
04-13 20:58:09.521: E/AndroidRuntime(929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.listActivity}: java.lang.NullPointerException
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.os.Looper.loop(Looper.java:123)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-13 20:58:09.521: E/AndroidRuntime(929): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:58:09.521: E/AndroidRuntime(929): at java.lang.reflect.Method.invoke(Method.java:507)
04-13 20:58:09.521: E/AndroidRuntime(929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 20:58:09.521: E/AndroidRuntime(929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-13 20:58:09.521: E/AndroidRuntime(929): at dalvik.system.NativeStart.main(Native Method)
04-13 20:58:09.521: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException
04-13 20:58:09.521: E/AndroidRuntime(929): at com.exmaple.app.listActivity.onCreate(listActivity.java:29)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-13 20:58:09.521: E/AndroidRuntime(929): ... 11 more
I´ve been strugling with this for weeks, so please any help will be appreciated.

You haven't called setContentView(), the code can't find your R.id.itemsList view. itemsList is null.

Related

android app parse values through intent

I am trying to parse a value through intent while switching between activities.
I know I should read values from last intent with getExtra but I don't know why it doesn't work.
Also when I switch between activities on button click, application crashes.
In activity main I read text from editText and put it in Intent:
public void schimba(View view){
int value = Integer.parseInt(instances.getText().toString());;
Intent intent = new Intent(this, Tabel.class);
intent.putExtra("max", value);
startActivity(intent);
}
When it switch to activity 2 I have this:
Intent intentObject = getIntent();
int value;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
value = intentObject.getIntExtra("max", 0);
/*
for(i=0;i<=value;i++)
{
LayoutInflater layoutinflate = null;
layoutinflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowview = layoutinflate.inflate( R.layout.activity_tabel, null);
}
*/
setContentView(R.layout.activity_tabel);
TextView showvalue;
showvalue = (TextView) findViewById(R.id.ShowValue);
showvalue.setText(""+value);
The idea is that I want to use this value in a for loop, I already know how to display the value in a textView but I don't need it, I wanna use it in for.
Logcat:
04-23 10:40:52.550: E/AndroidRuntime(1010): FATAL EXCEPTION: main
04-23 10:40:52.550: E/AndroidRuntime(1010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.instances_temperature/com.example.instances_temperature.Tabel}: java.lang.NullPointerException
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.os.Looper.loop(Looper.java:123)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-23 10:40:52.550: E/AndroidRuntime(1010): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 10:40:52.550: E/AndroidRuntime(1010): at java.lang.reflect.Method.invoke(Method.java:521)
04-23 10:40:52.550: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-23 10:40:52.550: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-23 10:40:52.550: E/AndroidRuntime(1010): at dalvik.system.NativeStart.main(Native Method)
04-23 10:40:52.550: E/AndroidRuntime(1010): Caused by: java.lang.NullPointerException
04-23 10:40:52.550: E/AndroidRuntime(1010): at com.example.instances_temperature.Tabel.onCreate(Tabel.java:26)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-23 10:40:52.550: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-23 10:40:52.550: E/AndroidRuntime(1010): ... 11 more
line 26 would be this:
value = intentObject.getIntExtra("max", 0);
You have to use the code below
int maxValue = getIntent().getExtras().getInt("max");
inside onCreate().
Hope it will solve your problem..
You have not declared intentObject...
Use this:
value = getIntent().getIntExtra("max", 0);
use this
value = getIntent().getStringExtra("max");
instead of this
value = intentObject.getIntExtra("max", 0);

First program causing loads of errors

I just started learning Android programming and have run into trouble. I'm using the book "Android Programming The Big Nerd Ranch Guide". My IDE is Eclipse and Genymotion to emulate the app. Here's the logcat:
04-13 00:19:48.065: D/dalvikvm(1256): Late-enabling CheckJNI
04-13 00:19:48.781: D/AndroidRuntime(1256): Shutting down VM
04-13 00:19:48.785: W/dalvikvm(1256): threadid=1: thread exiting with uncaught exception (group=0xa4d8ab20)
04-13 00:19:48.785: E/AndroidRuntime(1256): FATAL EXCEPTION: main
04-13 00:19:48.785: E/AndroidRuntime(1256): Process: com.bignerdranch.android.geoquiz, PID: 1256
04-13 00:19:48.785: E/AndroidRuntime(1256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.NullPointerException
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.os.Looper.loop(Looper.java:136)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-13 00:19:48.785: E/AndroidRuntime(1256): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 00:19:48.785: E/AndroidRuntime(1256): at java.lang.reflect.Method.invoke(Method.java:515)
04-13 00:19:48.785: E/AndroidRuntime(1256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-13 00:19:48.785: E/AndroidRuntime(1256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-13 00:19:48.785: E/AndroidRuntime(1256): at dalvik.system.NativeStart.main(Native Method)
04-13 00:19:48.785: E/AndroidRuntime(1256): Caused by: java.lang.NullPointerException
04-13 00:19:48.785: E/AndroidRuntime(1256): at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:30)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.Activity.performCreate(Activity.java:5231)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-13 00:19:48.785: E/AndroidRuntime(1256): ... 11 more
04-13 00:19:56.345: I/Process(1256): Sending signal. PID: 1256 SIG: 9
Here's the fragment_quiz.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="#string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button" />
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button" />
</LinearLayout>
</LinearLayout>
The QuizActivity.java file (most of the code imported by Eclipse):
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT)
.show();
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT)
.show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_quiz, container,
false);
return rootView;
}
}
}
What am I doing wrong?
Put your layout definitions in the PlaceholderFragment class. For the findViewById, use rootView.findViewById. For the context, use getActivity().
Why does this work? There are actually 2 independent layouts shown as one in your screen. The master one shows everything, including a fragment. A Fragment is basically a self-contained mini activity, very useful in Tablet development, and mildly useful even just for a phone type device. Your layouts are in the fragment_quiz xml file, which is only inflated inside the fragment, in the onCreateView statement. So long as you use the findViewById after it's been inflated, you'll be fine.

Android App Crashes on Button Click

I have been attempting to make my first android application (a simple temperature converter) in Eclipse, but when I click the button on my phone the app crashes. Here is the full java code
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
String number;
int number2;
int output;
boolean F;
public void onBtnClicked(View view){
EditText mEdit = (EditText)findViewById(R.id.editText1);
TextView myTextView = (TextView) findViewById(R.id.label);
number = mEdit.getText().toString();
number2 = Integer.parseInt(number);
if(F=true){
output=number2*9/5+32;
}
else{
output=number2-32*5/9;
}
myTextView.setText(output);
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio0:
if (checked)
F = true;
break;
case R.id.radio1:
if (checked)
F = false;
break;
}
}
}
LogCat when the button is clicked
04-13 20:19:50.423: E/AndroidRuntime(25200): FATAL EXCEPTION: main
04-13 20:19:50.423: E/AndroidRuntime(25200): java.lang.IllegalStateException: Could not execute method of the activity
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.view.View$1.onClick(View.java:3674)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.view.View.performClick(View.java:4198)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.view.View$PerformClick.run(View.java:17158)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.os.Handler.handleCallback(Handler.java:615)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.os.Looper.loop(Looper.java:137)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.app.ActivityThread.main(ActivityThread.java:4918)
04-13 20:19:50.423: E/AndroidRuntime(25200): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 20:19:50.423: E/AndroidRuntime(25200): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-13 20:19:50.423: E/AndroidRuntime(25200): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-13 20:19:50.423: E/AndroidRuntime(25200): at dalvik.system.NativeStart.main(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200): Caused by: java.lang.reflect.InvocationTargetException
04-13 20:19:50.423: E/AndroidRuntime(25200): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.view.View$1.onClick(View.java:3669)
04-13 20:19:50.423: E/AndroidRuntime(25200): ... 11 more
04-13 20:19:50.423: E/AndroidRuntime(25200): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x59
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.content.res.Resources.getText(Resources.java:242)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.widget.TextView.setText(TextView.java:3773)
04-13 20:19:50.423: E/AndroidRuntime(25200): at com.example.myfirstapp.MainActivity.onBtnClicked(MainActivity.java:43)
04-13 20:19:50.423: E/AndroidRuntime(25200): ... 14 more
04-13 20:19:50.453: E/android.os.Debug(718): !#Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
And lastly for the xml of the button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioGroup1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:onClick="onBtnClicked"
android:text="Calculate" />
I'm not sure how to go about fixing this, so hopefully someone can help.
Thanks.
Initialize your Buttons first then set onclicklistener to them
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize views here
EditText mEdit = (EditText) findViewById(R.id.editText1);
TextView myTextView = (TextView) findViewById(R.id.label);
Button yourButton = (Button) findViewByid(R.id.youridforbutton);
//set onclicklistener for your button
yourbutton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
number = mEdit.getText().toString();
number2 = Integer.parseInt(number);
if (F = true) {
output = number2 * 9 / 5 + 32;
} else {
output = number2 - 32 * 5 / 9;
}
myTextView.setText("" + output);
}
});
}
Similarly set the other button also
You need to cast output to String
myTextView.setText(String.valueOf(output));
setText method is overloaded and when You pass an integer to it it expects it to be an id of resource.
You can not set Integers to TextViews. You have to convert them into Strings.
myTextView.setText(String.valueOf(output));
You need to tell the click listener to listen to a particular button. So, in other words set an OnItemClickListener on the button. Something as follows:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do the "on click" action here
}
});
Hope this helps. If not, then please comment with further issue.

NullPointerException on TabGroupactivity when setting button typeface

I'm having a little problem with an activity I have loading in a TabGroupActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_profile);
f = new Functions();
String j = f.getUserInfo();
arialFont = Typeface.createFromAsset(this.getAssets(),"Arial.ttf");
username = (TextView)findViewById(R.id.txtProfileName);
firstname = (TextView)findViewById(R.id.txtName);
Address1 = (TextView)findViewById(R.id.txtAdd1);
Cit = (TextView)findViewById(R.id.txtCity);
PostCode = (TextView)findViewById(R.id.txtPost);
Coun = (TextView)findViewById(R.id.txtCountry);
Phon = (TextView)findViewById(R.id.txtPhone);
dob = (TextView)findViewById(R.id.txtDOB);
gender = (TextView)findViewById(R.id.txtGender);
yourDetails = (Button)findViewById(R.id.Button01);
payment = (Button)findViewById(R.id.Button02);
billing = (Button)findViewById(R.id.Button03);
history = (Button)findViewById(R.id.Button04);
yourDetails.setTypeface(arialFont); <-- Null Pointer exception starts here
payment.setTypeface(arialFont);
billing.setTypeface(arialFont);
history.setTypeface(arialFont);
try {
parseData(j);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The error I recieve from the code is this:
02-27 13:22:47.278: E/AndroidRuntime(929): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Ticketline.Ticketline/com.Ticketline.Ticketline.UserProfile}: java.lang.NullPointerException
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1796)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
02-27 13:22:47.278: E/AndroidRuntime(929): at com.Ticketline.Ticketline.TabGroupActivity.startChildActivity(TabGroupActivity.java:61)
02-27 13:22:47.278: E/AndroidRuntime(929): at com.Ticketline.Ticketline.Account.onCreate(Account.java:33)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.Activity.performCreate(Activity.java:4465)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-27 13:22:47.278: E/AndroidRuntime(929): ... 18 more
02-27 13:22:47.278: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException
02-27 13:22:47.278: E/AndroidRuntime(929): at com.Ticketline.Ticketline.UserProfile.onCreate(UserProfile.java:83)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.Activity.performCreate(Activity.java:4465)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-27 13:22:47.278: E/AndroidRuntime(929): ... 26 more
I know the issue is with setting the buttons typeface to Arial, the problem is I don't understand why this is an issue. If I comment the setTypeface lines out it works fine and will move onto other activity which do have their button typefaces set within the TabGroupActivity which perplexes me even more.
Top marks for anyone that can explain this to me
Figured the issue out, the coder who worked on this app before me gave the history button the id "btnHistory" instead of "Button04". Naturally this resolved in a null pointer error because the id "Button04" doesn't exist in that activity.

using startActivityForResult

i have made 2 classes YehActivity.java and h.java. On running the application i am getting an error ,Application has stopped unexpectedly.Here is the code
public class YehActivity extends Activity {
public static final int r=1;
Button b;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==r && resultCode==RESULT_OK){
String h=data.getStringExtra("a");
tv.setText(h);
}
}
}
where to check for null.
this is the second file
public class he extends Activity{
Button b;
EditText et;
Intent i=getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.h);
b=(Button) findViewById(R.id.button12);
et=(EditText) findViewById(R.id.editText1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String value=et.getText().toString().trim();
i.putExtra("a", value);
he.this.setResult(RESULT_OK, i);
finish();
}
});
}
}
and the log file is
02-11 23:31:46.408: I/Process(302): Sending signal. PID: 302 SIG: 9
02-11 23:45:04.778: D/AndroidRuntime(357): Shutting down VM
02-11 23:45:04.778: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:04.798: E/AndroidRuntime(357): FATAL EXCEPTION: main
02-11 23:45:04.798: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:04.798: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:04.798: E/AndroidRuntime(357): ... 11 more
02-11 23:45:11.158: I/Process(357): Sending signal. PID: 357 SIG: 9
02-11 23:45:22.708: D/AndroidRuntime(374): Shutting down VM
02-11 23:45:22.708: W/dalvikvm(374): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:22.728: E/AndroidRuntime(374): FATAL EXCEPTION: main
02-11 23:45:22.728: E/AndroidRuntime(374): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:22.728: E/AndroidRuntime(374): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): Caused by: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:22.728: E/AndroidRuntime(374): ... 11 more
02-11 23:45:25.497: I/Process(374): Sending signal. PID: 374 SIG: 9
In your
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
onCreate() method you attempt to use b, but you never initialize it (I'm assuming its declared as a global variable). This means that you will run into a NullPointerException when you try to call setOnClickListener().
In your code in OnCreate() you have to declare b as button and then apply listener to that.
b=(Button) findViewById(R.id.button12);
Also check your data is null or not. If it is null than handle it properly.
Then your code runs fine.

Categories