Simple android app always crashes - android

Im newbie at developing Android apps but even the simpliest app always crashes when I try to add click listener I tried to google my problem but with no success. I also tried to change API to different versions at new project screen. Im able to run Hello World app but when I try to add listener to my app then Im no longer able to run it. Here is my error log.
03-02 21:13:49.153 19700-19700/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4977)
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: java.lang.NullPointerException
at com.example.app.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:4538)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
 at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4977)
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)
03-02 21:13:49.163 1598-1922/? E/EmbeddedLogger﹕ App crashed! Process: com.example.app
03-02 21:13:49.163 1598-1922/? E/EmbeddedLogger﹕ App crashed! Package: com.example.app v1 (1.0)
03-02 21:13:49.163 1598-1922/? E/EmbeddedLogger﹕ Application Label: My Application 7
03-02 21:13:51.566 19729-19729/? E/ActivityThread﹕ Failed to find provider info for com.google.android.gallery3d.GooglePhotoProvider
03-02 21:13:56.871 19959-19959/? E/ActivityThread﹕ Failed to find provider info for com.google.android.gallery3d.GooglePhotoProvider
And here only piece of code where I changed something (I added button in design)
public class MainActivity extends ActionBarActivity {
Button btn = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setText("Hi");
}
});
}

According to your comment on the last answer, you do not import any R file.
You need to import it this way:
import com.<package_name>.<app_name>.R;
That will import the ids generated file available into your Java code and the Button id button will be visible.

Look at the line 32 of the file MainActivity.java. Sounds like you're trying to call a method on a null object.
For example, are you sure your layout activity_main.xml contains a Button with an id R.id.button?

The problem is either that R.id.button doesn't exist in your activity_main.xml layout, or that there is something null in this code:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Try them both out separately and figure out which one isn't working. Go from there.
By the way, the NullPointerException occurs on line 32.

Related

actionBar.setDisplayHomeAsUpEnabled(true); Crashes Android Application When Using Android Studio

I Recently Upgraded From Eclipse To Android Studio...
I Imported My App Into Android Studio And When I Try To Run My App Through ADB, My App Crashes..
I Removed actionBar.setDisplayHomeAsUpEnabled(true); From An Activity And That Activity Didn't Crash..
I Wasn't Facing This Error When I Was Using Eclipse...
Here Is The Logcat Log :
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/com.test.test.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.test.test.MainActivity.onCreate(MainActivity.java:33)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
At The 33rd Line In Main Activity I'm Setting The setDisplayHomeAsUpEnabled To True..
And The Theme Of MainActivity Is Not Set To Theme.NoTitleBar...
Edit : I Tried Importing More Apps But I Got The Same Error...All Of The Apps Worked With Eclipse..
Edit : Code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true); // Line 33

Why do I get this error when I call an activity from an other?

I have a main activity and I call a second "demo from android studio" activity when I click on a button of the first activity like this :
public void createNetworkButtonClicked (View view) {
Intent intent = new Intent(this, WiFiDirectActivity.class); // that works
startActivity(intent); // that does not work
}
I get this error :
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3698)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17337)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
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:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
Thanks for your help
There can be possibly two reasons for it.
1) Check your method name in onClick attribute of XML file. It should match with the name of the method in java.
2) Create an activity instead of creating individual XML file and Java file for the second class. You can create Activity by right clicking on app folder-->new-->activity-->empty activity.

Android fragment cannot be cast to android.app.Activity

