Unable to start activity ComponentInfo NullpointerException -- - java

I wanted to make an app where you can make pictures and save them, but when I test it I got this error in my logcat: "java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.random/com.example.random.FotoMaker}: java.lang.NullPointerException". I think the problem has something to do with the intent, but im not sure what I need to add/change in my FotoMaker.java.
MenuScreen.java:
package com.example.random;
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.view.Window;
import android.widget.Button;
public class MenuScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("DEBUG", "test");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
startActivityForResult(intent, 0);
}
});
findViewById(R.test1).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test1");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
}
});
findViewById(R.test2).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test2");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
}
});
findViewById(R.id.verlaat_app).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test3");
MenuScreen.this.finish();
}
});
}
}
MenuScreen.java:
package com.example.random;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class FotoMaker extends Activity
{
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.testpic);
iv = (ImageView) findViewById(R.id.imageView);
Button btn = (Button) findViewById(R.id.testpic);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick (View v){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 0)
{
Bitmap theImage = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(theImage);
}
}
}
LogCat:
11-16 18:30:14.366: E/AndroidRuntime(1522): FATAL EXCEPTION: main
11-16 18:30:14.366: E/AndroidRuntime(1522): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.random/com.example.random.FotoMaker}: java.lang.NullPointerException
11-16 18:30:14.366: E/AndroidRuntime(1522): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1996)
11-16 18:30:14.366: E/AndroidRuntime(1522): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2023)
11-16 18:30:14.366: E/AndroidRuntime(1522): at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-16 18:30:14.366: E/AndroidRuntime(1522): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1174)
11-16 18:30:14.366: E/AndroidRuntime(1522): at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 18:30:14.366: E/AndroidRuntime(1522): at android.os.Looper.loop(Looper.java:137)
11-16 18:30:14.366: E/AndroidRuntime(1522): at android.app.ActivityThread.main(ActivityThread.java:4503)
11-16 18:30:14.366: E/AndroidRuntime(1522): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 18:30:14.366: E/AndroidRuntime(1522): at java.lang.reflect.Method.invoke(Method.java:511)
11-16 18:30:14.366: E/AndroidRuntime(1522): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
11-16 18:30:14.366: E/AndroidRuntime(1522): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
11-16 18:30:14.366: E/AndroidRuntime(1522): at dalvik.system.NativeStart.main(Native Method)
11-16 18:30:14.366: E/AndroidRuntime(1522): Caused by: java.lang.NullPointerException
11-16 18:30:14.366: E/AndroidRuntime(1522): at com.example.random.FotoMaker.onCreate(FotoMaker.java:27)
11-16 18:30:14.366: E/AndroidRuntime(1522): at android.app.Activity.performCreate(Activity.java:4479)
11-16 18:30:14.366: E/AndroidRuntime(1522): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
11-16 18:30:14.366: E/AndroidRuntime(1522): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
11-16 18:30:14.366: E/AndroidRuntime(1522): ... 11 more
testpic.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" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_launcher" />
</LinearLayout>

It seems that you forget to add testpic button id in your xml layout,Therefore in FotoMaker activity your button btn is null.
Button btn = (Button) findViewById(R.id.testpic); <-- null here
So add button view with id testpic in your testpic.xml layout file.

Button btn = (Button) findViewById(R.id.testpic);
there is no Button called testpic in the layout "testpic"
you need to add a button testpic indide your layout file , otherwise it will return null.

Related

java.lang.RuntimeException: Unable to start activity ComponentInfo:android.view.InflateException

