Reputation: 41
I have two classes in program. I want when user clicks on the two first buttons, the program go to first class(Main Class). I don't use fragment. I write this code but when run the program and click on the Places button, the program has stopped. Why?(before i add button clicks, the program correct run). LogCat write nullexception but i can't find where is null?! Please help me.
Location.java
public class Location extends MapActivity {
private FrameLayout linear;
private MapView map;
private MapController controller;
private EditText text_location;
GeoPoint searchLocation;
GeoPoint mylocation;
double myLatitude=0;
double myLongitude=0;
List<Overlay> mapOverlays;
Drawable drawable;
SimpleItemizedOverlay itemizedOverlay;
//Other classes
private Button mHome;
private Button mProduct;
private Button mPlaces;
private WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
initOtherButtons();
//....
}
private void initOtherButtons(){
mHome=(Button)findViewById(R.id.button_home);
mProduct=(Button)findViewById(R.id.button_product);
mPlaces=(Button)findViewById(R.id.button_places);
mWebView=(WebView)findViewById(R.id.web_view);
//TEST
mHome.setOnClickListener(onClickListener);
mProduct.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener=new OnClickListener(){
Intent i=new Intent(Location.this,Main.class);
public void onClick(View v){
switch(v.getId()){
case R.id.button_home:
startActivity(i);
break;
case R.id.button_product:
startActivity(i);
break;
}
}
};
}
Main.java
public class Main extends Activity {
private WebView mWebView;
private Button mHome;
private Button mProduct;
private Button mPlaces;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView)findViewById(R.id.web_view);
mWebView.loadUrl("http://www.google.com");
mHome = (Button)findViewById(R.id.button_home);
mProduct=(Button)findViewById(R.id.button_product);
mPlaces=(Button)findViewById(R.id.button_places);
mHome.setOnClickListener(onClickListener);
mProduct.setOnClickListener(onClickListener);
mPlaces.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener=new OnClickListener(){
public void onClick(View v){
switch(v.getId()){
case R.id.button_home:
mWebView.loadUrl("http://www.google.com");
break;
case R.id.button_product:
mWebView.loadUrl("http://www.google.com");
break;
case R.id.button_places:
Intent i=new Intent(Main.this,Location.class);
startActivity(i);
break;
}
}
};
}
Manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.loyaltier"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".Main"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Location"
android:label="@string/Location_title">
</activity>
<activity android:name=".SimpleItemizedOverlay"
android:label="@string/app_name">
</activity>
<activity android:name=".BalloonOverlayView"
android:label="@string/app_name">
</activity>
<activity android:name=".BalloonItemizedOverlay"
android:label="@string/app_name">
</activity>
</application>
</manifest>
LogCat:
11-11 09:56:16.506: D/OpenGLRenderer(18214): Flushing caches (mode 1)
11-11 09:56:16.506: D/OpenGLRenderer(18214): Flushing caches (mode 0)
11-11 09:56:38.006: D/dalvikvm(18275): GC_CONCURRENT freed 172K, 2% free 14351K/14599K, paused 9ms+2ms
11-11 09:56:38.053: D/dalvikvm(18275): GC_FOR_ALLOC freed 7K, 2% free 14715K/14983K, paused 11ms
11-11 09:56:39.287: D/AndroidRuntime(18275): Shutting down VM
11-11 09:56:39.287: W/dalvikvm(18275): threadid=1: thread exiting with uncaught exception (group=0x40a891f8)
11-11 09:56:39.295: E/AndroidRuntime(18275): FATAL EXCEPTION: main
11-11 09:56:39.295: E/AndroidRuntime(18275): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.loyaltier/org.example.loyaltier.Location}: java.lang.NullPointerException
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.os.Looper.loop(Looper.java:137)
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-11 09:56:39.295: E/AndroidRuntime(18275): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 09:56:39.295: E/AndroidRuntime(18275): at java.lang.reflect.Method.invoke(Method.java:511)
11-11 09:56:39.295: E/AndroidRuntime(18275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-11 09:56:39.295: E/AndroidRuntime(18275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-11 09:56:39.295: E/AndroidRuntime(18275): at dalvik.system.NativeStart.main(Native Method)
11-11 09:56:39.295: E/AndroidRuntime(18275): Caused by: java.lang.NullPointerException
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.content.ComponentName.<init>(ComponentName.java:75)
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.content.Intent.<init>(In tent.java:3004)
11-11 09:56:39.295: E/AndroidRuntime(18275): at org.example.loyaltier.Location$1.<init>(Location.java:118)
11-11 09:56:39.295: E/AndroidRuntime(18275): at org.example.loyaltier.Location.<init>(Location.java:117)
11-11 09:56:39.295: E/AndroidRuntime(18275): at java.lang.Class.newInstanceImpl(Native Method)
11-11 09:56:39.295: E/AndroidRuntime(18275): at java.lang.Class.newInstance(Class.java:1319)
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-11 09:56:39.295: E/AndroidRuntime(18275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
11-11 09:56:39.295: E/AndroidRuntime(18275): ... 11 more
EDIT: SIMPLE Program
private OnClickListener onClickListener=new OnClickListener(){
public void onClick(View v){
switch(v.getId()){
case R.id.button_home:
mWebView.loadUrl("http://www.google.com");
break;
}
}
};
Upvotes: 0
Views: 142
Reputation: 41
I think the problem was of Intent object because i changed the program, the program correct run. What happened? Why should Intent object in per case?
public void onClick(View v){
switch(v.getId()){
case R.id.button_home:
Intent i=new Intent(Location.this,Main.class);
startActivity(i);
break;
case R.id.button_product:
Intent j=new Intent(Location.this,Main.class);
startActivity(j);
break;
case R.id.button_places:
//
break;
}
}
Thanks.Cheers
Upvotes: 0
Reputation: 7435
if you are running this in emulator then check that your emulator has created with support for Google API which is required for the MapView. Check out this link for AVD settings.
EDIT01: Please try moving Intent i=new Intent(Location.this,Main.class);
in side the onClick()
method like following:
private OnClickListener onClickListener=new OnClickListener(){
public void onClick(View v){
Intent i=new Intent(Location.this,Main.class);
switch(v.getId()){
case R.id.button_home:
startActivity(i);
break;
case R.id.button_product:
startActivity(i);
break;
}
}
};
Upvotes: 0
Reputation: 14274
It looks to be a NullPointerException:
11-11 09:56:39.295: E/AndroidRuntime(18275): Caused by: java.lang.NullPointerException
Are both your Activtiy classes in the manifest? If so, check Main's onCreate for NPEs or post your code here.
Upvotes: 0
Reputation: 51
Could be that the activity is declared incorrectly (or not at all) in your manifest.xml. Verify the manifest and make sure the activity is declared and that the name is correct.
Upvotes: 1