Tuesday, December 20, 2011

Why isn't my Android program working?

I am new to programming for the Android and am trying to write a relatively simple math program. I tried running the program in an emulator, but it hits an error upon start up. This happened after I added 2 other activities. I also changed the first activity in the manifest file (I decided I wanted a different activity to appear first). Could the problem lie in the manifest file, or is there some confusion with the spinner and the startActiviy? I have absolutely no idea what is wrong. Perhaps someone could walk me through using Eclipse's debugger or tell me what my error is (if its simple to see).





Here is the manifest.xml:


%26lt;?xml version="1.0" encoding="utf-8"?%26gt;


%26lt;manifest xmlns:android="http://schemas.android.co鈥?br>

package="com.math.mathapp"


android:versionCode="1"


android:versionName="1.0"%26gt;


%26lt;uses-sdk android:minSdkVersion="8" /%26gt;





%26lt;application android:icon="@drawable/icon" android:label="@string/app_name"%26gt;


%26lt;activity android:label="@string/app_name" android:name=".MainActivity.Main"%26gt; //This is what I changed


%26lt;intent-filter%26gt;


%26lt;action android:name="android.intent.action.MAIN鈥?/%26gt;


%26lt;category android:name="android.intent.category.LA鈥?/%26gt;


%26lt;/intent-filter%26gt;


%26lt;/activity%26gt;


%26lt;activity android:name=".Formulas"%26gt;%26lt;/activity%26gt;


%26lt;activity android:name=".Parabola"%26gt;%26lt;/activity%26gt;





%26lt;/application%26gt;


%26lt;/manifest%26gt;





Here is the Main.java:





public class Main extends Activity {


/** Called when the activity is first created. */





@Override


public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.main);





Spinner spinner1 = (Spinner) findViewById(R.id.spinnermain);


ArrayAdapter%26lt;CharSequence%26gt; adapter = ArrayAdapter.createFromResource(


this, R.array.program_array, android.R.layout.simple_spinner_item);


adapter.setDropDownViewResource(android.鈥?br>

spinner1.setAdapter(adapter);





spinner1.setOnItemSelectedListener(new programspinner());


}





public class programspinner implements OnItemSelectedListener {


public void onItemSelected(AdapterView%26lt;?%26gt; parent,


View view, int pos, long id) {


Intent intent = null;


String program;


program = parent.getItemAtPosition(pos).toString()鈥?br>

if(program=="Formula")


intent = new Intent(view.getContext(), Formulas.class);


if(program=="Parabola")


intent = new Intent(view.getContext(), Parabola.class);


startActivity(intent);


}


public void onNothingSelected(AdapterView parent) {


// Do nothing.


}


}


}








And here is the main.xml:





%26lt;?xml version="1.0" encoding="utf-8"?%26gt;


%26lt;LinearLayout


xmlns:android="http://schemas.android.co鈥?br>

android:orientation="vertical"


android:layout_width="fill_parent"


android:layout_height="fill_parent"


android:background="@drawable/blackbackg鈥?br>

android:padding="10dip"


%26gt;


%26lt;ImageView android:src="@drawable/title"


android:layout_width="wrap_content"


android:id="@+id/imageView1"


android:layout_height="wrap_content"


android:layout_gravity="top"%26gt;%26lt;/ImageView鈥?br>




%26lt;Spinner android:layout_width="match_parent"


android:id="@+id/spinnermain"


android:layout_height="wrap_content"


%26gt;%26lt;/Spinner%26gt;


%26lt;/LinearLayout%26gt;|||My first guess is that the line





android:name=".MainActivity.Main"





Should be android:name=".Main" as that is the name of the activity that you want Android to start.





When you are starting your application, switch to the DDMS view and that may have more information on why your application is not starting.

No comments:

Post a Comment