I've been following the Android tutorials and created MyFirstApp (see http://developer.android.com/training/basics/firstapp/index.html) and I can launch the app ok on the emulator, but upon entering a message and hitting "send" I get an IllegalStateException caused by a NullPointerException at this line in the below code:
String message = editText.getText().toString();
Upon debugging, it is clear that editText is null at this point, but I cannot see why, or anything I've missed in the tutorial.
My MainActivity class:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
/**
* <p>Main Activity class for the App</p>
*
* #author - thebloodguy
*/
public class MainActivity extends Activity {
//~ ----------------------------------------------------------------------------------------------------------------
//~ Static fields/initializers
//~ ----------------------------------------------------------------------------------------------------------------
/** Key to the extra message data in the sendMessage intent */
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
//~ ----------------------------------------------------------------------------------------------------------------
//~ Methods
//~ ----------------------------------------------------------------------------------------------------------------
/**
* {#inheritDoc}
*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/* Inflate the menu; this adds items to the action bar if it is present */
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* <p>Send a message using the data in the {#link View}</p>
*
* #param view - the {#link View} object representing the state of the view when the message is sent
*/
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) view.findViewById(R.id.edit_message);
String message = editText.getText().toString(); // This is the guilty line
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/**
* {#inheritDoc}
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
My activity_main.xml:
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
and the LogCat output:
12-21 10:02:32.210: E/Trace(621): error opening trace file: No such file or directory (2)
12-21 10:02:32.250: W/ActivityThread(621): Application com.example.myfirstapp is waiting for the debugger on port 8100...
12-21 10:02:32.270: I/System.out(621): Sending WAIT chunk
12-21 10:02:32.280: I/dalvikvm(621): Debugger is active
12-21 10:02:32.480: I/System.out(621): Debugger has connected
12-21 10:02:32.480: I/System.out(621): waiting for debugger to settle...
12-21 10:02:32.680: I/System.out(621): waiting for debugger to settle...
12-21 10:02:32.889: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.091: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.290: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.534: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.796: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.990: I/System.out(621): waiting for debugger to settle...
12-21 10:02:34.204: I/System.out(621): debugger has settled (1353)
12-21 10:02:35.429: D/gralloc_goldfish(621): Emulator without GPU emulation detected.
12-21 10:05:11.510: I/Choreographer(621): Skipped 94 frames! The application may be doing too much work on its main thread.
12-21 10:05:13.850: I/Choreographer(621): Skipped 35 frames! The application may be doing too much work on its main thread.
12-21 10:05:15.571: I/Choreographer(621): Skipped 38 frames! The application may be doing too much work on its main thread.
12-21 10:06:53.724: D/AndroidRuntime(621): Shutting down VM
12-21 10:06:53.724: W/dalvikvm(621): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-21 10:06:53.840: E/AndroidRuntime(621): FATAL EXCEPTION: main
12-21 10:06:53.840: E/AndroidRuntime(621): java.lang.IllegalStateException: Could not execute method of the activity
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$1.onClick(View.java:3591)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View.performClick(View.java:4084)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$PerformClick.run(View.java:16966)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Handler.handleCallback(Handler.java:615)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Looper.loop(Looper.java:137)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:06:53.840: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-21 10:06:53.840: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-21 10:06:53.840: E/AndroidRuntime(621): at dalvik.system.NativeStart.main(Native Method)
12-21 10:06:53.840: E/AndroidRuntime(621): Caused by: java.lang.reflect.InvocationTargetException
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$1.onClick(View.java:3586)
12-21 10:06:53.840: E/AndroidRuntime(621): ... 11 more
12-21 10:06:53.840: E/AndroidRuntime(621): Caused by: java.lang.NullPointerException
12-21 10:06:53.840: E/AndroidRuntime(621): at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:61)
12-21 10:06:53.840: E/AndroidRuntime(621): ... 14 more
Change this line
EditText editText = (EditText) view.findViewById(R.id.edit_message);
to
EditText editText = (EditText)findViewById(R.id.edit_message);
here in sendMessage method, view parameter is the Button being clicked, and EditText is not child of this view, so you need to call findViewById from the activity content view, which you call by
EditText editText = (EditText) findViewById(R.id.edit_message);
Related
So I have 3 Activities (LauncherActivity>MenuAvtivity>SelectionActivity)
When I press the back button to go back from the SelectionActivity to the MenuActivity my app chrashes.
Code: Intent intent = new Intent(MenuActivity.this,SelectionActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Error Code:
12-21 20:37:52.179: E/AndroidRuntime(14823): FATAL EXCEPTION: main
12-21 20:37:52.179: E/AndroidRuntime(14823): Process: com.example.myapp, PID: 14823
12-21 20:37:52.179: E/AndroidRuntime(14823): java.lang.IllegalThreadStateException: Thread already started
12-21 20:37:52.179: E/AndroidRuntime(14823): at java.lang.Thread.checkNotStarted(Thread.java:871)
12-21 20:37:52.179: E/AndroidRuntime(14823): at java.lang.Thread.start(Thread.java:1025)
12-21 20:37:52.179: E/AndroidRuntime(14823): at com.example.myapp.MenuView$1.surfaceCreated(MenuView.java:51)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.SurfaceView.updateWindow(SurfaceView.java:662)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:256)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.View.dispatchWindowVisibilityChanged(View.java:8096)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1448)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.Choreographer.doFrame(Choreographer.java:560)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.os.Handler.handleCallback(Handler.java:808)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.os.Handler.dispatchMessage(Handler.java:103)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.os.Looper.loop(Looper.java:193)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.app.ActivityThread.main(ActivityThread.java:5292)
12-21 20:37:52.179: E/AndroidRuntime(14823): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 20:37:52.179: E/AndroidRuntime(14823): at java.lang.reflect.Method.invoke(Method.java:515)
12-21 20:37:52.179: E/AndroidRuntime(14823): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
12-21 20:37:52.179: E/AndroidRuntime(14823): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
12-21 20:37:52.179: E/AndroidRuntime(14823): at dalvik.system.NativeStart.main(Native Method)
The LauncherActivity is using a SurfaceView and the MenuActivity a SurfaceView with a Thread drawing on a Canvas.
Thread Class:
#Override
public void run() {
while (running) {
Canvas c = null;
try {
c = menuView.getHolder().lockCanvas();
synchronized (menuView.getHolder()) {
menuView.doDraw(c);
}
} finally {
if (c != null) {
menuView.getHolder().unlockCanvasAndPost(c);
}
}
}
}
.IllegalThreadStateException: Thread already started
So in your surfaceCreated you're calling thread.start. However that thread was already started previously. So either don't start it again, or create a new Thread. My guess is the first one is the right answer, but without code I can't tell for sure.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
It say there's error in line 71 but i could figure what error was it any kind souls willing to help me ? THANKS
my problem is that when i click on the foodbutton on the mainactivity , the system crashes. BUT the logcat shows the error in food.java which is another page.
Here's the coding !
package com.yiqiexample.cabincrew;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
//import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Food extends ListActivity
implements OnClickListener {
// Progress Dialog
private ProgressDialog pDialog;
//testing on Emulator:
private static final String READ_COMMENTS_URL = "http://10.0.2.2/pbda2/foodordered.php";
// private CheckBox chkFood, chkDrinks, chkServices;
//private Button btnDisplay, chkClear, deliever, chkClearFood, fooddeliever, drinksdeliever, servicesdeliever, chkClearDrinks, chkClearServices;
//private TextView clearThis,orderdisplay, clearThisFood, foodorderdisplay, drinksorderdisplay, servicesorderdisplay, clearThisDrinks, clearThisServices;
private static final String TAG_SUCCESS = "success";
private static final String TAG_POSTS = "posts";
private static final String TAG_SEATNUMBER = "seatnumber";
private static final String TAG_FOODORDERED = "foodordered";
//it's important to note that the message is both in the parent branch of
//our JSON tree that displays a "Post Available" or a "No Post Available" message,
//and there is also a message for each individual post, listed under the "posts"
//category, that displays what the user typed as their message.
//An array of all of our comments
private JSONArray mComments = null;
//manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.food);
View v = findViewById(R.id.backmain);
//set event listener
v.setOnClickListener(this);
View z = findViewById(R.id.drinksbtn);
//set event listener
z.setOnClickListener(this);
View x = findViewById(R.id.servicebtn);
//set event listener
x.setOnClickListener(this);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//loading the comments via AsyncTask
new LoadComments().execute();
}
/**
* Retrieves recent post data from the server.
*/
public void updateJSONdata() {
// Instantiate the arraylist to contain all the JSON data.
// we are going to use a bunch of key-value pairs, referring
// to the json element name, and the content, for example,
// message it the tag, and "I'm awesome" as the content..
mCommentList = new ArrayList<HashMap<String, String>>();
// Bro, it's time to power up the J parser
JSONParser jParser = new JSONParser();
// Feed the beast our comments url, and it spits us
//back a JSON object. Boo-yeah Jerome.
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
//when parsing JSON stuff, we should probably
//try to catch any exceptions:
try {
//I know I said we would check if "Posts were Avail." (success==1)
//before we tried to read the individual posts, but I lied...
//mComments will tell us how many "posts" or comments are
//available
mComments = json.getJSONArray(TAG_POSTS);
// looping through all posts according to the json object returned
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
//gets the content of each tag
String seatnumber = c.getString(TAG_SEATNUMBER);
String foodordered = c.getString(TAG_FOODORDERED);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_SEATNUMBER, seatnumber);
map.put(TAG_FOODORDERED, foodordered);
// adding HashList to ArrayList
mCommentList.add(map);
//annndddd, our JSON data is up to date same with our array list
}
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.backmain){
//define a new Intent for the second Activity
Intent intent = new Intent(this,MainActivity.class);
//start the second Activity
this.startActivity(intent);
}
if(arg0.getId() == R.id.drinksbtn){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Drinks.class);
//start the second Activity
this.startActivity(intent);
}
if(arg0.getId() == R.id.servicebtn){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Services.class);
//start the second Activity
this.startActivity(intent);
}
}
/**
* Inserts the parsed data into the listview.
*/
private void updateList() {
// For a ListActivity we need to set the List Adapter, and in order to do
//that, we need to create a ListAdapter. This SimpleAdapter,
//will utilize our updated Hashmapped ArrayList,
//use our single_post xml template for each item in our list,
//and place the appropriate info from the list to the
//correct GUI id. Order is important here.
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_SEATNUMBER, TAG_FOODORDERED
//TAG_DRINKSORDERED, TAG_SERVICES
}, new int[] { R.id.seatnumber, R.id.orders
//R.id.drinkstv, R.id.servicestv,
});
// I shouldn't have to comment on this one:
setListAdapter(adapter);
// Optional: when the user clicks a list item we
//could do something. However, we will choose
//to do nothing...
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// This method is triggered if an item is click within our
// list. For our example we won't be using this, but
// it is useful to know in real life applications.
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Food.this);
pDialog.setMessage("Loading orders...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected Boolean doInBackground(Void... arg0) {
//we will develop this method in version 2
updateJSONdata();
return null;
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
//we will develop this method in version 2
updateList();
}
}
}
Here's the coding for main page
package com.yiqiexample.cabincrew;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity
implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View v = findViewById(R.id.foodbutton);
//set event listener
v.setOnClickListener(this);
View x= findViewById(R.id.drinks);
//set event listener
x.setOnClickListener(this);
View y = findViewById(R.id.services);
//set event listener
y.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.foodbutton){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Food.class);
//start the second Activity
this.startActivity(intent);
}
if(arg0.getId() == R.id.drinks){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Drinks.class);
//start the second Activity
this.startActivity(intent);
}
if(arg0.getId() == R.id.services){
//define a new Intent for the second Activity
Intent intent = new Intent(this,Services.class);
//start the second Activity
this.startActivity(intent);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
food.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android1:id="#+id/bg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android1:background="#E0FFFF" >
<Button
android1:id="#+id/backmain"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_alignParentBottom="true"
android1:layout_alignParentRight="true"
android1:text="#string/backtomain" />
<Button
android1:id="#+id/servicesdelivered"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_above="#+id/foodbtn"
android1:layout_alignLeft="#+id/backmain"
android1:text="#string/servicesdelivered"
android1:visibility="invisible" />
<Button
android:id="#+id/servicebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="358dp"
android:layout_toRightOf="#+id/foodbutton"
android:text="SERVICES" />
<Button
android1:id="#+id/foodbtn"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_alignParentBottom="true"
android1:layout_marginBottom="23dp"
android1:layout_marginRight="24dp"
android1:text="DRINKS ORDERS" />
<LinearLayout
android:id="#+id/top_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android1:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/foodtitle"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top_layover"
android1:layout_alignBottom="#+id/servicesdelivered"
android:background="#fff"
android:divider="#android:color/transparent"
android:scrollbars="none" />
<LinearLayout
android:id="#+id/bottom_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here's the LOGCAT !
11-03 04:23:36.974: W/dalvikvm(756): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-03 04:23:37.093: E/AndroidRuntime(756): FATAL EXCEPTION: main
11-03 04:23:37.093: E/AndroidRuntime(756): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yiqiexample.cabincrew/com.yiqiexample.cabincrew.Food}: java.lang.NullPointerException
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.os.Looper.loop(Looper.java:137)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-03 04:23:37.093: E/AndroidRuntime(756): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 04:23:37.093: E/AndroidRuntime(756): at java.lang.reflect.Method.invoke(Method.java:525)
11-03 04:23:37.093: E/AndroidRuntime(756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-03 04:23:37.093: E/AndroidRuntime(756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 04:23:37.093: E/AndroidRuntime(756): at dalvik.system.NativeStart.main(Native Method)
11-03 04:23:37.093: E/AndroidRuntime(756): Caused by: java.lang.NullPointerException
11-03 04:23:37.093: E/AndroidRuntime(756): at com.yiqiexample.cabincrew.Food.onCreate(Food.java:71)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.Activity.performCreate(Activity.java:5133)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-03 04:23:37.093: E/AndroidRuntime(756): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-03 04:23:37.093: E/AndroidRuntime(756): ... 11 more
11-03 04:23:43.713: I/Process(756): Sending signal. PID: 756 SIG: 9
11-03 04:23:46.715: D/dalvikvm(895): GC_FOR_ALLOC freed 44K, 4% free 2600K/2708K, paused 57ms, total 60ms
11-03 04:23:46.753: I/dalvikvm-heap(895): Grow heap (frag case) to 4.941MB for 2457616-byte allocation
11-03 04:23:46.893: D/dalvikvm(895): GC_FOR_ALLOC freed 2K, 3% free 4998K/5112K, paused 130ms, total 130ms
11-03 04:23:47.793: D/gralloc_goldfish(895): Emulator without GPU emulation detected.
11-03 04:31:41.842: I/Choreographer(895): Skipped 125 frames! The application may be doing too much work on its main thread.
11-03 04:31:42.284: I/Choreographer(895): Skipped 62 frames! The application may be doing too much work on its main thread.
11-03 04:31:42.913: I/Choreographer(895): Skipped 71 frames! The application may be doing too much work on its main thread.
11-03 04:32:15.008: D/dalvikvm(895): GC_FOR_ALLOC freed 143K, 4% free 5688K/5892K, paused 138ms, total 186ms
11-03 04:32:16.063: I/Choreographer(895): Skipped 91 frames! The application may be doing too much work on its main thread.
11-03 04:32:16.603: I/Choreographer(895): Skipped 58 frames! The application may be doing too much work on its main thread.
11-03 04:32:42.023: I/Choreographer(895): Skipped 46 frames! The application may be doing too much work on its main thread.
11-03 04:32:44.223: I/Choreographer(895): Skipped 30 frames! The application may be doing too much work on its main thread.
11-03 04:32:45.053: I/Choreographer(895): Skipped 122 frames! The application may be doing too much work on its main thread.
11-03 04:32:45.513: I/Choreographer(895): Skipped 68 frames! The application may be doing too much work on its main thread.
11-03 04:32:46.555: I/Choreographer(895): Skipped 135 frames! The application may be doing too much work on its main thread.
11-03 04:32:49.742: D/dalvikvm(895): GC_FOR_ALLOC freed 152K, 4% free 6327K/6544K, paused 350ms, total 378ms
11-03 04:32:50.245: D/dalvikvm(895): GC_FOR_ALLOC freed 59K, 4% free 7253K/7540K, paused 187ms, total 190ms
11-03 04:32:50.349: I/Choreographer(895): Skipped 192 frames! The application may be doing too much work on its main thread.
11-03 04:32:54.232: I/Choreographer(895): Skipped 35 frames! The application may be doing too much work on its main thread.
11-03 04:32:54.702: I/Choreographer(895): Skipped 73 frames! The application may be doing too much work on its main thread.
11-03 04:32:55.134: I/Choreographer(895): Skipped 71 frames! The application may be doing too much work on its main thread.
11-03 04:32:55.968: I/Choreographer(895): Skipped 112 frames! The application may be doing too much work on its main thread.
11-03 04:32:59.177: D/dalvikvm(895): GC_FOR_ALLOC freed 1715K, 21% free 6795K/8580K, paused 279ms, total 332ms
11-03 04:32:59.322: I/Choreographer(895): Skipped 134 frames! The application may be doing too much work on its main thread.
11-03 04:33:03.003: I/Choreographer(895): Skipped 53 frames! The application may be doing too much work on its main thread.
11-03 04:33:04.183: I/Choreographer(895): Skipped 35 frames! The application may be doing too much work on its main thread.
11-03 04:33:04.643: I/Choreographer(895): Skipped 35 frames! The application may be doing too much work on its main thread.
11-03 04:33:08.103: I/Choreographer(895): Skipped 43 frames! The application may be doing too much work on its main thread.
11-03 04:34:58.453: D/AndroidRuntime(951): Shutting down VM
11-03 04:34:58.453: W/dalvikvm(951): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-03 04:34:58.464: E/AndroidRuntime(951): FATAL EXCEPTION: main
11-03 04:34:58.464: E/AndroidRuntime(951): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.IllegalStateException: Unable to get package info for com.yiqiexample.cabincrew; is package not installed?
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.LoadedApk.makeApplication(LoadedApk.java:509)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4417)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.os.Looper.loop(Looper.java:137)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-03 04:34:58.464: E/AndroidRuntime(951): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 04:34:58.464: E/AndroidRuntime(951): at java.lang.reflect.Method.invoke(Method.java:525)
11-03 04:34:58.464: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-03 04:34:58.464: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 04:34:58.464: E/AndroidRuntime(951): at dalvik.system.NativeStart.main(Native Method)
11-03 04:34:58.464: E/AndroidRuntime(951): Caused by: java.lang.IllegalStateException: Unable to get package info for com.yiqiexample.cabincrew; is package not installed?
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:369)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
11-03 04:34:58.464: E/AndroidRuntime(951): at android.app.LoadedApk.makeApplication(LoadedApk.java:501)
11-03 04:34:58.464: E/AndroidRuntime(951): ... 11 more
11-03 04:35:10.544: D/dalvikvm(994): GC_FOR_ALLOC freed 55K, 5% free 2600K/2720K, paused 30ms, total 33ms
11-03 04:35:10.562: I/dalvikvm-heap(994): Grow heap (frag case) to 4.940MB for 2457616-byte allocation
11-03 04:35:10.673: D/dalvikvm(994): GC_FOR_ALLOC freed 2K, 3% free 4998K/5124K, paused 110ms, total 110ms
11-03 04:35:11.213: D/gralloc_goldfish(994): Emulator without GPU emulation detected.
11-03 04:40:44.032: D/AndroidRuntime(994): Shutting down VM
11-03 04:40:44.042: W/dalvikvm(994): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-03 04:40:44.112: E/AndroidRuntime(994): FATAL EXCEPTION: main
11-03 04:40:44.112: E/AndroidRuntime(994): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yiqiexample.cabincrew/com.yiqiexample.cabincrew.Food}: java.lang.NullPointerException
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.os.Looper.loop(Looper.java:137)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-03 04:40:44.112: E/AndroidRuntime(994): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 04:40:44.112: E/AndroidRuntime(994): at java.lang.reflect.Method.invoke(Method.java:525)
11-03 04:40:44.112: E/AndroidRuntime(994): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-03 04:40:44.112: E/AndroidRuntime(994): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 04:40:44.112: E/AndroidRuntime(994): at dalvik.system.NativeStart.main(Native Method)
11-03 04:40:44.112: E/AndroidRuntime(994): Caused by: java.lang.NullPointerException
11-03 04:40:44.112: E/AndroidRuntime(994): at com.yiqiexample.cabincrew.Food.onCreate(Food.java:71)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.Activity.performCreate(Activity.java:5133)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-03 04:40:44.112: E/AndroidRuntime(994): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-03 04:40:44.112: E/AndroidRuntime(994): ... 11 more
if you need more coding to spot the error please tell me ! :)
Assuming line 71 is this one.-
z.setOnClickListener(this);
this line must be setting z to null.
View z = findViewById(R.id.drinksbtn);
Double check that food layout contains a view with id drinksbtn with no typos.
Notice that there's no drinksbtn in your xml, nor foodbutton neither.
public class ListSceneAdapter extends OAListSceneAdapter {
public ListSceneAdapter(Context context) {
super(context, R.layout.listitem); // pass the custom UI layout for list items
}
#Override
protected void setupListItemView(View listItemView, OAScene scene) {
// fill the custom UI with the given scene data for the list item
((TextView)listItemView.findViewById(R.id.listItemName)).setText(scene.name);
// add listener for the button
final OAScene _scene = scene;
((Button)listItemView.findViewById(R.id.listItemButton)).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), _scene.description, Toast.LENGTH_SHORT).show();
}
});
((Button)listItemView.findViewById(R.id.listItemMap)).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
double lat = 1.559034;
double longi =103.641486;
Intent intent = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+ lat + "," + longi+ "&daddr="+_scene.getLatitude()+","+_scene.getLongitude()+""));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
//Toast.makeText(getContext(), _scene.description, Toast.LENGTH_SHORT).show();
}
});
}
}
heres the code. when i try to onClick and open new activity which give navigation from Google Map api.. it giver this error..
The method startActivity(Intent) is undefined for the type new View.OnClickListener(){}
heres the LOGCAT
12-21 11:30:10.867: D/SensorManager(4782): ====>>>>>Num Sensor: 1
12-21 11:30:10.867: D/SensorManager(4782): ====>>>>>Num Sensor: 2
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 3
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 4
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 5
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 6
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 0
12-21 11:30:10.898: I/inertialManager(4782): Rotation is: 0 or 180
12-21 11:30:10.898: I/intertialManager(4782): Natural Orientation is landscape
12-21 11:30:11.000: W/System.err(4782): java.lang.NumberFormatException: unable to parse 'main' as integer
12-21 11:30:11.015: W/System.err(4782): at java.lang.Integer.parse(Integer.java:383)
12-21 11:30:11.015: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:372)
12-21 11:30:11.015: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:332)
12-21 11:30:11.015: W/System.err(4782): at java.lang.Integer.valueOf(Integer.java:506)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.characters(SceneXMLReader.java:427)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.readManifest(SceneXMLReader.java:549)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:72)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:1)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManagerAssets.loadScenes(OADataManagerAssets.java:200)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManager.startLoading(OADataManager.java:266)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.setupScenes(OAListComponentBase.java:87)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.onCreate(OAListComponentBase.java:167)
12-21 11:30:11.015: W/System.err(4782): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 11:30:11.015: W/System.err(4782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-21 11:30:11.015: W/System.err(4782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-21 11:30:11.015: W/System.err(4782): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 11:30:11.023: W/System.err(4782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-21 11:30:11.023: W/System.err(4782): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 11:30:11.023: W/System.err(4782): at android.os.Looper.loop(Looper.java:130)
12-21 11:30:11.023: W/System.err(4782): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:11.023: W/System.err(4782): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:11.023: W/System.err(4782): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:11.023: W/System.err(4782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:11.023: W/System.err(4782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:11.023: W/System.err(4782): at dalvik.system.NativeStart.main(Native Method)
12-21 11:30:11.054: W/System.err(4782): java.lang.NumberFormatException: unable to parse 'main' as integer
12-21 11:30:11.062: W/System.err(4782): at java.lang.Integer.parse(Integer.java:383)
12-21 11:30:11.062: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:372)
12-21 11:30:11.062: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:332)
12-21 11:30:11.070: W/System.err(4782): at java.lang.Integer.valueOf(Integer.java:506)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.characters(SceneXMLReader.java:427)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.readManifest(SceneXMLReader.java:549)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:72)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:1)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManagerAssets.loadScenes(OADataManagerAssets.java:200)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManager.startLoading(OADataManager.java:266)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.setupScenes(OAListComponentBase.java:87)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.onCreate(OAListComponentBase.java:167)
12-21 11:30:11.070: W/System.err(4782): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 11:30:11.070: W/System.err(4782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-21 11:30:11.070: W/System.err(4782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-21 11:30:11.070: W/System.err(4782): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 11:30:11.070: W/System.err(4782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-21 11:30:11.070: W/System.err(4782): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 11:30:11.070: W/System.err(4782): at android.os.Looper.loop(Looper.java:130)
12-21 11:30:11.070: W/System.err(4782): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:11.070: W/System.err(4782): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:11.070: W/System.err(4782): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:11.070: W/System.err(4782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:11.070: W/System.err(4782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:11.070: W/System.err(4782): at dalvik.system.NativeStart.main(Native Method)
12-21 11:30:11.242: W/System.err(4782): java.lang.NumberFormatException: unable to parse 'main' as integer
12-21 11:30:11.242: W/System.err(4782): at java.lang.Integer.parse(Integer.java:383)
12-21 11:30:11.242: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:372)
12-21 11:30:11.242: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:332)
12-21 11:30:11.242: W/System.err(4782): at java.lang.Integer.valueOf(Integer.java:506)
12-21 11:30:11.242: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.characters(SceneXMLReader.java:427)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
12-21 11:30:11.242: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.readManifest(SceneXMLReader.java:549)
12-21 11:30:11.242: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:72)
12-21 11:30:11.242: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:1)
12-21 11:30:11.242: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManagerAssets.loadScenes(OADataManagerAssets.java:200)
12-21 11:30:11.250: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManager.startLoading(OADataManager.java:266)
12-21 11:30:11.250: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.setupScenes(OAListComponentBase.java:87)
12-21 11:30:11.250: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.onCreate(OAListComponentBase.java:167)
12-21 11:30:11.250: W/System.err(4782): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 11:30:11.250: W/System.err(4782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-21 11:30:11.250: W/System.err(4782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-21 11:30:11.250: W/System.err(4782): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 11:30:11.250: W/System.err(4782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-21 11:30:11.250: W/System.err(4782): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 11:30:11.250: W/System.err(4782): at android.os.Looper.loop(Looper.java:130)
12-21 11:30:11.250: W/System.err(4782): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:11.250: W/System.err(4782): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:11.250: W/System.err(4782): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:11.250: W/System.err(4782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:11.250: W/System.err(4782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:11.250: W/System.err(4782): at dalvik.system.NativeStart.main(Native Method)
12-21 11:30:11.398: D/dalvikvm(4782): GC_CONCURRENT freed 265K, 47% free 3001K/5639K, external 499K/517K, paused 3ms+10ms
12-21 11:30:11.398: I/GPSManager(4782): Network Location Provider available
12-21 11:30:11.468: E/SensorManager(4782): registerListener :: handle = 1600615030 name= Gravity Sensor delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$3#4055af88
12-21 11:30:11.468: E/SensorManager(4782): =======>>>Sensor Thread RUNNING <<<========
12-21 11:30:11.476: E/SensorManager(4782): registerListener :: handle = 1 name= MMC328X delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$2#4055ac88
12-21 11:30:11.476: E/SensorManager(4782): reg :: handle = 1
12-21 11:30:12.687: E/SensorManager(4782): unregisterListener:: all sensors, listener = com.hitlabnz.androidar.sensors.inertial.InertialManager$2#4055ac88
12-21 11:30:12.695: E/SensorManager(4782): unregisterListener:: all sensors, listener = com.hitlabnz.androidar.sensors.inertial.InertialManager$3#4055af88
12-21 11:30:12.804: I/GPSManager(4782): Network Location Provider available
12-21 11:30:12.867: E/SensorManager(4782): registerListener :: handle = 1600615030 name= Gravity Sensor delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$3#4055af88
12-21 11:30:12.867: E/SensorManager(4782): registerListener :: handle = 1 name= MMC328X delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$2#4055ac88
12-21 11:30:12.875: E/SensorManager(4782): reg :: handle = 1
12-21 11:30:12.976: D/AndroidRuntime(4782): Shutting down VM
12-21 11:30:12.976: W/dalvikvm(4782): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-21 11:30:13.039: E/AndroidRuntime(4782): FATAL EXCEPTION: main
12-21 11:30:13.039: E/AndroidRuntime(4782): java.lang.NullPointerException
12-21 11:30:13.039: E/AndroidRuntime(4782): at com.hitlabnz.tutorialbasic.ListSceneAdapter$2.onClick(ListSceneAdapter.java:53)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.view.View.performClick(View.java:2485)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.view.View$PerformClick.run(View.java:9080)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.os.Handler.handleCallback(Handler.java:587)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.os.Looper.loop(Looper.java:130)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:13.039: E/AndroidRuntime(4782): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:13.039: E/AndroidRuntime(4782): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:13.039: E/AndroidRuntime(4782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:13.039: E/AndroidRuntime(4782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:13.039: E/AndroidRuntime(4782): at dalvik.system.NativeStart.main(Native Method)
OAListSceneAdapter class
/*
* Copyright 2011 the Human Interface Technology Laboratory New Zealand, University of Canterbury.
* http://www.hitlabnz.org
*
* This software is provided under the license terms described in LICENSE.TXT file distributed with this software package.
*/
package com.hitlabnz.outdoorar.api;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.hitlabnz.androidar.data.SceneData;
import com.hitlabnz.outdoorar.R;
/**
* Basic adapter class for making custom layout for list items in list components.
* #author Gun Lee
*/
public class OAListSceneAdapter extends ArrayAdapter<SceneData> {
protected LayoutInflater layoutInflater;
private int listItemLayoutId;
private Context c;
/**
* Instantiate with the context to access the layout resource for list items.
* Subclasses should override this method and call super(context, layoutResId) with a customized layout resource.
* #param context context for accessing resources
*/
public OAListSceneAdapter(Context context) {
this(context, R.layout.oa_list_item);
c =context;
}
/**
* Instantiate with the context to access the layout resource for list items.
* Subclasses should NOT override this method.
* #param context context for accessing resources
* #param layoutResId resource id of the list item layout
*/
protected OAListSceneAdapter(Context context, int layoutResId) {
super(context, R.layout.list_item);
this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listItemLayoutId = layoutResId;
}
/**
* Called back for setting up the list item view for a given scene.
* Subclasses should override this method for setting up the list item view for the scene.
* Subclasses need NOT to call the method in the super class.
* #param listItemView view for the list item, create with the layout resource id given at instan
* #param scene
*/
protected void setupListItemView(View listItemView, OAScene scene) {
TextView nameText = (TextView) listItemView.findViewById(R.id.listItemName);
TextView categoryText = (TextView) listItemView.findViewById(R.id.listItemCategory);
nameText.setText(scene.name);
if(scene.category == null)
categoryText.setText("");
else
categoryText.setText("- " + scene.category);
}
/**
* Called back for a view for the list item.
* Subclasses should NOT override this method, but override the setupListItemView() method for customization.
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = layoutInflater.inflate(listItemLayoutId, parent, false);
OAScene scene = (OAScene)this.getItem(position);
setupListItemView(convertView, scene);
return convertView;
}
}
That's because Listeners don't start Activities.
I'm assuming that the Context passed to your class is available to the whole class via a getter method or something similar. If not, you can store a global reference to this Context when it is obtained via the constructor. You may need to make it final or the Listener may complain.
Anyways, what you have to do, is call
context.startActivity(intent)
instead of
startActivity(intent);
because Contexts can start Activities.
You can pass the object activity class to the adapter, from which you are calling the adapter. Then use that object to call startActivity.
((Button)listItemView.findViewById(R.id.listItemMap)).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(A.alias,B.class);
A.alias.startActivity(intent);
}
});
I have created new activity that my MainActivity Should lunch, some why the application is crashing on the start of the new activity (called GamePlayActivity).
Here is the java code:
Intent startGameDrill = new Intent(MainActivity.this, GamePlayActivity.class);
startActivity(startGameDrill);
Here is the startGameDrill:
package com.simplemathgame;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class GamePlayActivity extends MainActivity {
int addDrills;
int subDrils;
int mulDrills;
int divDrills;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_play);
try {
numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);
minBoundText = (TextView) findViewById(R.id.min_text);
maxBoundText = (TextView) findViewById(R.id.max_text);
} catch (Exception e1) {
// TODO Auto-generated catch block
Log.w("game","error");
}
try {
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
subDrils = Integer.parseInt((String) numberOfSubDrills.getText());
mulDrills = Integer.parseInt((String) numberOfMulDrills.getText());
divDrills = Integer.parseInt((String) numberOfDivDrills.getText());
} catch (NumberFormatException e) {
Log.w("GameDrills","string to int");
}
Log.w("add", "" + addDrills);
Log.w("add", "" + subDrils);
Log.w("add", "" + mulDrills);
Log.w("add", "" + divDrills);
}
}
Here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simplemathgame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme" >
<activity
android:name="com.simplemathgame.Splash"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.simplemathgame.MainActivity"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.simplemathgame.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.simplemathgame.GamePlayActivity"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>
</application>
</manifest>
here is the logCat:
12-21 22:05:53.949: D/dalvikvm(610): GC_EXTERNAL_ALLOC freed 42K, 53% free 2546K/5379K, external 1917K/2137K, paused 41ms
12-21 22:06:32.809: D/AndroidRuntime(610): Shutting down VM
12-21 22:06:32.809: W/dalvikvm(610): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-21 22:06:32.818: E/AndroidRuntime(610): FATAL EXCEPTION: main
12-21 22:06:32.818: E/AndroidRuntime(610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplemathgame/com.simplemathgame.GamePlayActivity}: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.os.Looper.loop(Looper.java:123)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-21 22:06:32.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 22:06:32.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 22:06:32.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-21 22:06:32.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 22:06:32.818: E/AndroidRuntime(610): at dalvik.system.NativeStart.main(Native Method)
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-21 22:06:32.818: E/AndroidRuntime(610): ... 11 more
Here is the layout of the GamePlayActivity:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000044">
<TableRow
android:layout_marginTop="10dp"
android:gravity="center_vertical" >
<TextView
android:textColor="#FFFFFFFF"
android:textSize="16sp"
android:textStyle="bold"
android:text="Addition Drills:"/>
</TableRow>
</TableLayout>
Why does it crashes?
Have a look here:
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
Line 31 of GamePlayActivity.java is this line:
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
Since the only thing on this line that you reference a member or method of is numberOfAddDrills, then it must be null.
Consider that you are checking for exceptions when using findViewById; however, if the view is not found it will just return null, not throw an exception.
Have a look at the activity_game_play.xml you posted; there is no TextView with android:id="#+id/add_drills_number". You need to create it, and the same deal for the other five TextViews.
Oh, and a hint: Don't just catch generic exceptions with } catch (Exception e1) {, especially if you have no intent of reading the logs. I can't tell you how many times people have missed errors from doing this.
If you want to pass values from another layout, you have two options: keep a reference to the View objects (this is a bit silly, don't do this); or pass the data you need from them to your new Activity:
startGameDrill.putExtra("myIntExtra", 5); // For example
Then, in GamePlayActivity.onCreate(), you can fetch it using getIntent()'s extras:
Bundle extras = getIntent().getExtras();
int myIntExtra = extras.getInt("myIntExtra");
Then use that value instead of relying on the view in the previous layout. There is a fuller example in this answer.
Follow the stacktrace until you find some references to your classes:
It shows:
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
So you are creating a NullPointerException at line 31 in GamePlayActivity.java
Line 31:
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
so maybe numberOfAddDrills is null?
I have this code for an expandable list, I want to have a checkbox in the childgroups of the list view, and check if one of the checkboxes is checked.
The problem is that when I check if the checkbox is checked I get a NULL Pointer Exception.
Can you please tell me whats wrong?
here's my code - I've edited the code to inflate a view of the child_row.xml that holds that checkbox but I still get that null pointer, what am I doing wrong?!?!
package send.Shift;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class Shifts extends ExpandableListActivity implements
OnCheckedChangeListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list);
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), R.layout.group_row,
new String[] { "Group Item" }, new int[] { R.id.row_name },
createChildList(), R.layout.child_row,
new String[] { "Sub Item" }, new int[] { R.id.grp_child });
getExpandableListView().setGroupIndicator(
getResources().getDrawable(R.drawable.expander_group));
ExpandableListView EX = (ExpandableListView) findViewById(android.R.id.list);
EX.setAdapter(expListAdapter);
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
final TextView choosenGroup = (TextView) findViewById(R.id.choosen);
LayoutInflater inflater = LayoutInflater.from(Shifts.this);
View view2 = inflater.inflate(R.layout.child_row, null);
childBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(childBox.isChecked() == true){
choosenGroup.setText("Shift Set");
}
}
});
}
Here is some of the Logcat log:
12-23 07:38:00.644: W/dalvikvm(880): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-23 07:38:00.654: E/AndroidRuntime(880): FATAL EXCEPTION: main
12-23 07:38:00.654: E/AndroidRuntime(880): java.lang.RuntimeException: Unable to start activity ComponentInfo{send.Shift/send.Shift.Shifts}: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Looper.loop(Looper.java:123)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invoke(Method.java:507)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-23 07:38:00.654: E/AndroidRuntime(880): at dalvik.system.NativeStart.main(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): Caused by: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at send.Shift.Shifts.onCreate(Shifts.java:41)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-23 07:38:00.654: E/AndroidRuntime(880): ... 11 more
If you get a nullpointer on a certain line, something on that line is null. If it is the if(childBox.isChecked() line, probably childBox is null. Hard to say without the stack, but most probable cause is the line where you retrieve that checkbox.
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
Might be returning null, and this could be for several reasons. It could be your id is childBox instead of childBOX. Or that it is not in R.layout.list.
The best thing you can do is start debugging. What line is the error. Find the object that is null. Find out why it is null and if that is expected or not.
Instead of checking the childBox.isChecked() you can use the isChecked value. So your code would look like this:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
choosenGroup.setText("Shift Set");
}
}
Check your layout list.xml I think this layout may is missing android:id="#+id/childBOX" for Check Box or android:id="#+id/choosen" for TextView
Check the folowin sample
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/childBOX"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/choosen"/>