main.java
package com.learnactivities;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Main" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
</RelativeLayout>
Second.java
package com.learnactivities;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Second" >
<TextView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="15dp"
android:text="#string/nd" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnactivities"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.learnactivities.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".second" />
</application>
</manifest>
I m getting following errors. I can see that error caused by java.lang.NullPointerException. I did my best searching here and there but could not get to the solution.Plz, anyone help me sort this problem out?
08-25 19:08:44.442: D/AndroidRuntime(276): Shutting down VM
08-25 19:08:44.442: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-25 19:08:44.472: E/AndroidRuntime(276): FATAL EXCEPTION: main
08-25 19:08:44.472: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learnactivities/com.learnactivities.Main}: java.lang.NullPointerException
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-25 19:08:44.472: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
08-25 19:08:44.472: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException
08-25 19:08:44.472: E/AndroidRuntime(276): at com.learnactivities.Main.onCreate(Main.java:19)
08-25 19:08:44.472: E/AndroidRuntime(276): at `android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)`
`08-25 19:08:44.472: E/AndroidRuntime(276): at` `android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)`
08-25 19:08:44.472: E/AndroidRuntime(276): ... 11 more
R.id.button1 is declared inside second.xml, but you are looking for it inside activity.xml. So in Main, when you do
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
findViewById is returning null
Try this in your main class..
TextView b = (TextView ) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
and also in manifest change like below line
<activity android:name=".Second" />
Related
I've been doing some research of this problem but no solution has yet to work for me. I've added empty constructors in the fragment classes and tried with different imports of fragments, but nothing seems to work. I hope you guys can help me!
My Activity:
package com.example.com.example.android.rssfeed;
import android.app.Activity;
import android.os.Bundle;
public class RssfeedActivity extends Activity implements MyListFragment.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rssfeed);
}
public void onRssItemSelected(String link) {
DetailFragment fragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(link);
}//end if
}//end method onRssItemSelected
}//end class
My fragment:
package com.example.com.example.android.rssfeed;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MyListFragment extends Fragment {
public MyListFragment() {
}
private OnItemSelectedListener listener;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rsslist_overview, container, false);
Button button = (Button) view.findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateDetail();
}//end onClick
});
return view;
}//end onCreateView
public interface OnItemSelectedListener {
public void onRssItemSelected(String link);
}//end onItemSelectedListener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ "Must implement MyListFragment.OnItemSelectedListener");
}//end else
}//end onAttach
//May also be triggered from the activity
public void updateDetail() {
//Create fake data
String newTime = String.valueOf(System.currentTimeMillis());
// Send data to Activity
listener.onRssItemSelected(newTime);
}//end method updateDetail
}//end class
My fragment XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.example.android.rssfeed.MyListFragment"
android:layout_marginTop="?android:attr/actionBarSize"></fragment>
<fragment
android:id="#+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.android.rssfeed.DetailFragment"></fragment>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.example.android.rssfeed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.com.example.android.rssfeed.RssfeedActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Button 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" >
<button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to Update">
</button>
</LinearLayout>
LogCat error:
05-17 10:35:01.864: E/AndroidRuntime(6384): FATAL EXCEPTION: main
05-17 10:35:01.864: E/AndroidRuntime(6384): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.example.android.rssfeed/com.example.com.example.android.rssfeed.RssfeedActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.access$700(ActivityThread.java:150)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.os.Looper.loop(Looper.java:137)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.main(ActivityThread.java:5279)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.reflect.Method.invoke(Method.java:511)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-17 10:35:01.864: E/AndroidRuntime(6384): at dalvik.system.NativeStart.main(Native Method)
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:342)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.setContentView(Activity.java:1928)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.example.com.example.android.rssfeed.RssfeedActivity.onCreate(RssfeedActivity.java:11)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.performCreate(Activity.java:5267)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 11 more
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.rssfeed.MyListFragment: make sure class name exists, is public, and has an empty constructor that is public
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:592)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:560)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.onCreateView(Activity.java:4862)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 21 more
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.android.rssfeed.MyListFragment" on path: /data/app/com.example.com.example.android.rssfeed-1.apk
05-17 10:35:01.864: E/AndroidRuntime(6384): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:582)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 24 more
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.example.android.rssfeed.MyListFragment" on path:
/data/app/com.example.com.example.android.rssfeed-1.apk
Change this
class="com.example.android.rssfeed.MyListFragment"
to
class="com.example.com.example.android.rssfeed.MyListFragment"
cause the packagename is package com.example.com.example.android.rssfeed.
Similarly
class="com.example.com.example.android.rssfeed.DetailFragment"
if the package name is the same
Change this
<button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to Update">
To
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to Update" />
Change
"class= com.example.android.rssfeed.MyListFragment"
to
"com.example.com.example.android.rssfeed.MyListFragment"
And your problem will be solved out
Add this line in fragment
class="com.google.android.gms.maps.SupportMapFragment"
Hey im a begginer in android coding and ive been following this series on youtube. Whenever I run my project as an android application and go to the emulator to select it, it gives me "Sorry- The application (Appname) has stopped unexpectedly. Please try again" Eclipse doesnt give me any error line to where the problem is and so it is very frustrating.. Ive been looking on the internet for answers to this, but I havent been able to fix it. I think it has something to do with my Manifest. Whenever i run my application there are errors on my LogCat.
My application is a small app that when selected will first open a logo screen for 3 seconds then it will take you to an interface where there are 2 buttons to choose from. Both buttons lead to this thing that edits text.
Ive posted all my code below and please guys keep in mind when you answer that I am a begginer and might not understand some terms. Thank you!
Here is my LogCat when i try to start it
07-20 20:19:17.382: D/dalvikvm(254): GC_EXTERNAL_ALLOC freed 663 objects / 51912 bytes in 177ms
07-20 20:19:19.791: D/AndroidRuntime(254): Shutting down VM
07-20 20:19:19.801: W/dalvikvm(254): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-20 20:19:19.811: E/AndroidRuntime(254): FATAL EXCEPTION: main
07-20 20:19:19.811: E/AndroidRuntime(254): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.thepasics/com.example.thepasics.Menu}: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.os.Looper.loop(Looper.java:123)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.reflect.Method.invoke(Method.java:521)
07-20 20:19:19.811: E/AndroidRuntime(254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-20 20:19:19.811: E/AndroidRuntime(254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-20 20:19:19.811: E/AndroidRuntime(254): at dalvik.system.NativeStart.main(Native Method)
07-20 20:19:19.811: E/AndroidRuntime(254): Caused by: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
07-20 20:19:19.811: E/AndroidRuntime(254): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
07-20 20:19:19.811: E/AndroidRuntime(254): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-20 20:19:19.811: E/AndroidRuntime(254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-20 20:19:19.811: E/AndroidRuntime(254): ... 11 more
My Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thepasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.thepasics.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thePasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TutorialOne"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thePasics.TUTORIALONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I have 3 Java files which are Menu.java Main.java TutorialOne.java. This one is the Main one
package com.example.thepasics;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class Main extends Activity {
MediaPlayer logoMusic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
logoMusic = MediaPlayer.create(Main.this, R.raw.music);
logoMusic.start();
Thread logoTimer = new Thread(){
public void run(){
try{
sleep(2000);
Intent menuIntent = new Intent("com.example.thePasics.MENU");
startActivity(menuIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
logoMusic.release();
}
}
This One is the menu.java
package com.example.thepasics;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class menu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Button sound
final MediaPlayer buttonSound = MediaPlayer.create(menu.this, R.raw.buttonsound);
//Setting up button references
Button tut1 = (Button) findViewById(R.id.button1);
Button tut2 = (Button) findViewById(R.id.button2);
tut1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.thepasics.TUTORIALONE"));
}
});
tut2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.example.thepasics.TutorialOne"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
The third one is the TutorialOne.java
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class TutorialOne extends Activity implements OnCheckedChangeListener{
TextView textOut;
EditText textIn;
RadioGroup gravityG, styleG;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
textOut = (TextView) findViewById(R.id.tvChange);
textIn = (EditText) findViewById(R.id.editText1);
gravityG = (RadioGroup) findViewById(R.id.rgGravity);
gravityG.setOnCheckedChangeListener(this);
styleG = (RadioGroup) findViewById(R.id.rgStyle);
styleG.setOnCheckedChangeListener(this);
Button gen = (Button) findViewById(R.id.bGenerate);
gen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textOut.setText(textIn.getText());
}
});
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rbLeft:
textOut.setGravity(Gravity.LEFT);
break;
case R.id.rbCenter:
textOut.setGravity(Gravity.CENTER);
break;
case R.id.rbRight:
textOut.setGravity(Gravity.RIGHT);
break;
case R .id.rbNormal:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL), Typeface.NORMAL);
break;
case R .id.rbItalic:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.ITALIC);
break;
case R .id.rbBold:
textOut.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD), Typeface.BOLD);
break;
}
}
}
My 3 xml files are splash, tutorial1 and main
this one is main.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:background="#drawable/backgroundwithoutericapp"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Choose a function" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1"
android:textSize="25dp"
android:textStyle="bold"
android:id="#+id/button1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button2"
android:textSize="25dp"
android:textStyle="bold"
android:id="#+id/button2"/>
</LinearLayout>
second one is splash.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"
android:background="#drawable/background">
</LinearLayout>
third one is tutorial1.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:background="#drawable/tutorialonebackground"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/tvStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Style"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvGravity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Gravity"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<RadioGroup
android:id="#+id/rgStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:id="#+id/rbNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal" />
<RadioButton
android:id="#+id/rbItalic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Italic" />
<RadioButton
android:id="#+id/rbBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bold" />
</RadioGroup>
<RadioGroup
android:id="#+id/rgGravity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:id="#+id/rbLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left" />
<RadioButton
android:id="#+id/rbCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Center" />
<RadioButton
android:id="#+id/rbRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right" />
</RadioGroup>
</LinearLayout>
<TextView
android:id="#+id/tvChange"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Type in Text and Press the Button Below" />
<Button
android:id="#+id/bGenerate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate" />
</LinearLayout>
You have specified wrong class name in manifest.xml
Replace
<activity
android:name="Menu"
android:label="#string/app_name" >
with
<activity
android:name="com.example.thepasics.menu"
android:label="#string/app_name" >
Your Manifest and menu activity don't match. Your manifest has
<activity
android:name="Menu" ...
which first of all isn't a valid name. It could be either android:name=".Menu" to refer to com.com.example.thepasics.Menu or you could use the fully qualified name (as you did for com.example.thepasics.Main).
In addition, your class is named menu, not Menu - remember it is case sensitive. Java convention has class names starting with a capital letter, so it should probably be corrected to Menu.
Your Menu class is actually called "menu" and not "MENU".
public class menu extends Activity{
Try changing "com.example.thePasics.MENU" to "com.example.thePasics.menu" throughout your app.
I only have looked into your Logcat:
07-20 20:19:19.801: W/dalvikvm(254): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-20 20:19:19.811: E/AndroidRuntime(254): FATAL EXCEPTION: main
07-20 20:19:19.811: E/AndroidRuntime(254): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.thepasics/com.example.thepasics.Menu}: java.lang.ClassNotFoundException: com.example.thepasics.Menu in loader dalvik.system.PathClassLoader[/data/app/com.example.thepasics-2.apk]
Your app is crashing becuase your com.example.thepasics.Menu is not being found, and also use android:name=".Menu" instead of android:name="Menu".
I'm Trying to set a different Class for Specs on my TabHost, but i had only some errors (log pasted below)
If i change my champ.setIndicator("Champion").setContent(R.id.tab1); and so on for the other tabs it works.
Could someone help me?
MY code:
TabsMain.java
package com.girardi.lolguides;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabsMain extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs_main);
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
TabSpec champ = tabHost.newTabSpec("champ");
TabSpec equip = tabHost.newTabSpec("item");
champ.setIndicator("Champion").setContent(new Intent(this, Champion.class));
equip.setIndicator("Items").setContent(new Intent(this, Equip.class));
tabHost.addTab(champ);
tabHost.addTab(equip);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tabs_main, menu);
return true;
}
}
Champion.java
package com.girardi.lolguides;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Champion extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* First Tab Content */
TextView textView = new TextView(this);
textView.setText("First Tab");
setContentView(textView);
}
}
Equip.java
package com.girardi.lolguides;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Equip extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Second Tab Content */
TextView textView = new TextView(this);
textView.setText("Second Tab");
setContentView(textView);
}
}
activity_tabs_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".TabsMain" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
LOG:
05-20 13:51:22.890: D/AndroidRuntime(22816): Shutting down VM
05-20 13:51:22.890: W/dalvikvm(22816): threadid=1: thread exiting with uncaught exception (group=0x40e832a0)
05-20 13:51:22.910: E/AndroidRuntime(22816): FATAL EXCEPTION: main
05-20 13:51:22.910: E/AndroidRuntime(22816): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.girardi.lolguides/com.girardi.lolguides.TabsMain}: java.lang.NullPointerException
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.os.Looper.loop(Looper.java:137)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-20 13:51:22.910: E/AndroidRuntime(22816): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 13:51:22.910: E/AndroidRuntime(22816): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 13:51:22.910: E/AndroidRuntime(22816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-20 13:51:22.910: E/AndroidRuntime(22816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-20 13:51:22.910: E/AndroidRuntime(22816): at dalvik.system.NativeStart.main(Native Method)
05-20 13:51:22.910: E/AndroidRuntime(22816): Caused by: java.lang.NullPointerException
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.widget.TabHost.addTab(TabHost.java:243)
05-20 13:51:22.910: E/AndroidRuntime(22816): at com.girardi.lolguides.TabsMain.onCreate(TabsMain.java:23)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.Activity.performCreate(Activity.java:5206)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-20 13:51:22.910: E/AndroidRuntime(22816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-20 13:51:22.910: E/AndroidRuntime(22816): ... 11 more
05-20 13:53:10.655: D/AndroidRuntime(24134): Shutting down VM
05-20 13:53:10.655: W/dalvikvm(24134): threadid=1: thread exiting with uncaught exception (group=0x40e832a0)
05-20 13:53:10.665: E/AndroidRuntime(24134): FATAL EXCEPTION: main
05-20 13:53:10.665: E/AndroidRuntime(24134): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.girardi.lolguides/com.girardi.lolguides.TabsMain}: java.lang.NullPointerException
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.os.Looper.loop(Looper.java:137)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-20 13:53:10.665: E/AndroidRuntime(24134): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 13:53:10.665: E/AndroidRuntime(24134): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 13:53:10.665: E/AndroidRuntime(24134): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-20 13:53:10.665: E/AndroidRuntime(24134): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-20 13:53:10.665: E/AndroidRuntime(24134): at dalvik.system.NativeStart.main(Native Method)
05-20 13:53:10.665: E/AndroidRuntime(24134): Caused by: java.lang.NullPointerException
05-20 13:53:10.665: E/AndroidRuntime(24134): at com.girardi.lolguides.TabsMain.onCreate(TabsMain.java:19)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.Activity.performCreate(Activity.java:5206)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-20 13:53:10.665: E/AndroidRuntime(24134): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-20 13:53:10.665: E/AndroidRuntime(24134): ... 11 more
Your activity should extend TabActivity (which is deprecated) or use Fragments (which is the right way) to use tabs. See TabActivity for more informations and examples.
EDIT: about layout you can use a relativeLayout like this:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/list"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<android.support.v4.app.TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/footerlist"
android:layout_alignParentTop="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
</RelativeLayout>
About MainActivity:
public class MainActivity extends android.support.v4.app.FragmentActivity {
private android.support.v4.app.FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(...);
}
}
i have a very annoying bug that i couldn't figure out what causes it. it seens that i am getting a null pointer exception when trying to start an activity from another activity with an Intent. i tried all possible solutions i could think of,checked the syntax houndreds of times, tried to setContentView with main layout before the new layout..nothing!
I need some urgent help here. I have everything declared in the manifest,and the layout xml files. the problem relies in the ShowNoteActivity,called by the main activity (from onItemClick() method) : I get nullpointerexception from the logcat and bunch of other errors i don't know what they are.
anyway,this is the relevant code,tell me if you need more.
public class MainActivity extends Activity implements OnClickListener ,OnItemClickListener{
public final static String EXTRA_MESSAGE = "assaf.notepad.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseManager db = new DatabaseManager(this);
String[]notes = db.listNotes();
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,notes);
ListView lv = (ListView)findViewById(R.id.notes_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
ImageButton addNoteBtn = (ImageButton)findViewById(R.id.add_note);
addNoteBtn.setOnClickListener(this);
}
#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
public void onClick(View view) {
Intent intent = new Intent(this, AddNoteActivity.class);
startActivity(intent);
}
#Override
public void onItemClick(AdapterView adapter, View view, int position, long id) {
DatabaseManager db = new DatabaseManager(this);
String[]notesContent = db.getNoteContent();
Intent intent = new Intent(this,ShowNoteActivity.class);
intent.putExtra(EXTRA_MESSAGE, notesContent[position]);
startActivity(intent);
}
}
and this is the problematic activity!!
public class ShowNoteActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String text = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView)findViewById(R.id.show_note);
textView.setText(text);
setContentView(R.layout.activity_show_note);
}
}
this is the logcat
02-19 03:18:07.861: E/AndroidRuntime(829): FATAL EXCEPTION: main
02-19 03:18:07.861: E/AndroidRuntime(829): java.lang.RuntimeException: Unable to start activity ComponentInfo{assaf.notepad/assaf.notepad.ShowNoteActivity}: java.lang.NullPointerException
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.os.Looper.loop(Looper.java:137)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-19 03:18:07.861: E/AndroidRuntime(829): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 03:18:07.861: E/AndroidRuntime(829): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 03:18:07.861: E/AndroidRuntime(829): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-19 03:18:07.861: E/AndroidRuntime(829): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-19 03:18:07.861: E/AndroidRuntime(829): at dalvik.system.NativeStart.main(Native Method)
02-19 03:18:07.861: E/AndroidRuntime(829): Caused by: java.lang.NullPointerException
02-19 03:18:07.861: E/AndroidRuntime(829): at assaf.notepad.ShowNoteActivity.onCreate(ShowNoteActivity.java:19)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.Activity.performCreate(Activity.java:5104)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-19 03:18:07.861: E/AndroidRuntime(829): ... 11 more
the relevant xml ,the "ShowNoteActivity" xml ,with the problematic textView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/board"
android:scaleType="fitXY"/>
<Button
android:id="#+id/edit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="#string/edit_btn"
android:textColor="#color/white"/>
<Button
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/edit_btn"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:text="#string/delete_btn"
android:textColor="#color/white"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/paper"
android:layout_below="#id/edit_btn"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="30dp"/>
<TextView
android:id="#+id/show_note"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/edit_btn"
android:layout_marginTop="110dp"
android:layout_marginRight="40dp"
android:layout_marginLeft="45dp"
android:layout_marginBottom="80dp"
android:typeface="sans"
android:textSize="10sp"/>
</RelativeLayou
t>
this is not a database problem or something like that, the problem still exists even if i try to pass a "hello world" string to the new activity. the program always crashes when invoking the new activity.
this is the xml manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="assaf.notepad"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="assaf.notepad.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="assaf.notepad.AddNoteActivity">
</activity>
<activity
android:name="assaf.notepad.ShowNoteActivity">
</activity>
</application>
</manifest>
The problem is that you try to access a TextView before you call setContentView in your ShowNoteActivity. Your code should be:
setContentView(R.layout.activity_show_note);
TextView textView = (TextView)findViewById(R.id.show_note);
textView.setText(text);
try
setContentView(R.layout.activity_show_note);
Intent intent = getIntent();
String text = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView)findViewById(R.id.show_note);
if(textView!=null && text!=null)
{
Log.d("Mytag","Everything is right");
textView.setText(text);
}
else
{
if(textView==null)
Log.d("Mytag","Textview is null");
else
Log.d("Mytag","Text is null");
}
There are 3 buttons on the screen! Start, View Map, Stop
When I click View Map, it should go to a new screen that shows the map! But something goes wrong and the app is getting force closed! I keep getting Could not find class A referenced from method B error. Please please please someone correct it. I've been struck with this for 3 days!!! :'(
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="GPS App"
android:id="#+id/textView1"
android:textSize="40sp"
android:padding="10dp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Button
android:text="Start"
android:id="#+id/buttonStart"
android:textSize="30sp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Button
android:text="View Map"
android:id="#+id/buttonMap"
android:textSize="30sp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Button
android:text="Stop"
android:id="#+id/buttonStop"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
map.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mymap"
android:clickable="true"
android:enabled="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="xxxxxxxxxxxxx"
/>
<LinearLayout
android:id="#+id/myzoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Firstdroid.Gps"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:permission="android.permission.INTERNET"
android:name=".IntentService" android:enabled="true" />
<activity android:name=".MapViewer" android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
MainActivity.java
package Firstdroid.Gps;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* START BUTTON */
Button startButton = (Button) findViewById(R.id.buttonStart);
startButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
Log.d("Firstdroid.Gps", "Starting Exploration..");
// Start Exploration
startService(new Intent(MainActivity.this, GPSexp.class));
}
});
/* MAP BUTTON */
Button mapButton = (Button) findViewById(R.id.buttonMap);
mapButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
Log.d("Firstdroid.Gps", "Loading Map..");
// Loading Google Map View
startService(new Intent(MainActivity.this, MapViewer.class));
}
});
/* STOP BUTTON */
Button stopButton = (Button) findViewById(R.id.buttonStop);
stopButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.d("Firstdroid.Gps", "Stopping Exploration..");
// Stop Exploration
stopService(new Intent(MainActivity.this, GPSexp.class));
}
});
}
}/* End of MainActivity */
The start and stop buttons refer to GPSexp.class defined in GPSexp.java which works correctly. Problem is with MapViewer.java
MapViewer.java
package Firstdroid.Gps;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MapViewer extends MapActivity {
MapView myMap;
MyLocationOverlay myLocOverlay;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
initMap();
initMyLocation();
}
private void initMap() {
myMap = (MapView) findViewById(R.id.mymap);
View zoomView = myMap.getZoomControls();
LinearLayout myzoom = (LinearLayout) findViewById(R.id.myzoom);
myzoom.addView(zoomView);
myMap.displayZoomControls(true);
}
/**
* Initialises the MyLocationOverlay and adds it to the overlays of the map
*/
private void initMyLocation() {
myLocOverlay = new MyLocationOverlay(this, myMap);
myLocOverlay.enableMyLocation();
myMap.getOverlays().add(myLocOverlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Logcat
06-30 04:25:07.519: WARN/dalvikvm(357): Unable to resolve superclass of LFirstdroid/Gps/MapViewer; (37)
06-30 04:25:07.519: WARN/dalvikvm(357): Link of class 'LFirstdroid/Gps/MapViewer;' failed
06-30 04:25:07.547: ERROR/dalvikvm(357): Could not find class 'Firstdroid.Gps.MapViewer', referenced from method Firstdroid.Gps.MainActivity$2.onClick
06-30 04:25:07.547: WARN/dalvikvm(357): VFY: unable to resolve const-class 8 (LFirstdroid/Gps/MapViewer;) in LFirstdroid/Gps/MainActivity$2;
06-30 04:25:07.559: DEBUG/dalvikvm(357): VFY: replacing opcode 0x1c at 0x000d
06-30 04:25:07.573: DEBUG/dalvikvm(357): VFY: dead code 0x000f-0015 in LFirstdroid/Gps/MainActivity$2;.onClick (Landroid/view/View;)V
06-30 04:25:07.909: INFO/ActivityManager(58): Displayed activity Firstdroid.Gps/.MainActivity: 1828 ms (total 1828 ms)
06-30 04:25:13.180: DEBUG/dalvikvm(129): GC_EXPLICIT freed 670 objects / 38560 bytes in 169ms
06-30 04:25:13.840: DEBUG/Firstdroid.Gps(357): Loading Map..
06-30 04:25:13.850: DEBUG/AndroidRuntime(357): Shutting down VM
06-30 04:25:13.850: WARN/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): FATAL EXCEPTION: main
06-30 04:25:13.880: ERROR/AndroidRuntime(357): java.lang.NoClassDefFoundError: Firstdroid.Gps.MapViewer
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at Firstdroid.Gps.MainActivity$2.onClick(MainActivity.java:40)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.view.View.performClick(View.java:2408)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.view.View$PerformClick.run(View.java:8816)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.os.Handler.handleCallback(Handler.java:587)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:521)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-30 04:25:13.880: ERROR/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
06-30 04:25:13.900: WARN/ActivityManager(58): Force finishing activity Firstdroid.Gps/.MainActivity
06-30 04:25:14.450: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44fe90c0 Firstdroid.Gps/.MainActivity}
06-30 04:25:15.750: INFO/Process(357): Sending signal. PID: 357 SIG: 9
06-30 04:25:15.841: INFO/ActivityManager(58): Process Firstdroid.Gps (pid 357) has died.
06-30 04:25:15.880: INFO/WindowManager(58): WIN DEATH: Window{4500c988 Firstdroid.Gps/Firstdroid.Gps.MainActivity paused=false}
You added uses-library as child of the activity element... maybe that's the problem.
This is not a standard package in the
Android library. In order to use it,
you must add the following XML
element, as a child of the
application element, in your AndroidManifest.xml file:
<uses-library android:name="com.google.android.maps" />
You're using startService to start your MapViewer activity. Hope that helps