I got an error reported from a user the other day, but how it happened is boggling my mind.
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.inthub.greenfly/com.inthub.greenfly.view.fragment.MyProfileFragment}: java.lang.ClassCastException: com.inthub.greenfly.view.fragment.MyProfileFragment cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.ClassCastException: com.inthub.greenfly.view.fragment.MyProfileFragment cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java)
I know your first thought is "you must be starting up the fragment with startActivity or something". I start up this fragment in one single place:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment, new MyProfileFragment());
ft.commit();
I have been unable to figure out how this was started as an activity and also have not been able to reproduce it. Any ideas where to continue looking?
My class extends Fragment and not FragmentActivity.
I have an onCreate, I've removed a little of the code to keep things short:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getActivity().getActionBar() != null) {
getActivity().getActionBar().setTitle(R.string.myprofile);
}
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my_profile, container, false);
.... tons of lines setting up my view ....
}
I am declaring my fragments in the manifest file as activities. I am reading that it may be incorrect to do this. If that is the source of the problem why am I unable to reproduce the error across multiple devices?
<activity
android:name="com.inthub.greenfly.view.fragment.MyProfileFragment"
android:screenOrientation="portrait" >
</activity>
Update, just to be clear. I have had the incorrect activity lines in my manifest for quite some time and have never encountered this error. It was reported through Crashlytics from another user. I have spent hundreds of hours developing and testing across multiple devices and not encountered it. If it is as simple as removing the lines, why am I unable to reproduce it?

Android Problem on Widget ImageButton click

I'm new in Android development, and my English is awfull.
I have an application with a homescreen widget.
When I start application, in main activity, I can select options for widget (these options are saved to a DB).
Then, I add a Widget, that it have an ImageButton on it, to the homescreen. The click on ImageButton works fine.
If I kill the application activity with some Task Manager, delete the widget, and add it again. The click on ImageButton crash.
Is this normally?
Here you have the error:
java.lang.RuntimeException: Unable to start service cat.aat.quoteswidget.Widget$UpdateService#45d1b188 with Intent { cmp=cat.aat.quoteswidget/.Widget$UpdateService }: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3282)
at android.app.ActivityThread.access$3600(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2211)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at cat.aat.quoteswidget.Widget$UpdateService.buildUpdate(Widget.java:130)
at cat.aat.quoteswidget.Widget$UpdateService.onStart(Widget.java:90)
at android.app.Service.onStartCommand(Service.java:420)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3267)
... 10 more
Thanks!
You have a NullPointerException on line 130 of your Widget.java file, in the buildUpdate() method of your UpdateService.

Unable to bind onClick xml onto a method in my activity

I've got the following Button declared in my main.xml
<Button android:name="#+id/clickedStartService"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="clickedStartService"
android:text="Start"
/>
<Button android:name="#+id/clickedStopService"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="clickedStopService"
android:text="Stop"
/>
And I have the following methods in the activity..
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void clickedStartService(View v)
{
startService(new Intent(this, LocalService.class));
}
public void clickedStopService(View v)
{
stopService(new Intent(this, LocalService.class));
}
I'm getting the following error when I click the button. I have tried rebuilding the project in my IDE and have also restarted the emulator, but same error re-appears. I've followed the documentation on the Android developers site, and I've had this working in a separate project, I just can't get it to work here..
ERROR/AndroidRuntime(335): FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find a method clickedStartService(View) in the activity class com.jameselsey.observerpattern.MyApp for onClick handler on view class android.widget.Button
at android.view.View$1.onClick(View.java:2059)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException
at java.lang.Class.getDeclaredMethods(Native Method)
at java.lang.ClassCache.getDeclaredPublicMethods(ClassCache.java:166)
at java.lang.ClassCache.getDeclaredMethods(ClassCache.java:179)
at java.lang.ClassCache.findAllMethods(ClassCache.java:249)
at java.lang.ClassCache.getFullListOfMethods(ClassCache.java:223)
at java.lang.ClassCache.getAllPublicMethods(ClassCache.java:204)
at java.lang.Class.getMethod(Class.java:984)
at android.view.View$1.onClick(View.java:2052)
... 11 more
XML click listeners were added in Android 1.6. Check your AndroidManifest to verify that your min SDK version isn't set to cupcake (1.5, or API level 3) -
If you're using Eclipse with the ADT plugin, also check project properties > Android and check the build target, make sure it's Donut or higher.
You need to do:
import android.view.View
instead of:
import view.View

Categories

Resources