I'm trying to develop a very simple apk.
I'm using a textView to show two team's name that i enter in a previous activity (brought here with the intent that open this activity).
When i try to use setText to show the names of these teams the apk crash.
This is the class that crash:
public class MatchPage extends Activity {
private String locali= null;
private String ospiti= null;
private TextView localiTV;
private TextView ospitiTV;
private MatchRugby partita;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.match);
localiTV =(TextView) findViewById(R.id.localiTV);
ospitiTV =(TextView) findViewById(R.id.ospitiTV);
getLocali();
getOspiti();
createMatch();
localiTV.setText("Locali /n"+ partita.teamA.getName());
ospitiTV.setText("Ospiti /n"+ partita.teamB.getName());
}
/**
* Prende il nome della squadra locale dall'intent
* #return
*/
public String getLocali(){
Intent matchStart = getIntent();
String locali = matchStart.getStringExtra(NewMatchPage.LOCALI);
return locali;
}
/**
* prende il nome della squadra ospite dall'intent
* #return
*/
public String getOspiti(){
Intent matchStart = getIntent();
String ospiti = matchStart.getStringExtra(NewMatchPage.OSPITI);
return ospiti;
}
public MatchRugby createMatch(){
TeamRugby teamLocali= new TeamRugby(locali);
TeamRugby teamOspiti= new TeamRugby(ospiti);
MatchRugby partita= new MatchRugby(teamLocali, teamOspiti);
return partita;
}
}
This is the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/localiTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ospitiTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
This is the class that send the intent:
public class NewMatchPage extends Activity {
public static final String LOCALI = null;
public static final String OSPITI = null;
private Button startMatch;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_match);
startMatch= (Button) findViewById(R.id.startMatch);
startMatch.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
startMatch();
}
});
}
public void startMatch(){
Intent startMatch= new Intent(this, MatchPage.class);
//Prendo il testo scritto nella casella locali e la porto nella partita
EditText locali= (EditText) findViewById(R.id.Locali);
String locali1 = locali.getText().toString();
startMatch.putExtra(LOCALI, locali1);
//Prendo il testo scritto nella casella ospiti e la porto nella partita
EditText ospiti= (EditText) findViewById(R.id.Ospiti);
String ospiti1 = ospiti.getText().toString();
startMatch.putExtra(OSPITI, ospiti1);
//inizio la partita
startActivity(startMatch);
}
}
And finally that's the logcat log:
04-24 16:49:08.928: W/dalvikvm(1377): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-24 16:49:08.958: E/AndroidRuntime(1377): FATAL EXCEPTION: main
04-24 16:49:08.958: E/AndroidRuntime(1377): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gmail.david.corsalini.sportscout/com.gmail.david.corsalini.sportscout.MatchPage}: java.lang.NullPointerException
04-24 16:49:08.958: E/AndroidRuntime(1377): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-24 16:49:08.958: E/AndroidRuntime(1377): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-24 16:49:08.958: E/AndroidRuntime(1377): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-24 16:49:08.958: E/AndroidRuntime(1377): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-24 16:49:08.958: E/AndroidRuntime(1377): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 16:49:08.958: E/AndroidRuntime(1377): at android.os.Looper.loop(Looper.java:137)
04-24 16:49:08.958: E/AndroidRuntime(1377): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 16:49:08.958: E/AndroidRuntime(1377): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 16:49:08.958: E/AndroidRuntime(1377): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 16:49:08.958: E/AndroidRuntime(1377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 16:49:08.958: E/AndroidRuntime(1377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 16:49:08.958: E/AndroidRuntime(1377): at dalvik.system.NativeStart.main(Native Method)
04-24 16:49:08.958: E/AndroidRuntime(1377): Caused by: java.lang.NullPointerException
04-24 16:49:08.958: E/AndroidRuntime(1377): at com.gmail.david.corsalini.sportscout.MatchPage.onCreate(MatchPage.java:25)
04-24 16:49:08.958: E/AndroidRuntime(1377): at android.app.Activity.performCreate(Activity.java:4465)
04-24 16:49:08.958: E/AndroidRuntime(1377): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-24 16:49:08.958: E/AndroidRuntime(1377): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-24 16:49:08.958: E/AndroidRuntime(1377): ... 11 more
MatchRugby class
public class MatchRugby {
public TeamRugby teamA;
public TeamRugby teamB;
/**
* Costruttore della partita
*/
public MatchRugby(TeamRugby teamA, TeamRugby teamB){
this.teamA=teamA;
this.teamB=teamB;
}
/**
* #return the teamA
*/
public TeamRugby getTeamA() {
return teamA;
}
/**
* #param teamA the teamA to set
*/
public void setTeamA(TeamRugby teamA) {
this.teamA = teamA;
}
/**
* #return the teamB
*/
public TeamRugby getTeamB() {
return teamB;
}
/**
* #param teamB the teamB to set
*/
public void setTeamB(TeamRugby teamB) {
this.teamB = teamB;
}
public void EndOfMatch(){
//nothing to do with the problem
}
}
Your variable scoping is all off. set up your class like this:
public class MatchPage extends Activity {
private String locali= null;
private String ospiti= null;
private TextView localiTV;
private TextView ospitiTV;
private MatchRugby partita;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.match);
localiTV =(TextView) findViewById(R.id.localiTV);
ospitiTV =(TextView) findViewById(R.id.ospitiTV);
getLocali();
getOspiti();
createMatch();
localiTV.setText("Locali /n"+ partita.teamA.getName());
ospitiTV.setText("Ospiti /n"+ partita.teamB.getName());
}
/**
* Prende il nome della squadra locale dall'intent
* #return
*/
public String getLocali(){
if (locali == null) {
Intent matchStart = getIntent();
locali = matchStart.getStringExtra(NewMatchPage.LOCALI);
}
return locali;
}
/**
* prende il nome della squadra ospite dall'intent
* #return
*/
public String getOspiti(){
if (ospiti == null) {
Intent matchStart = getIntent();
ospiti = matchStart.getStringExtra(NewMatchPage.OSPITI);
}
return ospiti;
}
public MatchRugby createMatch(){
TeamRugby teamLocali= new TeamRugby(locali);
TeamRugby teamOspiti= new TeamRugby(ospiti);
partita = new MatchRugby(teamLocali, teamOspiti);
return partita
}
private String locali
private String ospiti
private MatchRugby partita
}
You have a NullPointerException at line #25 of your MatchPage class in onCreate method. Not sure which line exactly it is in the code you posted, but my best guess is your findViewById call fails to find anything.
java.lang.NullPointerException
at com.gmail.david.corsalini.sportscout.MatchPage.onCreate(MatchPage.java:25)
Seem that you partita object is null from the above error. And from your code where you are initializing it
createMatch();
replace the above method with below line
partita = createMatch();
Also you can change the following code::
public void createMatch(){
TeamRugby teamLocali= new TeamRugby(locali);
TeamRugby teamOspiti= new TeamRugby(ospiti);
partita= new MatchRugby(teamLocali, teamOspiti);
}
Related
I write this counter but when I lunch the app get crashed I don't now where the error some help and thank you tell where is the Error here or how can I do code in right way
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
TextView txt = (TextView)findViewById(R.id.textView);
count i;
public void bu1(View view) {
starrtime();
}
public void bu2(View view) {
i.cancel();
}
void starrtime(){
i = new count(100,1000);
i.start();
}
public class count extends CountDownTimer {
public count(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
txt.setText(String.valueOf(millisUntilFinished));
}
#Override
public void onFinish() {
txt.setText("Done");
}
}
}
this my log cat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kira.counter, PID: 4598
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kira.counter/com.example.kira.counter.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:116)
at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:50)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.kira.counter.MainActivity.(MainActivity.java:23)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
I believe you can not initialize a TextView before runtime because the views are not set yet. Classes should be capitalized. Try this.
public class MainActivity extends AppCompatActivity{
private TextView txt;
private Count i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.textView);
}
}
When i am trying to display the contacts in card view when i click on Button it shows fatal exception error it doesnot display cardview anyone can solve this programming with brillliance and another error is it show only one cardview when i click on again the app will be strucked?
Showcontacts.java
public class ShowContacts extends Activity
{
private SQLiteDatabase db;
DbOperations doo;
private List<Contacts> contactsList;
private RecyclerView rv;
private Cursor c;
String names,email,address;
int phone;
String read_query = "select * from"+ ContactsTask.ContactsEntry.TABLE_NAME;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycle_layout);
doo = new DbOperations(this);
openDatabase();
rv = (RecyclerView)findViewById(R.id.recyclerview);
initializeData();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
rv.setLayoutManager(linearLayoutManager);
rv.setHasFixedSize(true);
ContactAdapter cc = new ContactAdapter(contactsList);
rv.setAdapter(cc);
}
public void initializeData() {
contactsList = new ArrayList<>();
c = db.rawQuery(read_query,null);
c.moveToFirst();
while (!c.isLast())
{
names = c.getString(0);
phone = c.getInt(1);
email = c.getString(2);
address = c.getString(3);
contactsList.add(new Contacts(names,phone,email,address));
}
c.isLast();
names = c.getString(0);
phone = c.getInt(1);
email = c.getString(2);
address = c.getString(3);
contactsList.add(new Contacts(names,phone,email,address));
}
private void openDatabase() {
db = openOrCreateDatabase("contactDB", Context.MODE_PRIVATE,null);
}
}
Logacat error
06-28 08:57:43.107 568-568/com.example.anilkumar.contactstask E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.anilkumar.contactstask/com.example.anilkumar.contactstask.ShowContacts}: android.database.sqlite.SQLiteException: near "fromcontacts": syntax error: , while compiling: select * fromcontacts
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "fromcontacts": syntax error: , while compiling: select * fromcontacts
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1538)
at com.example.anilkumar.contactstask.ShowContacts.initializeData(ShowContacts.java:44)
at com.example.anilkumar.contactstask.ShowContacts.onCreate(ShowContacts.java:34)
at android.app.Activity.performCreate(Activity.java:4466)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
Another logact error
06-28 13:14:40.552 11252-11261/com.example.anilkumar.contactstask E/SQLiteDatabase: close() was never explicitly called on database '/data/data/com.example.anilkumar.contactstask/databases/contactDB'
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1043)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1036)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:761)
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:215)
at com.example.anilkumar.contactstask.ShowContacts.openDatabase(ShowContacts.java:66)
at com.example.anilkumar.contactstask.ShowContacts.onCreate(ShowContacts.java:33)
at android.app.Activity.performCreate(Activity.java:4466)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
Just update
String read_query = "select * from"+ ContactsTask.ContactsEntry.TABLE_NAME;
to
String read_query = "select * from "+ ContactsTask.ContactsEntry.TABLE_NAME;
Always close cursor. update your initializeData method
public void initializeData() {
try {
contactsList = new ArrayList<>();
try{
c = db.rawQuery(read_query,null);
c.moveToFirst();
while (!c.isLast())
{
names = c.getString(0);
phone = c.getInt(1);
email = c.getString(2);
address = c.getString(3);
contactsList.add(new Contacts(names,phone,email,address));
}
c.isLast();
names = c.getString(0);
phone = c.getInt(1);
email = c.getString(2);
address = c.getString(3);
contactsList.add(new Contacts(names,phone,email,address));
} catch (Exception e) {
// exception handling
} finally {
if(c != null){
c.close();
}
}
}
I wanted to test my code on another device than my physical one, so I launched the app in GenyMotion emulator with a Samsung Galaxy S3 (API 18), but it keeps throwing a NoClassDefFoundError Exception in my class "SlidingMenuUtil" (a customized drawer menu) which is called on startup by my MainActivity.
Here is code from my onCreate in MainActivity:
#Bind(R.id.viewContentFullScreen) RelativeLayout viewContentFullScreen;
#Bind(R.id.viewContentTopBar) RelativeLayout viewContentTopBar;
#Bind(R.id.topBarWrapper) RelativeLayout topbarView;
private ViewContainer viewContainer;
private SlidingMenuUtil leftMenu;
private Bundle bundle;
private MessageHandler messageHandler;
private GoBackFunction currentGoBackFunction;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = savedInstanceState;
setContentView(R.layout.activity_main);
leftMenu = new SlidingMenuUtil(this, SlidingMenuUtil.MenuType.LEFT, R.layout.drawer_menu, (int)(LayoutUtil.getScreenWidth(this) * 0.75), false);
populateMenu();
ButterKnife.bind(this);
messageHandler = new MessageHandler(this, findViewById(R.id.spinner));
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
}
The problem occurs in the SlidingMenuUtil class on line: 67. Here is the constructor for the class:
public SlidingMenuUtil(Activity activity, MenuType menuType, int menuLayout, int shownMenuWidth, boolean fadeEffectOn) {
this.activity = activity;
this.menuType = menuType;
this.shownMenuWidth = shownMenuWidth;
this.fadeEffectOn = fadeEffectOn;
this.screenWidth = LayoutUtil.getScreenWidth(activity);
this.rootView = (ViewGroup)((ViewGroup)activity.findViewById(android.R.id.content)).getChildAt(0);
this.activityWrapper = new RelativeLayout(activity);
this.activityWrapper.setLayoutParams(this.rootView.getLayoutParams());
this.overlay = new RelativeLayout(activity);
this.overlay.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
this.overlay.setBackgroundColor(Color.parseColor("#000000"));
this.overlay.setAlpha(0.0F);
this.overlay.setVisibility(View.GONE);
this.overlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (menuOpen) {
toggle(new ToggleMenu() {
#Override
public void animationDone() {
}
});
}
}
});
this.rootView.addView(this.overlay);
this.menu = (LinearLayout)activity.getLayoutInflater().inflate(menuLayout, (ViewGroup) null, false);
this.menu.setLayoutParams(new ViewGroup.LayoutParams(shownMenuWidth, -1));
if (menuType == MenuType.LEFT) {
this.menu.setTranslationX((float)(-shownMenuWidth));
} else {
this.menu.setTranslationX((float)(screenWidth));
}
this.rootView.addView(this.menu);
this.menuOpen = false;
}
lin 67 is:
this.overlay.setOnClickListener(new View.OnClickListener() {
As mentioned before the problem only occurs in the emulator.
Here is the log:
812-812/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: nightout.dk.nightoutandroid.utils.SlidingMenuUtil$1
at nightout.dk.nightoutandroid.utils.SlidingMenuUtil.<init>(SlidingMenuUtil.java:67)
at nightout.dk.nightoutandroid.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
I hope somone can help me out.
Any help will be gratly appreciated
Hi i have to develop the app is insert the database from spinner in mysql database via soap webserices in android application...
i have use below webservice code:
public class Insertion {
public String insertData(String userName,String userPassword){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/androidlogin","root","");
PreparedStatement statement = con.prepareStatement("INSERT INTO user(status) VALUES ('"+userName+"');");
int result = statement.executeUpdate();
}
catch(Exception exc){
System.out.println(exc.getMessage());
}
return "Insertion successfull!!";
}
}
i have use below code for android spinner example:
public class InsertionExample extends Activity{
private final String NAMESPACE = "http://xcart.com";
private final String URL = "http://192.168.1.168:8085/XcartLogin/services/Insertion?wsdl";
private final String SOAP_ACTION = "http://xcart.com/insertData";
private final String METHOD_NAME = "insertData";
Button btninsert;
private Spinner spnMusketeers;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btninsert = (Button)findViewById(R.id.btn_insert);
btninsert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
insertValues();
}
});
}
public void insertValues(){
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
Spinner userName = (Spinner) findViewById(R.id.spnMusketeers);
List<String> lsMusketeers = new ArrayList<String>();
lsMusketeers.add("Q");
lsMusketeers.add("P");
lsMusketeers.add("C");
ArrayAdapter<String> aspnMusketeers =
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
lsMusketeers);
aspnMusketeers.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
spnMusketeers.setAdapter(aspnMusketeers);
// Set up a callback for the spinner
spnMusketeers.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onNothingSelected(AdapterView<?> arg0) { }
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
// Code that does something when the Spinner value changes
}
});
String user_Name = userName.getContext().toString();
//Pass value for userName variable of the web service
PropertyInfo unameProp =new PropertyInfo();
unameProp.setName("userName");//Define the variable name in the web service method
unameProp.setValue(user_Name);//Define value for fname variable
unameProp.setType(String.class);//Define the type of the variable
request.addProperty(unameProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
TextView result = (TextView) findViewById(R.id.textView2);
result.setText(response.toString());
}
catch(Exception e){
}
// Code that does something when the Spinner value changes
}
}
Here how is change my code for this question.
my logcat window says:
08-23 02:48:40.030: D/AndroidRuntime(4055): Shutting down VM
08-23 02:48:40.030: W/dalvikvm(4055): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-23 02:48:40.060: E/AndroidRuntime(4055): FATAL EXCEPTION: main
08-23 02:48:40.060: E/AndroidRuntime(4055): java.lang.NullPointerException
08-23 02:48:40.060: E/AndroidRuntime(4055): at com.android.soap.InsertionExample.insertValues(InsertionExample.java:63)
08-23 02:48:40.060: E/AndroidRuntime(4055): at com.android.soap.InsertionExample$1.onClick(InsertionExample.java:43)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.view.View.performClick(View.java:2408)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.view.View$PerformClick.run(View.java:8816)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.os.Handler.handleCallback(Handler.java:587)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.os.Looper.loop(Looper.java:123)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 02:48:40.060: E/AndroidRuntime(4055): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 02:48:40.060: E/AndroidRuntime(4055): at java.lang.reflect.Method.invoke(Method.java:521)
08-23 02:48:40.060: E/AndroidRuntime(4055): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-23 02:48:40.060: E/AndroidRuntime(4055): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-23 02:48:40.060: E/AndroidRuntime(4055): at dalvik.system.NativeStart.main(Native Method)
please help me....what error is occurred here.give me solution...
use this code for this insert function:
String selectedItem = parent.getItemAtPosition(pos).toString();
I can't seem to trace the culprit of this bug. Here's my whole activity:
public class CalendarActivity extends Activity implements OnClickListener {
private GestureDetector mGestureDetector;
private ProgressDialog mProgressDialog;
private int mGridWidth, mGridHeight;
private MonthlyCalendarWidget mMonthlyCalendar;
private Button mNext;
private Button mPrev;
private CalendarGridAdapter mDayGridAdapter;
private CalendarEventsListAdapter mEventsListAdapter;
private CalendarAssignmentsListAdapter mAssignmentsListAdapter;
private Calendar mCalendarSource;
private String mMonthId;
private int mMonth, mYear;
private CalendarDbAdapter mCalendarDb;
private CalendarSQLiteAdapter mCalendarSQL;
private CalendarEventData mCalendarEvent;
private ArrayList<CalendarEventData> mEventsList;
private CalendarAssignmentData mCalendarAssignment;
private ArrayList<CalendarAssignmentData> mAssignmentsList;
public ArrayList<CalendarEventData> getCalendarEventData() throws Exception {
ArrayList<CalendarEventData> eventsList = CalendarDbAdapter
.getCalendarEvent("2011", 9, 36, "ParentAndroidSerpong");
return eventsList;
}
public void cacheCalendarEventData(ArrayList<CalendarEventData> data) {
for (int i = 0; i < data.size(); i++) {
mCalendarSQL.addEvents(data.get(i));
}
}
private SimpleCursorAdapter fillEventData() {
Cursor c = mCalendarSQL.fetchAllEvents();
startManagingCursor(c);
String[] from = new String[] { CalendarSQLiteAdapter.KEY_EVENTS_NAME, CalendarSQLiteAdapter.KEY_EVENTS_START_DATE };
int[] to = new int[] { R.id.monthlylistchip_texttop, R.id.monthlylistchip_textbottom };
return new SimpleCursorAdapter(this, R.layout.monthly_listchip, c, from, to);
}
public ArrayList<CalendarAssignmentData> getCalendarAssignmentData()
throws Exception {
ArrayList<CalendarAssignmentData> assignmentList = CalendarDbAdapter
.getCalendarAssignment("0970001931", "2011", "4", 36,
"ParentAndroidSerpong");
return assignmentList;
}
public void cacheCalendarAssignmentData(
ArrayList<CalendarAssignmentData> data) {
for (int i = 0; i < data.size(); i++) {
mCalendarSQL.addAssignments(data.get(i));
}
}
private void updateView(int month, int year) {
mDayGridAdapter = new CalendarGridAdapter(this,
R.id.monthly_gridchip_date, mGridWidth, mGridHeight, month,
year);
mEventsListAdapter = new CalendarEventsListAdapter(this,
R.layout.monthly_listchip, mEventsList);
mAssignmentsListAdapter = new CalendarAssignmentsListAdapter(this,
R.layout.monthly_listchip, mAssignmentsList);
mCalendarSource.set(year, month - 1,
mCalendarSource.get(Calendar.DAY_OF_MONTH));
mMonthId = (String) DateUtils.getFormattedDateString(
mCalendarSource.getTime(), DateUtils.MONTH_AND_YEAR);
mMonthlyCalendar.instantiate(this, mMonthId, mDayGridAdapter,
fillEventData(), mAssignmentsListAdapter);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGridHeight = ScreenUtils.getDisplayHeight(this) / 10;
mGridWidth = ScreenUtils.getDisplayWidth(this) / 7;
mNext = (Button) findViewById(R.id.monthlyview_header_monthnext);
mNext.setOnClickListener(this);
mPrev = (Button) findViewById(R.id.monthlyview_header_monthprev);
mPrev.setOnClickListener(this);
mCalendarSQL = new CalendarSQLiteAdapter(this);
mCalendarSQL.open();
try {
cacheCalendarEventData(getCalendarEventData());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCalendarSource = Calendar.getInstance(Locale.getDefault());
mMonth = mCalendarSource.get(Calendar.MONTH) + 1;
mYear = mCalendarSource.get(Calendar.YEAR);
updateView(mMonth, mYear);
}
#Override
public void onClick(View v) {
if (v == mPrev) {
if (mMonth <= 1) {
mMonth = 12;
mYear--;
} else {
mMonth--;
}
updateView(mMonth, mYear);
} else if (v == mNext) {
if (mMonth > 11) {
mMonth = 1;
mYear++;
} else {
mMonth++;
}
updateView(mMonth, mYear);
}
}
}
This is the LogCat entries of the error:
07-03 09:58:29.112: E/AndroidRuntime(5185): FATAL EXCEPTION: main
07-03 09:58:29.112: E/AndroidRuntime(5185): java.lang.RuntimeException: Unable to start activity ComponentInfo{stub.binusitdirectorate.calendar/stub.binusitdirectorate.calendar.controller.CalendarActivity}: java.lang.NullPointerException
07-03 09:58:29.112: E/AndroidRuntime(5185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-03 09:58:29.112: E/AndroidRuntime(5185): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-03 09:58:29.112: E/AndroidRuntime(5185): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-03 09:58:29.112: E/AndroidRuntime(5185): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-03 09:58:29.112: E/AndroidRuntime(5185): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 09:58:29.112: E/AndroidRuntime(5185): at android.os.Looper.loop(Looper.java:123)
07-03 09:58:29.112: E/AndroidRuntime(5185): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-03 09:58:29.112: E/AndroidRuntime(5185): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 09:58:29.112: E/AndroidRuntime(5185): at java.lang.reflect.Method.invoke(Method.java:521)
07-03 09:58:29.112: E/AndroidRuntime(5185): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-03 09:58:29.112: E/AndroidRuntime(5185): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-03 09:58:29.112: E/AndroidRuntime(5185): at dalvik.system.NativeStart.main(Native Method)
07-03 09:58:29.112: E/AndroidRuntime(5185): Caused by: java.lang.NullPointerException
07-03 09:58:29.112: E/AndroidRuntime(5185): at stub.binusitdirectorate.calendar.controller.CalendarActivity.updateView(CalendarActivity.java:117)
07-03 09:58:29.112: E/AndroidRuntime(5185): at stub.binusitdirectorate.calendar.controller.CalendarActivity.onCreate(CalendarActivity.java:151)
07-03 09:58:29.112: E/AndroidRuntime(5185): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-03 09:58:29.112: E/AndroidRuntime(5185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-03 09:58:29.112: E/AndroidRuntime(5185): ... 11 more
All I know is that the code breaks when I called this:
mMonthlyCalendar.instantiate(this, mMonthId, mDayGridAdapter,
mEventsListAdapter, mAssignmentsListAdapter);
That method is supposed to bind adapters to my custom view.
All of the required variables are supplied, so why did the system gave me a NullPointerException?