I am trying to create an Image Slider containing 4 images for my application and i've been running into the following errors. I've tried searching and have found similiar titles but the problems are different.
This is the logcat:
example.prgguru.com.wallit E/AndroidRuntime: FATAL EXCEPTION: main
Process: example.prgguru.com.wallit, PID: 32048
java.lang.RuntimeException: Unable to start activity ComponentInfo{example.prgguru.com.wallit/example.prgguru.com.wallit.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:2059)
at example.prgguru.com.wallit.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class example.prgguru.com.wallit.MainActivityFragment that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:585)
at android.app.Fragment.instantiate(Fragment.java:561)
at android.app.Activity.onCreateView(Activity.java:5066)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:2059)
at example.prgguru.com.wallit.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException
at android.app.Fragment.instantiate(Fragment.java:585)
at android.app.Fragment.instantiate(Fragment.java:561)
at android.app.Activity.onCreateView(Activity.java:5066)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:2059)
at example.prgguru.com.wallit.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
This is my Main Activity.java:
package example.prgguru.com.wallit;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:name="example.prgguru.com.wallit.MainActivityFragment"
android:id="#+id/fragment"
tools:layout="#layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#32383d">
<!-- ViewPager -->
<!-- Footer -->
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageviewPager" />
<include layout="#layout/footer"/>
</FrameLayout>
MainActivityFragment.java
package example.prgguru.com.wallit;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivityFragment extends Fragment {
private ViewPager _mViewPager;
private ImageViewPagerAdapter _adapter;
private ImageView _btn1, _btn2, _btn3,_btn4;
public MainActivityFragment() {
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setUpView();
setTab();
onCircleButtonClick();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
private void onCircleButtonClick() {
_btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn1.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(0);
}
});
_btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn2.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(1);
}
});
_btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn3.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(2);
}
});
_btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_btn4.setImageResource(R.drawable.fill_circle);
_mViewPager.setCurrentItem(3);
}
});
}
private void setUpView() {
_mViewPager = (ViewPager) getView().findViewById(R.id.imageviewPager);
_adapter = new ImageViewPagerAdapter(getActivity(), getFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
initButton();
}
private void setTab() {
_mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int position) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
_btn1.setImageResource(R.drawable.holo_circle);
_btn2.setImageResource(R.drawable.holo_circle);
_btn3.setImageResource(R.drawable.holo_circle);
_btn4.setImageResource(R.drawable.holo_circle);
btnAction(position);
}
});
}
private void btnAction(int action) {
switch (action) {
case 0:
_btn1.setImageResource(R.drawable.fill_circle);
break;
case 1:
_btn2.setImageResource(R.drawable.fill_circle);
break;
case 2:
_btn3.setImageResource(R.drawable.fill_circle);
break;
case 3:
_btn4.setImageResource(R.drawable.fill_circle);
break;
}
}
private void initButton() {
_btn1 = (ImageView) getView().findViewById(R.id.btn1);
_btn1.setImageResource(R.drawable.fill_circle);
_btn2 = (ImageView) getView().findViewById(R.id.btn2);
_btn3 = (ImageView) getView().findViewById(R.id.btn3);
_btn4 = (ImageView) getView().findViewById(R.id.btn4);
}
private void setButton(Button btn, String text, int h, int w) {
btn.setWidth(w);
btn.setHeight(h);
btn.setText(text);
}
}
Your Activity extends from Activity and not AppCompatActivity. But your fragment extends from android.support.v4.app.Fragment. You must use one type of fragment: from support library or normal one.
Try to change extends Fragment from android.app.Fragment in your fragment class.

SharedPreferences in Android AndroidRunTime Exception and NullPointerException

package com.example.cyber.sayac_say;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
int count;
Button btn;
SharedPreferences ayarlar = null;
RelativeLayout arkaplan;
Boolean ses_durumu, titresim_durumu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ayarlar = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
ayarlariYukle();
btn = (Button) findViewById(R.id.button);
arkaplan = (RelativeLayout) findViewById(R.id.rl);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
btn.setText(""+count);
}
});
}
hi , this is my codes. And when I used Shared Preferences so I have this error.But when I erase shared preferences lines so error is gone.
The problem is about Shared Prefenrences.I definitely sure.
What can I do before crazy:)?
03-22 14:16:23.972 6613-6613/com.example.cyber.sayac_say
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cyber.sayac_say/com.example.cyber.sayac_say.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
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(Native Method)
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(Native Method)
Caused by: java.lang.NullPointerException
at com.example.cyber.sayac_say.MainActivity.ayarlariYukle(MainActivity.java:76)
at com.example.cyber.sayac_say.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
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(Native Method) 
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(Native Method) 

MediaPlayer onClick crashes when start() is called

I have an app that simply displays 1 picture, implemented through Picasso. The onClick works fine if I replace the contents with a simple toast, so it must be the MediaPlayer calls. I don't know why it keeps crashing though.
package com.example.andrew.crossfade;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class MainActivity extends ActionBarActivity{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setClickable(true);
Picasso.with(this)
.load("http://i.imgur.com/vpeH7S2.jpg")
.into(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(MainActivity.this, R.raw.later);
mp.start();
}
});
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
The XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
And finally, the log cat:
10-29 12:12:07.343 18409-18409/com.example.andrew.crossfade W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41f38da0)
10-29 12:12:07.343 18409-18409/com.example.andrew.crossfade E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.andrew.crossfade, PID: 18409
java.lang.NullPointerException
at com.example.andrew.crossfade.MainActivity$1.onClick(MainActivity.java:47)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
something is null in your code
java.lang.NullPointerException
call mp.prepare() before start
and make sure R.raw.later is playable audio for media player

