I am attempting to integrate Mixpanel into a simple application using the instructions here. However I am getting an error that is causing my App to crash.
I have no syntax errors in my code (see below).
Android Activity:
package com.example.testmixpanel;
import org.json.JSONException;
import org.json.JSONObject;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
public static final String MIXPANEL_TOKEN = "xxx"; //intentionally not shown
// Initialize the library with your
// Mixpanel project token, MIXPANEL_TOKEN, and a reference
// to your application context.
MixpanelAPI mixpanel =
MixpanelAPI.getInstance(this, MIXPANEL_TOKEN);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONObject props = new JSONObject();
mixpanel.track("Plan Selected", props);
}
#Override
protected void onDestroy() {
mixpanel.flush();
super.onDestroy();
}
}
However, when I try to run the application I get this error:
0-09 16:36:31.575: E/AndroidRuntime(13192): FATAL EXCEPTION: main
10-09 16:36:31.575: E/AndroidRuntime(13192): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testmixpanel/com.example.testmixpanel.MainActivity}: java.lang.NullPointerException
What could be the cause of this issue?
Full stack trace (for reference):
10-09 16:36:31.575: E/AndroidRuntime(13192): FATAL EXCEPTION: main
10-09 16:36:31.575: E/AndroidRuntime(13192): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testmixpanel/com.example.testmixpanel.MainActivity}: java.lang.NullPointerException
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.access$700(ActivityThread.java:159)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.os.Looper.loop(Looper.java:176)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.main(ActivityThread.java:5419)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.reflect.Method.invoke(Method.java:525)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
10-09 16:36:31.575: E/AndroidRuntime(13192): at dalvik.system.NativeStart.main(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192): Caused by: java.lang.NullPointerException
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.mixpanel.android.mpmetrics.MixpanelAPI.getInstance(MixpanelAPI.java:175)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.example.testmixpanel.MainActivity.<init>(MainActivity.java:22)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.Class.newInstanceImpl(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.Class.newInstance(Class.java:1130)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
10-09 16:36:31.575: E/AndroidRuntime(13192): ... 11 more
EDIT:
public class MainActivity extends Activity {
public static final String MIXPANEL_TOKEN = "xxxxx";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MixpanelAPI mixpanel =
MixpanelAPI.getInstance(getApplicationContext(), MIXPANEL_TOKEN);
setContentView(R.layout.activity_main);
JSONObject props = new JSONObject();
mixpanel.track("Plan Selected", props);
}
#Override
protected void onDestroy() {
mixpanel.flush(); //how to call this as mixpanel is not a class variable?
super.onDestroy();
}
}
If you want to use the mixpanel on onDestroy you should instantiate it like this:
public class MainActivity extends Activity {
public static final String MIXPANEL_TOKEN = "xxxxx";
MixpanelAPI mixpanel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mixpanel =
MixpanelAPI.getInstance(getApplicationContext(), MIXPANEL_TOKEN);
setContentView(R.layout.activity_main);
JSONObject props = new JSONObject();
mixpanel.track("Plan Selected", props);
}
(...)
Related
I am currently looking through the Android Developer tutorials and am on the most basic of tutorials which is passing an intent from one activity to another and displaying the result.
Eclipse shows the app has having no errors and everything works fine (not functional) till I insert these lines of code:
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
I'm looking at the LogCat and did manage to resolve one error where i'd simply named something incorrectly but now i'm stumped again. Here is my current LogCat output:
10-09 18:09:03.882: D/Send Message Button(9652): Pressed
10-09 18:09:03.917: E/FragmentManager(9652): No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.917: E/FragmentManager(9652): Activity state:
10-09 18:09:03.917: D/FragmentManager(9652): Local FragmentActivity 422d1598 State:
10-09 18:09:03.922: D/FragmentManager(9652): mCreated=truemResumed=false mStopped=false mReallyStopped=false
10-09 18:09:03.922: D/FragmentManager(9652): mLoadersStarted=false
10-09 18:09:03.922: D/FragmentManager(9652): Active Fragments in 422d1808:
10-09 18:09:03.922: D/FragmentManager(9652): #0: PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.922: D/FragmentManager(9652): mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
10-09 18:09:03.922: D/FragmentManager(9652): mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
10-09 18:09:03.927: D/FragmentManager(9652): mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-09 18:09:03.927: D/FragmentManager(9652): mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
10-09 18:09:03.927: D/FragmentManager(9652): mRetainInstance=false mRetaining=false mUserVisibleHint=true
10-09 18:09:03.927: D/FragmentManager(9652): mFragmentManager=FragmentManager{422d1808 in DisplayMessageActivity{422d1598}}
10-09 18:09:03.927: D/FragmentManager(9652): mActivity=com.example.myfirstapp.DisplayMessageActivity#422d1598
10-09 18:09:03.927: D/FragmentManager(9652): Added Fragments:
10-09 18:09:03.927: D/FragmentManager(9652): #0: PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.927: D/FragmentManager(9652): FragmentManager misc state:
10-09 18:09:03.927: D/FragmentManager(9652): mActivity=com.example.myfirstapp.DisplayMessageActivity#422d1598
10-09 18:09:03.927: D/FragmentManager(9652): mContainer=android.support.v4.app.FragmentActivity$2#422d1880
10-09 18:09:03.927: D/FragmentManager(9652): mCurState=2 mStateSaved=false mDestroyed=false
10-09 18:09:03.927: D/FragmentManager(9652): View Hierarchy:
10-09 18:09:03.927: D/FragmentManager(9652): com.android.internal.policy.impl.PhoneWindow$DecorView{422d31a8 V.E..... ... 0,0-0,0}
10-09 18:09:03.932: D/FragmentManager(9652): com.android.internal.widget.ActionBarOverlayLayout{422d37e8 V.ED.... ... 0,0-0,0 #1020313 android:id/action_bar_overlay_layout}
10-09 18:09:03.932: D/FragmentManager(9652): android.widget.FrameLayout{422d43f8 V.E..... ... 0,0-0,0 #1020002 android:id/content}
10-09 18:09:03.932: D/FragmentManager(9652): android.widget.TextView{422e3b30 V.ED.... ... 0,0-0,0}
10-09 18:09:03.932: D/FragmentManager(9652): com.android.internal.widget.ActionBarContainer{422d47f8 V.ED.... ... 0,0-0,0 #1020314 android:id/action_bar_container}
10-09 18:09:03.932: D/FragmentManager(9652): com.android.internal.widget.ActionBarView{422d4d00 V.E..... ... 0,0-0,0 #1020315 android:id/action_bar}
10-09 18:09:03.932: D/FragmentManager(9652): android.widget.LinearLayout{422d5270 VFE...C. ... 0,0-0,0}
10-09 18:09:03.932: D/FragmentManager(9652): com.android.internal.widget.ActionBarView$HomeView{422d6350 V.E..... ... 0,0-0,0}
10-09 18:09:03.937: D/FragmentManager(9652): android.widget.ImageView{422d66e8 V.ED.... ... 0,0-0,0 #102025a android:id/up}
10-09 18:09:03.937: D/FragmentManager(9652): android.widget.ImageView{422d6a48 V.ED.... ... 0,0-0,0 #102002c android:id/home}
10-09 18:09:03.937: D/FragmentManager(9652): android.widget.LinearLayout{422d7c40 G.E..... ... 0,0-0,0}
10-09 18:09:03.937: D/FragmentManager(9652): android.widget.TextView{422d7f58 V.ED.... ... 0,0-0,0 #1020265 android:id/action_bar_title}
10-09 18:09:03.937: D/FragmentManager(9652): android.widget.TextView{422d8be0 G.ED.... ... 0,0-0,0 #1020266 android:id/action_bar_subtitle}
10-09 18:09:03.937: D/FragmentManager(9652): com.android.internal.widget.ActionBarContextView{422d9200 G.E..... ... 0,0-0,0 #1020316 android:id/action_context_bar}
10-09 18:09:03.937: D/AndroidRuntime(9652): Shutting down VM
10-09 18:09:03.937: W/dalvikvm(9652): threadid=1: thread exiting with uncaught exception (group=0x41f55ba8)
10-09 18:09:03.942: E/AndroidRuntime(9652): FATAL EXCEPTION: main
10-09 18:09:03.942: E/AndroidRuntime(9652): Process: com.example.myfirstapp, PID: 9652
10-09 18:09:03.942: E/AndroidRuntime(9652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.os.Handler.dispatchMessage(Handler.java:102)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.os.Looper.loop(Looper.java:136)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-09 18:09:03.942: E/AndroidRuntime(9652): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 18:09:03.942: E/AndroidRuntime(9652): at java.lang.reflect.Method.invoke(Method.java:515)
10-09 18:09:03.942: E/AndroidRuntime(9652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-09 18:09:03.942: E/AndroidRuntime(9652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-09 18:09:03.942: E/AndroidRuntime(9652): at dalvik.system.NativeStart.main(Native Method)
10-09 18:09:03.942: E/AndroidRuntime(9652): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.Activity.performStart(Activity.java:5241)
10-09 18:09:03.942: E/AndroidRuntime(9652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
10-09 18:09:03.942: E/AndroidRuntime(9652): ... 11 more
MainActivity.java
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public void sendMessage(View view) {
Log.d("Send Message Button", "Pressed");
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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;
}
#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_main, container,
false);
return rootView;
}
}
}
DisplayMessageActivity.java
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#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_display_message,
container, false);
return rootView;
}
}
}
Any help is appreciated. Thanks
EDIT: Ran a clean and this is logcat now:
10-09 18:24:43.317: W/dalvikvm(9771): threadid=1: thread exiting with uncaught exception (group=0x41f55ba8)
10-09 18:24:43.322: E/AndroidRuntime(9771): FATAL EXCEPTION: main
10-09 18:24:43.322: E/AndroidRuntime(9771): Process: com.example.myfirstapp, PID: 9771
10-09 18:24:43.322: E/AndroidRuntime(9771): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4228bce8 #0 id=0x7f05003c}
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.os.Handler.dispatchMessage(Handler.java:102)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.os.Looper.loop(Looper.java:136)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread.main(ActivityThread.java:5001)
10-09 18:24:43.322: E/AndroidRuntime(9771): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 18:24:43.322: E/AndroidRuntime(9771): at java.lang.reflect.Method.invoke(Method.java:515)
10-09 18:24:43.322: E/AndroidRuntime(9771): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-09 18:24:43.322: E/AndroidRuntime(9771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-09 18:24:43.322: E/AndroidRuntime(9771): at dalvik.system.NativeStart.main(Native Method)
10-09 18:24:43.322: E/AndroidRuntime(9771): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4228bce8 #0 id=0x7f05003c}
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.Activity.performStart(Activity.java:5241)
10-09 18:24:43.322: E/AndroidRuntime(9771): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
10-09 18:24:43.322: E/AndroidRuntime(9771): ... 11 more
You have replace the view R.layout.activity_display_message with text view. The layout R.layout.activity_display_message contains the container to put fragment into it.
The IllegalArgument is passed in your setContentView that is textView in your case so the container Id in you mainActivity is missing and your fragment is still attached to your activity thats why the exception occured.
Though you have not provided xml. Check your xml file and look for container Because it says No view found for container. Try adding or replacing it with frame layout
10-09 18:09:03.942: E/AndroidRuntime(9652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{422e3888 #0 id=0x7f05003c}
In your setContentView you have to set a layout you defined in /res/layout folder. There you can create your textView...
For example setContentView(R.layout.myFirstLayout);
I'm getting a Null Pointer Exception when trying to set the map type after committing the MapFragment and getting a reference to its map with getMap().
I think the cause of the error is that the fragment hasn't been initialized yet and therefore I can't set the map type.
How can I know when this fragment has been initialized and I am allowed to call its public methods? Is there an interface that I can implement in my MainActivity to know when the fragment has loaded?
Furthermore, why am I allowed to call getMap() on mMapFragment if it is not yet initialized? Is it actually just the GoogleMap object that's not properly initialized?
Here's my code:
public class MainActivity extends Activity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
private FragmentManager fm;
private MapFragment mMapFragment;
private GoogleMap mGoogleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_container);
fm = getFragmentManager();
mMapFragment = MapFragment.newInstance();
fm.beginTransaction().add(R.id.fragment_container, mMapFragment).commit();
mGoogleMap = mMapFragment.getMap();
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
}
}
And the XML for main_container:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</FrameLayout>
And here's the LogCat output:
10-09 11:17:16.457: E/AndroidRuntime(31679): FATAL EXCEPTION: main
10-09 11:17:16.457: E/AndroidRuntime(31679): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matthewlogan.loopfinder/com.matthewlogan.loopfinder.MainActivity}: java.lang.NullPointerException
10-09 11:17:16.457: E/AndroidRuntime(31679): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2248)
10-09 11:17:16.457: E/AndroidRuntime(31679): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2298)
10-09 11:17:16.457: E/AndroidRuntime(31679): at android.app.ActivityThread.access$600(ActivityThread.java:142)
10-09 11:17:16.457: E/AndroidRuntime(31679): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
10-09 11:17:16.457: E/AndroidRuntime(31679): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 11:17:16.457: E/AndroidRuntime(31679): at android.os.Looper.loop(Looper.java:137)
10-09 11:17:16.457: E/AndroidRuntime(31679): at android.app.ActivityThread.main(ActivityThread.java:5270)
10-09 11:17:16.457: E/AndroidRuntime(31679): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 11:17:16.457: E/AndroidRuntime(31679): at java.lang.reflect.Method.invoke(Method.java:525)
10-09 11:17:16.457: E/AndroidRuntime(31679): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:974)
10-09 11:17:16.457: E/AndroidRuntime(31679): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:790)
10-09 11:17:16.457: E/AndroidRuntime(31679): at dalvik.system.NativeStart.main(Native Method)
10-09 11:17:16.457: E/AndroidRuntime(31679): Caused by: java.lang.NullPointerException
10-09 11:17:16.457: E/AndroidRuntime(31679): at com.matthewlogan.loopfinder.MainActivity.onCreate(MainActivity.java:36)
10-09 11:17:16.457: E/AndroidRuntime(31679): at android.app.Activity.performCreate(Activity.java:5133)
10-09 11:17:16.457: E/AndroidRuntime(31679): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1098)
10-09 11:17:16.457: E/AndroidRuntime(31679): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
10-09 11:17:16.457: E/AndroidRuntime(31679): ... 11 more
MapFragment's GoogleMap created via code is ready when:
its onCreateView has returned
Google Play Services app is installed and has correct version
If you want to get non-null GoogleMap in your Activity, do it in onResume:
if (mGoogleMap == null) {
mGoogleMap = mMapFragment.getMap();
if (mGoogleMap != null) {
initMap();
}
}
Double ifs ensure initMap is called only once per Activity instance and called only when GoogleMap is ready. It is a correct way of handling as the GoogleMap can be ready after user installs Google Play Services and returns to your app (onResume called for the second time).
How can I know when this fragment has been initialized and I am allowed to call its public methods?
Configure the map fragment in one of its lifecycle methods, like onActivityCreated().
Or, instead of using a FragmentTransaction, use a <fragment> tag in a layout.
In my application I want to present the user with a little DialogFragment containing a ListView with two hard-coded items. These items are both instances of a custom View class i wrote called PictureMethodOptionItem.
A PictureMethodOptionItem contains of a TextView next to an ImageView.
picture_method_list.xml:
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:stereovise="http://schemas.android.com/apk/res/de.app.stereovise"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<de.app.stereovise.PictureMethodOptionItem
android:id="#+id/listItemGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
stereovise:optionType="gallery" />
<de.app.stereovise.PictureMethodOptionItem
android:id="#+id/listItemCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
stereovise:optionType="camera" />
</ListView>
picture_method_option.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="7dp" >
<ImageView
android:id="#+id/method_option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:src="#drawable/output"
android:contentDescription="#string/methodOptionIcon" />
<TextView
android:id="#+id/method_option_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/method_option_icon"
android:layout_alignTop="#id/method_option_icon"
android:layout_centerVertical="true" />
</RelativeLayout>
And last but certainly not least the Java code of my custom View class:
PictureMethodOptionItem.java:
public class PictureMethodOptionItem extends View {
private Context context;
private ImageView methodIcon;
private TextView methodText;
public PictureMethodOptionItem(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initialize();
initFromAttributes(attrs);
}
public PictureMethodOptionItem(Context context) {
super(context);
this.context = context;
initialize();
}
private void initialize() {
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.picture_method_option, null, false);
//View.inflate(context, R.layout.picture_method_option, null);
methodIcon = (ImageView) findViewById(R.id.method_option_icon);
methodText = (TextView) findViewById(R.id.method_option_text);
// at this point methodIcon and methodText are still null!
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
}
private void initFromAttributes(AttributeSet attrs) {
String type = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/de.app.stereovise",
"optionType"
);
if("0".equals(type)) {
methodIcon.setImageResource(android.R.drawable.gallery_thumb);
methodText.setText(R.string.methodGallery);
} else {
methodIcon.setImageResource(android.R.drawable.ic_menu_camera);
methodText.setText(R.string.methodCamera);
}
}
}
The relevant code in which the DialogFragment gets inflated is from PictureOptionsDialog.java:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(context);
View contentView = inflater.inflate(R.layout.picture_method_list, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder
.setTitle(R.string.pictureChoiceTitle)
.setCancelable(true)
.setView(contentView);
return builder.create();
}
The problem is now, that if I try to show() this Dialog it'll give me the following error:
10-09 23:34:46.259: E/AndroidRuntime(14660): FATAL EXCEPTION: main
10-09 23:34:46.259: E/AndroidRuntime(14660): android.view.InflateException: Binary XML file line #8: Error inflating class de.app.stereovise.PictureMethodOptionItem
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-09 23:34:46.259: E/AndroidRuntime(14660): at de.app.stereovise.PictureOptionsDialog.onCreateDialog(PictureOptionsDialog.java:22)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.os.Handler.handleCallback(Handler.java:587)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.os.Looper.loop(Looper.java:144)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.app.ActivityThread.main(ActivityThread.java:4937)
10-09 23:34:46.259: E/AndroidRuntime(14660): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 23:34:46.259: E/AndroidRuntime(14660): at java.lang.reflect.Method.invoke(Method.java:521)
10-09 23:34:46.259: E/AndroidRuntime(14660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-09 23:34:46.259: E/AndroidRuntime(14660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-09 23:34:46.259: E/AndroidRuntime(14660): at dalvik.system.NativeStart.main(Native Method)
10-09 23:34:46.259: E/AndroidRuntime(14660): Caused by: java.lang.reflect.InvocationTargetException
10-09 23:34:46.259: E/AndroidRuntime(14660): at de.app.stereovise.PictureMethodOptionItem.<init>(PictureMethodOptionItem.java:22)
10-09 23:34:46.259: E/AndroidRuntime(14660): at java.lang.reflect.Constructor.constructNative(Native Method)
10-09 23:34:46.259: E/AndroidRuntime(14660): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
10-09 23:34:46.259: E/AndroidRuntime(14660): ... 20 more
10-09 23:34:46.259: E/AndroidRuntime(14660): Caused by: java.lang.NullPointerException
10-09 23:34:46.259: E/AndroidRuntime(14660): at de.app.stereovise.PictureMethodOptionItem.initFromAttributes(PictureMethodOptionItem.java:55)
10-09 23:34:46.259: E/AndroidRuntime(14660): ... 24 more
Hours of search only brought up solutions that either were already present in my code (such as overriding the View constructor that takes an AttributeSet) or aren't applicable in my case b/c they were pointing more towards Activities and their setContentView() methods.
Does anybody see the mistake(s) I made (probably very dull ones) and can tell me why the LayoutInflater has such a hard time inflating my little DialogFragment?
You call this method:
inflater.inflate(R.layout.picture_method_option, null, false);
And you ignore the result. The last parameter, false, indicates to not add the content of the XML layout to the layout root (which you set to null anyway.) You need to do something like this:
View v = inflater.inflate(R.layout.picture_method_option, null, false);
addView(v);
Or:
inflater.inflate(R.layout.picture_method_option, this, true);
As I have seen on previously asked questions, inside the custom adapter class (say, MyAdapter extends ArrayAdapter) they always use an inflated xml list-item layout. What I am hoping to do is create everything entirely using Java and no XML...
// for example
String[] wordlist = new String[] {a, b, c};
LinearLayout list_item_layout = new LinearLayout(this);
list_item_layout.setId(5000);
TextView listText = new TextView(this);
listText.setId(5001);
listLayout.addView(listText);
ListView list = new ListView(this);
// ** QUESTION ** do I declare a programmatic .setAdapter() like this?
// ** TAKE NOTE ** I passed 'wordlist' here..
list.setAdapter(new MyAdapter(this, list_item_layout.getId(), listText.getId(), wordlist));
and then for MyAdapter...
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView; //is this the list_item_layout that I passed??
TextView tv = (TextView) v.findViewById(5001);
// ** QUESTION ** do I pass 'wordlist' again here?
tv.setText( wordlist[position] );
return v;
}
}
What happens when I run this on my device is I get the following errors...
10-08 23:11:19.775: E/AndroidRuntime(18276): FATAL EXCEPTION: main
10-08 23:11:19.775: E/AndroidRuntime(18276): android.content.res.Resources$NotFoundException: String resource ID #0x0
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.content.res.Resources.getText(Resources.java:222)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.widget.TextView.setText(TextView.java:3011)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.turista.client.TuristaClientMain.onClick(TuristaClientMain.java:113)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.view.View.performClick(View.java:2538)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.view.View$PerformClick.run(View.java:9152)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Handler.handleCallback(Handler.java:587)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Looper.loop(Looper.java:123)
10-08 23:11:19.775: E/AndroidRuntime(18276): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-08 23:11:19.775: E/AndroidRuntime(18276): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 23:11:19.775: E/AndroidRuntime(18276): at java.lang.reflect.Method.invoke(Method.java:507)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-08 23:11:19.775: E/AndroidRuntime(18276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-08 23:11:19.775: E/AndroidRuntime(18276): at dalvik.system.NativeStart.main(Native Method)
Can anyone explain how to do this programmatically?
* EDITED * October 9, 2012
Ok, as I am still stuck in this issue I think I've made some improvements but still get error messages. The improved code is as follows..
//wordlist is a global variable
String[] wordlist = new String[] {a, b, c};
// ..
// .. inside onCreate...
ListView list = new ListView(this);
list.setAdapter(new MyAdapter(this, R.layout.listitem, R.id.mLargeTextView, wordlist));
// since the ArrayAdapter class needs XML parameters to inflate, I created a dummy layout
now inside MyAdapter class..
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout listLayout = new LinearLayout(Main.this);
listLayout.setLayoutParams(new LayoutParams(wrapContent, wrapContent));
listLayout.setId(5000);
TextView listText = new TextView(Main.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(wordlist[position]);
return listLayout;
}
}
As you can see, I think I have to override getView in order to display my custom view so this is what I did. Unfortunately I think I misunderstood it. Here are the errors..
10-09 09:24:10.095: E/AndroidRuntime(2517): FATAL EXCEPTION: main
10-09 09:24:10.095: E/AndroidRuntime(2517): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.measureScrapChild(ListView.java:1183)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.measureHeightOfChildren(ListView.java:1266)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.onMeasure(ListView.java:1175)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.os.Looper.loop(Looper.java:123)
10-09 09:24:10.095: E/AndroidRuntime(2517): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-09 09:24:10.095: E/AndroidRuntime(2517): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:24:10.095: E/AndroidRuntime(2517): at java.lang.reflect.Method.invoke(Method.java:507)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-09 09:24:10.095: E/AndroidRuntime(2517): at dalvik.system.NativeStart.main(Native Method)
ListView extends AbsListView which in turn extends AdapterView which extends view group. So all the child of ListView would be added to ViewGroup. So to set the layout param, you can
use ViewGroup.LayoutParam while setting layout param for listLayout.
Or try AbsListView.LayoutParams for setting layout param for listLayout
Please try it and let me know if it worked.
Following code is working fine..
public class MainActivity1 extends Activity {
String[] wordlist = new String[] { "a", "b", "c" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list = new ListView(this);
list.setAdapter(new MyAdapter(this, wordlist));
setContentView(list);
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, String[] strings) {
super(context, -1, -1, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout listLayout = new LinearLayout(MainActivity1.this);
listLayout.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.WRAP_CONTENT,
AbsListView.LayoutParams.WRAP_CONTENT));
listLayout.setId(5000);
TextView listText = new TextView(MainActivity1.this);
listText.setId(5001);
listLayout.addView(listText);
listText.setText(super.getItem(position));
return listLayout;
}
}
}
The problem was that by default Eclipse imports ViewGroup.LayoutParams which are incompatible with the AbsListView.LayoutParams which need to be used for setting layout param for view returned from the getView() method.
Please check and let me know how it went..
To add to the above answer, setting id with a constant number is not a good idea.
Replace, listLayout.setId(5000); with,listLayout.setId(View.generateViewId());
In case you are targeting API 16 and below, refer this.
when testing my application in the emulator(I'm using Eclipse) it shows me "The application Counter(com.ian.counter) has stopped unexpectedly. Please try again." when running it. I've searched and searched for an answer but I've found none. Anyone know the problem?
Code:
package com.ian.counter;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.Toast;
public class CounterActivity extends Activity {
/** Called when the activity is first created. */
TextView textView1 = (TextView) this.findViewById(R.id.textView1);
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}
}
Logcat:
12-28 16:59:20.615: D/AndroidRuntime(353): Shutting down VM
12-28 16:59:20.686: W/dalvikvm(353): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-28 16:59:20.756: E/AndroidRuntime(353): FATAL EXCEPTION: main
12-28 16:59:20.756: E/AndroidRuntime(353): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ian.counter/com.ian.counter.CounterActivity}: java.lang.NullPointerException
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.os.Looper.loop(Looper.java:123)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.reflect.Method.invoke(Method.java:507)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-28 16:59:20.756: E/AndroidRuntime(353): at dalvik.system.NativeStart.main(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): Caused by: java.lang.NullPointerException
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.Activity.findViewById(Activity.java:1647)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.ian.counter.CounterActivity.<init>(CounterActivity.java:10)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.Class.newInstanceImpl(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.Class.newInstance(Class.java:1409)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
Any help is appreciated.
You're trying to set the textView1 variable before the layout has been set. You need to define it as a variable and then assign it in onCreate() after you've called setContentView(). Like this:
package com.ian.counter;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.Toast;
public class CounterActivity extends Activity {
/** Called when the activity is first created. */
TextView textView1;
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView) this.findViewById(R.id.textView1);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}
}
Just copy and paste the above, that'll work just fine.
You should put this line
textView1 = (TextView) this.findViewById(R.id.textView1);
after
setContentView(R.layout.main);
and you just declare TextView in the begining
TextView textView1;
dont try to use findViewById(R.id.textView1); while declaraing instance variable as view does not exist at that point of time.
This is how your code should look like:
TextView textView1 = null;
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView1 = (TextView) findViewById(R.id.textView1);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}