i want to run the application in ma Galaxy S2 but i found this error. i post the logcat. please tell me what s the problem? and how to resolve it ? thanks
05-07 11:23:38.820: D/AndroidRuntime(8900): Shutting down VM
05-07 11:23:38.820: W/dalvikvm(8900): threadid=1: thread exiting with uncaught exception (group=0x40c3f1f8)
05-07 11:23:38.820: E/AndroidRuntime(8900): FATAL EXCEPTION: main
05-07 11:23:38.820: E/AndroidRuntime(8900): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.aaaaa/com.example.aaaaa.MainActivity}: java.lang.NullPointerException
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1892)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.os.Looper.loop(Looper.java:137)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.app.ActivityThread.main(ActivityThread.java:4507)
05-07 11:23:38.820: E/AndroidRuntime(8900): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 11:23:38.820: E/AndroidRuntime(8900): at java.lang.reflect.Method.invoke(Method.java:511)
05-07 11:23:38.820: E/AndroidRuntime(8900): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-07 11:23:38.820: E/AndroidRuntime(8900): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-07 11:23:38.820: E/AndroidRuntime(8900): at dalvik.system.NativeStart.main(Native Method)
05-07 11:23:38.820: E/AndroidRuntime(8900): Caused by: java.lang.NullPointerException
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.view.View.<init>(View.java:2721)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.view.View.<init>(View.java:2769)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.widget.TextView.<init>(TextView.java:573)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.widget.TextView.<init>(TextView.java:566)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.widget.TextView.<init>(TextView.java:561)
05-07 11:23:38.820: E/AndroidRuntime(8900): at com.example.aaaaa.MainActivity.<init>(MainActivity.java:28)
05-07 11:23:38.820: E/AndroidRuntime(8900): at java.lang.Class.newInstanceImpl(Native Method)
05-07 11:23:38.820: E/AndroidRuntime(8900): at java.lang.Class.newInstance(Class.java:1319)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
05-07 11:23:38.820: E/AndroidRuntime(8900): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1883)
05-07 11:23:38.820: E/AndroidRuntime(8900): ... 11 more
this is MainActivity.java I think that the error is in the TextView view = new TextView(this);
because i dont have TextView in MainActivity but i want to extract data from textview in another class (setting.java)
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MainActivity extends MapActivity {
String num;
TextView view = new TextView(this);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extra = getIntent().getExtras();
num = extra.getString("label");
final TextView view = new TextView(this);
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
String sms = "";
while (cur.moveToNext()) {
String spn ="+21629188153";
String phNum =cur.getString(2);
if (spn.equals(phNum)){
sms +=cur.getString(12);
}
}
String a =sms.substring(0,10);
String b =sms.substring(11,21);
MapView maMap;
MapController monControler;
maMap = (MapView)findViewById(R.id.myGmap);
maMap.setBuiltInZoomControls(true);
GeoPoint point = new GeoPoint (microdegrees(Double.parseDouble(a)),microdegrees(Double.parseDouble(b)));
MonOverlay object = new MonOverlay(getResources().getDrawable(R.drawable.marker_voisinage_rouge));
object.addPoint(point);
maMap.getOverlays().add(object);
maMap.setSatellite(true);
monControler = maMap.getController();
monControler.setZoom(12);
monControler.setCenter(point);
Button monBouton = (Button)findViewById(R.id.envoie_sms);
monBouton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View monBoutton = null;
// TODO Auto-generated method stub
Intent monIntent = new Intent(MainActivity.this,SendSms.class);
startActivity(monIntent);
}
} );
Button mnBouton = (Button)findViewById(R.id.setting);
mnBouton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View monBoutton = null;
// TODO Auto-generated method stub
setContentView(R.layout.setting);
}
} );
Button myBouton = (Button)findViewById(R.id.aff);
myBouton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View monBoutton = null;
// TODO Auto-generated method stub
view.setText(num);
setContentView(view);
}
} );
}
private int microdegrees (double value){
return (int)(value*1000000);
}
@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;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public class MonOverlay extends ItemizedOverlay<OverlayItem>{
List<GeoPoint> points = new ArrayList<GeoPoint>();
public MonOverlay(Drawable arg0) {
super(boundCenterBottom(arg0));
// TODO Auto-generated constructor stub
}
@Override
protected OverlayItem createItem(int i) {
GeoPoint point = points.get(i);
return new OverlayItem(point,"titre","description");
}
@Override
public int size() {
return points.size();
}
public void addPoint (GeoPoint point){
this.points.add(point);
populate();
}
}
}
this is Setting.java and i want to pass s1 = num.getText().toString();
to MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class setting extends Activity {
Button sauvg;
EditText num;
String s1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
sauvg = (Button)findViewById(R.id.ok);
num = (EditText)findViewById(R.id.editText1);
sauvg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent t = new Intent(setting.this, MainActivity.class);
s1 = num.getText().toString();
t.putExtra("label",s1);
startActivity(t);
// TODO Auto-generated method stub
}
});
}
}
The error is here:
public class MainActivity extends MapActivity {
String num;
TextView view = new TextView(this); // <- HERE
You cannot create a View there, but you should move the creation in the onCreate
method, like this:
public class MainActivity extends MapActivity {
String num;
TextView view;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.view = new TextView(this);
Bundle extra = getIntent().getExtras();
Be careful with variable names: i see you have 2 called "view", which is not an error (one is a local variable in onCreate
, the other is an instance variable) but it's easy to make errors.
By the way you can't access directly the TextView in other Activity. If you want to share data between activities you should use Intents or Message&Handler
If MainActivity is the first activity to be launched when your app starts I suppose that you get your app crashing. This happens because you should check if the calling intent has extras before you use it, like this:
Bundle extra = getIntent().getExtras();
if (extra != null) {
num = extra.getString("label");
}