NullPionter Exception when passing string via Bundle

I am trying to pass a String form one activity to another in my app, however I keep getting a null pointer error. This is what shows up in my LogCat:
12-29 02:49:03.256: D/(663): HostConnection::get() New Host Connection established 0x96cb850, tid 663
12-29 02:49:06.288: W/KeyCharacterMap(663): No keyboard for id 0
12-29 02:49:06.288: W/KeyCharacterMap(663): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-29 02:49:09.600: D/AndroidRuntime(663): Shutting down VM
12-29 02:49:09.600: W/dalvikvm(663): threadid=1: thread exiting with uncaught exception (group=0xb5f444f0)
12-29 02:49:09.604: E/AndroidRuntime(663): FATAL EXCEPTION: main
12-29 02:49:09.604: E/AndroidRuntime(663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld3/com.example.helloworld3.FloorPlan}: java.lang.NullPointerException
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.os.Looper.loop(Looper.java:130)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-29 02:49:09.604: E/AndroidRuntime(663): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 02:49:09.604: E/AndroidRuntime(663): at java.lang.reflect.Method.invoke(Method.java:507)
12-29 02:49:09.604: E/AndroidRuntime(663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-29 02:49:09.604: E/AndroidRuntime(663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-29 02:49:09.604: E/AndroidRuntime(663): at dalvik.system.NativeStart.main(Native Method)
12-29 02:49:09.604: E/AndroidRuntime(663): Caused by: java.lang.NullPointerException
12-29 02:49:09.604: E/AndroidRuntime(663): at com.example.helloworld3.FloorPlan.onCreate(FloorPlan.java:24)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-29 02:49:09.604: E/AndroidRuntime(663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-29 02:49:09.604: E/AndroidRuntime(663): ... 11 more
12-29 02:49:12.316: D/(677): HostConnection::get() New Host Connection established 0x96c5790, tid 677
This is my ManActivity.java:
package com.example.helloworld3;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract.RawContacts.Data;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText destination = (EditText)findViewById(R.id.roomdinput);
final Button floorPlan = (Button)findViewById(R.id.floorPlanButton);
floorPlan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
String roomName = destination.getText().toString();
Bundle myb = new Bundle();
myb.putString("key1", roomName);
Intent a = new Intent(MainActivity.this, FloorPlan.class);
a.putExtras(myb);
startActivity(a);
startActivity(new Intent("com.example.helloworld3.FLOORPLAN"));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is my FloorPan.java:
package com.example.helloworld3;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class FloorPlan extends Activity{
DrawView2 drawView;
String roomName2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.floorplan);
Bundle myb2= getIntent().getExtras();
roomName2 = myb2.getString("key1");
drawView = new DrawView2(this);
drawView.setBackgroundResource(R.drawable.tait1st);
setContentView(drawView);
}
public class DrawView2 extends View {
Paint paint = new Paint();
float ux, dx, rx,lx;
public String getRoomName(){
return roomName2;
}
public void setCoordinates(){
if(roomName2 == "C154"){
ux =90;
dx = 250;
rx = 90;
lx = 400;
}else {
ux =76;
dx = 98;
rx = 140;
lx = 300;
}
};
public DrawView2(Context context) {
super(context);
paint.setColor(Color.RED);
//roomName2 = drawView.getTag();
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawLine(ux, dx , rx, lx, paint);
canvas.drawLine(20, 0, 0, 20, paint);
canvas.drawCircle(150, 400, 30, paint);
}
}
}
Thank you for all your help!
I think , This line in OnClickListener of floorPlan in your MainActivity causing problem :
startActivity(new Intent("com.example.helloworld3.FLOORPLAN"));
putString method is correct. It's inherited from android.os.BaseBundle.
The logcat shows your bundle get from intent.getExtras() is null:
Bundle myb2= getIntent().getExtras();
roomName2 = myb2.getString("key1");//here myb2 is null
Maybe you should override onNewIntent to setIntent:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
Why you have started activity 2 times in below code ?
Intent a = new Intent(MainActivity.this, FloorPlan.class);
a.putExtras(myb); startActivity(a);
startActivity(new Intent("com.example.helloworld3.FLOORPLAN")); // this intent doesnt`t have bundle
This might be the reason which lead where Bundle object is null in intent and causing nullpointerexception.

my android app crashes as soon as i want to make a new task

hi guys im new to android and i want to make an app.something like a daily planner.
the problem is this, my app opens up and i have the new task button, when i hit the button to add a new task the app crashes. i searched the forum for similar problems but couldn't get help.
here's my cat log file:
09-27 19:03:46.035: D/dalvikvm(19346): Late-enabling CheckJNI
09-27 19:03:46.440: D/dalvikvm(19346): GC_FOR_ALLOC freed 136K, 4% free 12874K/13319K, paused 31ms
09-27 19:03:46.440: I/dalvikvm-heap(19346): Grow heap (frag case) to 13.127MB for 379472-byte allocation
09-27 19:03:46.465: D/dalvikvm(19346): GC_FOR_ALLOC freed 0K, 4% free 13245K/13703K, paused 20ms
09-27 19:03:46.535: D/AbsListView(19346): Get MotionRecognitionManager
09-27 19:03:49.510: D/CLIPBOARD(19346): Hide Clipboard dialog at Starting input: finished by someone else... !
09-27 19:03:53.625: D/AndroidRuntime(19346): Shutting down VM
09-27 19:03:53.625: W/dalvikvm(19346): threadid=1: thread exiting with uncaught exception (group=0x40c5d1f8)
09-27 19:03:53.635: E/AndroidRuntime(19346): FATAL EXCEPTION: main
09-27 19:03:53.635: E/AndroidRuntime(19346): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.dailyplanner/com.example.dailyplanner.new_task}: java.lang.NullPointerException
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread.access$600(ActivityThread.java:128)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.os.Looper.loop(Looper.java:137)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread.main(ActivityThread.java:4514)
09-27 19:03:53.635: E/AndroidRuntime(19346): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 19:03:53.635: E/AndroidRuntime(19346): at java.lang.reflect.Method.invoke(Method.java:511)
09-27 19:03:53.635: E/AndroidRuntime(19346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-27 19:03:53.635: E/AndroidRuntime(19346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-27 19:03:53.635: E/AndroidRuntime(19346): at dalvik.system.NativeStart.main(Native Method)
09-27 19:03:53.635: E/AndroidRuntime(19346): Caused by: java.lang.NullPointerException
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.Activity.findViewById(Activity.java:1851)
09-27 19:03:53.635: E/AndroidRuntime(19346): at com.example.dailyplanner.new_task.<init>(new_task.java:19)
09-27 19:03:53.635: E/AndroidRuntime(19346): at java.lang.Class.newInstanceImpl(Native Method)
09-27 19:03:53.635: E/AndroidRuntime(19346): at java.lang.Class.newInstance(Class.java:1319)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.Instrumentation.newActivity(Instrumentation.java:1027)
09-27 19:03:53.635: E/AndroidRuntime(19346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
09-27 19:03:53.635: E/AndroidRuntime(19346): ... 11 more
this is the android manifest:
<manifest package="com.example.dailyplanner"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Light.NoTitleBar" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".new_task"/>
</application>
here is my main.java file:
package com.example.dailyplanner;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null );
//db.execSQL("CREATE TABLE IF NOT EXISTS Data1 (Id INT IDENTITY(1,1) NOT NULL, text VARCHAR, time TIME, date DATE)");
Button nTaskBtn = (Button) findViewById(R.id.nTask);
nTaskBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent nTaskScreen = new Intent(getApplicationContext() , new_task.class);
startActivity(nTaskScreen);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
and this is the new_task.java file:
package com.example.dailyplanner;
import android.R.string;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.net.UrlQuerySanitizer.ValueSanitizer;
import android.os.Bundle;
import android.renderscript.Sampler.Value;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class new_task extends Activity {
static final int TIME_DIALOG_ID=0;
final TextView timeText = (TextView) findViewById(R.id.timeTxt);
TextView dateText = (TextView) findViewById(R.id.dateTxt);
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.new_task);
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
Button btnSetTime = (Button) findViewById(R.id.btnSetTime);
btnSetTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timeText.setText(hourOfDay+":"+minute);
};
};
protected Dialog onCreateDialog(int id){
switch(id){
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, 0, 0, true);
}
return null;
}
Any help?
You can only use findViewById after you set the content view. It is also useful to null check variables before accessing them. Finally, make sure that the layout file R.layout.new_task contains items with the ids timeTxt, dateTxt, btnSave.
I re-wrote some of your work to give you an example of what I mean:
private TextView timeText;
private TextView dateText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_task);
timeText = (TextView) findViewById(R.id.timeTxt);
dateText = (TextView) findViewById(R.id.dateTxt);
Button btnSave = (Button) findViewById(R.id.btnSave);
if (btnSave != null) {
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
} else {
Log.e("MyActivity", "btnSave was null :(");
}
}
You have to call findViewById() in onCreate() after the call to setContentView().

Categories