NullPointerException. Cannot create activity - java

I can't figure out where error is occurring. Here is the stack trace:
08-16 18:00:36.455: E/AndroidRuntime(22834): FATAL EXCEPTION: main
08-16 18:00:36.455: E/AndroidRuntime(22834): Process: org.example.calcu, PID: 22834
08-16 18:00:36.455: E/AndroidRuntime(22834): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.calcu/org.example.calcu.Menu}: java.lang.NullPointerException
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread.access$800(ActivityThread.java:144)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.os.Handler.dispatchMessage(Handler.java:102)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.os.Looper.loop(Looper.java:136)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread.main(ActivityThread.java:5158)
08-16 18:00:36.455: E/AndroidRuntime(22834): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 18:00:36.455: E/AndroidRuntime(22834): at java.lang.reflect.Method.invoke(Method.java:515)
08-16 18:00:36.455: E/AndroidRuntime(22834): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
08-16 18:00:36.455: E/AndroidRuntime(22834): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
08-16 18:00:36.455: E/AndroidRuntime(22834): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
08-16 18:00:36.455: E/AndroidRuntime(22834): at dalvik.system.NativeStart.main(Native Method)
08-16 18:00:36.455: E/AndroidRuntime(22834): Caused by: java.lang.NullPointerException
08-16 18:00:36.455: E/AndroidRuntime(22834): at org.example.calcu.Menu.onCreate(Menu.java:79)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.Activity.performCreate(Activity.java:6084)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-16 18:00:36.455: E/AndroidRuntime(22834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
08-16 18:00:36.455: E/AndroidRuntime(22834): ... 12 more
This is where I try to start activity in my MainActivity:
empty is Button (fairly long and empty - no text in it). I swipe my finger left to create new intent - Menu
empty = (Button) findViewById(R.id.empty);
empty.setOnTouchListener(new OnSwipeTouchListener(this){
#Override
public void onSwipeLeft() {
Intent ent;
ent = new Intent(MainActivity.this, Menu.class);
startActivity(ent);
}
});
This is activity that I try to create, Menu:
package org.example.calcu;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class Menu extends MainActivity{
Button blueTheme;
View menu;
class OnSwipeTouchListener implements OnTouchListener{
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("LOG", "menu opened!");
setContentView(R.layout.menu);
menu = (View) findViewById(R.layout.menu);
menu.setOnTouchListener(new OnSwipeTouchListener(this){ /* this is line 79 */
#Override
public void onSwipeRight(){
setContentView(R.layout.activity_main);
}
});
blueTheme = (Button) findViewById(R.id.blueTheme);
blueTheme.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
one.setBackgroundColor(Color.parseColor("#90CAF9"));
}
});
}
}
Thanks for your help, and sorry I am pretty beginner in programming.

menu = (View) findViewById(R.layout.menu);
findViewById takes an id as parameter not a layout.
The could should be:
menu = (View) findViewById(R.id.menu);
but, of course you also should have a view with id menu in your xml layout.
To finish your activity and get back to the previous one:
menu.setOnTouchListener(new OnSwipeTouchListener(this){
#Override public void onSwipeRight()
{
Menu.this.finish();
}
}

Related

"Application has stopped" when filling ListView

Well, I have this problem and I don't know why because Eclipse doesn't tells me which the error is. I'm a beginner in Java and I don't know what to do. Here's my code:
package com.example.asado;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
public class ListaActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set View to register.xml
setContentView(R.layout.listas);
addListenerOnButton();
final ListView listview = (ListView) findViewById(R.id.lista_compra);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN) #Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction(new Runnable() {
#Override
public void run() {
list.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
});
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
#Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
}
public void addListenerOnButton() {
ImageButton compra = (ImageButton) findViewById(R.id.compra);
// Listening to Login Screen link
compra.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Cierro la ventana
// cambio de pesta�a
finish();
}
});
}
}
It's a bit messy but the error is that when this layout is about to open, I get the error "Unfortunately the application has stopped". Thanks in advance.
These are the Errors at Logcat:
07-23 16:05:06.509: E/AndroidRuntime(8794): FATAL EXCEPTION: main
07-23 16:05:06.509: E/AndroidRuntime(8794): Process: com.example.asado, PID: 8794
07-23 16:05:06.509: E/AndroidRuntime(8794): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asado/com.example.asado.ListaActivity}: java.lang.NullPointerException
07-23 16:05:06.509: E/AndroidRuntime(8794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
07-23 16:05:06.509: E/AndroidRuntime(8794): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
07-23 16:05:06.509: E/AndroidRuntime(8794): at android.app.ActivityThread.access$800(ActivityThread.java:139)
07-23 16:05:06.509: E/AndroidRuntime(8794): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
07-23 16:05:06.509: E/AndroidRuntime(8794): at android.os.Handler.dispatchMessage(Handler.java:102)
07-23 16:05:06.509: E/AndroidRuntime(8794): at android.os.Looper.loop(Looper.java:136)
07-23 16:05:06.509: E/AndroidRuntime(8794): at android.app.ActivityThread.main(ActivityThread.java:5102)
07-23 16:05:06.509: E/AndroidRuntime(8794): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 16:05:06.509: E/AndroidRuntime(8794): at java.lang.reflect.Method.invoke(Method.java:515)
07-23 16:05:06.509: E/AndroidRuntime(8794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-23 16:05:06.509: E/AndroidRuntime(8794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-23 16:05:06.509: E/AndroidRuntime(8794): at dalvik.system.NativeStart.main(Native Method)
07-23 16:05:06.509: E/AndroidRuntime(8794): Caused by: java.lang.NullPointerException
07-23 16:05:06.509: E/AndroidRuntime(8794): at com.example.asado.ListaActivity.onCreate(ListaActivity.java:39)
07-23 16:05:06.509: E/AndroidRuntime(8794): at android.app.Activity.performCreate(Activity.java:5248)
07-23 16:05:06.509: E/AndroidRuntime(8794): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
07-23 16:05:06.509: E/AndroidRuntime(8794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
07-23 16:05:06.509: E/AndroidRuntime(8794): ... 11 more
Try to replace:
#Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
with this:
#Override
public long getItemId(int position) {
return position;
}
ListActivity's line 39 has nullpointer see if list is set correctly, if you look at this example List works better if its id is created #android:id/list instead of created by giving id value.
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

Fragment is trying to cast to activity

I know this answer is probably simple but i cannot figure it out. I am modifying example code for a project and keep getting a ClassCastException when i try to launch the app. Says that ListActivityFragment cannot be cast to android.app.Activity. What im not understanding(which is probably the simple part) is why its trying to cast it onto an activity when my main is a fragmentactivity. I know the code is rough, still trying to finish it up but cant get past this.
FragmentActivity
public class MainActivity extends FragmentActivity {
private boolean isLargeScreen = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.normal_screen_layout) != null) {
isLargeScreen = false;
ListActivityFragment listActivityFragment = new ListActivityFragment();
listActivityFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.normal_screen_layout, listActivityFragment)
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addItem:
ListActivityFragment newFragment = new ListActivityFragment();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.normal_screen_layout, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
}
ListActivity
public class ListActivityFragment extends ListFragment implements
OnClickListener {
private ArrayList<String> items = new ArrayList<String>();
private ListView listView;
private View outerView;
private Button deleteButton;
private ListActivityListener listener;
public interface ListActivityListener {
public void meetingAdded();
}
public ListActivityFragment() {
}
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (ListActivityListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
outerView = inflater.inflate(R.layout.list_of_meetings, container,
false);
listView = (ListView) outerView.findViewById(R.id.listView1);
deleteButton = (Button) outerView.findViewById(R.id.deleteButton);
createList();
return outerView;
}
public void createList() {
items.clear();
Iterator<Items> iterator = ItemCollection.Instance().iterator();
while (iterator.hasNext()) {
items.add(iterator.next().toString());
}
listView.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, items));
}
#Override
public void onClick(DialogInterface arg0, int arg1) {
ListView listView = getListView();
for (int index = 0; index < listView.getChildCount(); index++) {
View viewGroup = listView.getChildAt(index);
CheckBox checkBox = (CheckBox) viewGroup
.findViewById(R.id.itemSelectCheckBox);
if (checkBox.isChecked()) {
TextView textView = (TextView) viewGroup
.findViewById(R.id.productTextView);
int key = (Integer) textView.getTag();
ItemCollection.Instance().delete(key);
}
}
createList();
}
#Override
public void onStart() {
super.onStart();
}
public void onListItemClick(ListView l, View v, int position, long id) {
getListView().setItemChecked(position, true);
}
}
And heres the Logcat
07-15 01:59:50.710: E/AndroidRuntime(878): FATAL EXCEPTION: main
07-15 01:59:50.710: E/AndroidRuntime(878): Process: com.example.assignment5, PID: 878
07-15 01:59:50.710: E/AndroidRuntime(878): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.assignment5/com.example.assignment5.ListActivityFragment}: java.lang.ClassCastException: com.example.assignment5.ListActivityFragment cannot be cast to android.app.Activity
07-15 01:59:50.710: E/AndroidRuntime(878): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-15 01:59:50.710: E/AndroidRuntime(878): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-15 01:59:50.710: E/AndroidRuntime(878): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-15 01:59:50.710: E/AndroidRuntime(878): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-15 01:59:50.710: E/AndroidRuntime(878): at android.os.Handler.dispatchMessage(Handler.java:102)
07-15 01:59:50.710: E/AndroidRuntime(878): at android.os.Looper.loop(Looper.java:136)
07-15 01:59:50.710: E/AndroidRuntime(878): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-15 01:59:50.710: E/AndroidRuntime(878): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 01:59:50.710: E/AndroidRuntime(878): at java.lang.reflect.Method.invoke(Method.java:515)
07-15 01:59:50.710: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-15 01:59:50.710: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-15 01:59:50.710: E/AndroidRuntime(878): at dalvik.system.NativeStart.main(Native Method)
07-15 01:59:50.710: E/AndroidRuntime(878): Caused by: java.lang.ClassCastException: com.example.assignment5.ListActivityFragment cannot be cast to android.app.Activity
07-15 01:59:50.710: E/AndroidRuntime(878): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-15 01:59:50.710: E/AndroidRuntime(878): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-15 01:59:50.710: E/AndroidRuntime(878): ... 11 more
07-15 01:59:51.160: E/NetdConnector(383): NDC Command {65 bandwidth setiquota eth0 9223372036854775807} took too long (1117ms)
Caused by: java.lang.ClassCastException:
com.example.assignment5.ListActivityFragment cannot be cast to
android.app.Activity 07-15 01:59:50.710: E/AndroidRuntime(878):
As the error says -
ListActivityFragment is not an activity. So you are trying to access it just like an activity.
Make sure you haven't declared ListActivityFragment in the manifest. In your manifest if you made an entry for the fragment then that's wrong.
Also make sure that you haven't used startActivity for the fragment. Because this is done for activity class and not for fragment class.

Android App FC once it tries to get an int in OnCreate() Method

The app has two activities, PlanetListViewActivity.java and MainActivity.java. The problem seems to be on the line:
int pos = b.getInt("com.biekerwebdesign.scrollingplanets.Position=", "-"+pos);
When that line is commented out, the app opens and doesn't FC.
package com.biekerwebdesign.scrollingplanets;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.textView1);
Bundle b = getIntent().getExtras();
int pos = b.getInt("com.biekerwebdesign.scrollingplanets.Position");
Log.i("com.biekerwebdesign.scrollingplanets.Position=", "-"+pos);
if(pos==0) {
tv.setTextSize(20);
tv.setText("MERCURY \n Aphelion \n69,816,900 km\n0.466 697 AU\nPerihelion\n 46,001,200 km\n");
}
if(pos==2) {
tv.setText("earth");
}
}
#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;
}
}
Here is the PlanetListViewActivity.java:
package com.biekerwebdesign.scrollingplanets;
import java.util.ArrayList;
import java.util.Arrays;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.content.Intent;
//import android.util.Log;
public class PlanetListViewActivity extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// Create and populate a List of planet names.
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune",
"Alpha Centari A"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
//my change
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.planetrow, planetList);
// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );
listAdapter.remove( "Alpha Centari A");
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
//Log.i("m", "-"+pos);
Intent myIntent = new Intent(PlanetListViewActivity.this,
MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.putExtra("com.biekerwebdesign.scrollingplanets.Position", pos);
startActivity(myIntent);
}
});
}
}
And here is my Logcat:
10-10 08:43:03.323: D/AndroidRuntime(29285): Shutting down VM
10-10 08:43:03.323: W/dalvikvm(29285): threadid=1: thread exiting with uncaught exception (group=0x418a7700)
10-10 08:43:03.328: E/AndroidRuntime(29285): FATAL EXCEPTION: main
10-10 08:43:03.328: E/AndroidRuntime(29285): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.biekerwebdesign.scrollingplanets/com.biekerwebdesign.scrollingplanets.MainActivity}: java.lang.NullPointerException
10-10 08:43:03.328: E/AndroidRuntime(29285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
10-10 08:43:03.328: E/AndroidRuntime(29285): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
10-10 08:43:03.328: E/AndroidRuntime(29285): at android.app.ActivityThread.access$600(ActivityThread.java:153)
10-10 08:43:03.328: E/AndroidRuntime(29285): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
10-10 08:43:03.328: E/AndroidRuntime(29285): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 08:43:03.328: E/AndroidRuntime(29285): at android.os.Looper.loop(Looper.java:137)
10-10 08:43:03.328: E/AndroidRuntime(29285): at android.app.ActivityThread.main(ActivityThread.java:5289)
10-10 08:43:03.328: E/AndroidRuntime(29285): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 08:43:03.328: E/AndroidRuntime(29285): at java.lang.reflect.Method.invoke(Method.java:525)
10-10 08:43:03.328: E/AndroidRuntime(29285): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-10 08:43:03.328: E/AndroidRuntime(29285): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-10 08:43:03.328: E/AndroidRuntime(29285): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
10-10 08:43:03.328: E/AndroidRuntime(29285): at dalvik.system.NativeStart.main(Native Method)
10-10 08:43:03.328: E/AndroidRuntime(29285): Caused by: java.lang.NullPointerException
10-10 08:43:03.328: E/AndroidRuntime(29285): at com.biekerwebdesign.scrollingplanets.MainActivity.onCreate(MainActivity.java:17)
10-10 08:43:03.328: E/AndroidRuntime(29285): at android.app.Activity.performCreate(Activity.java:5133)
10-10 08:43:03.328: E/AndroidRuntime(29285): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-10 08:43:03.328: E/AndroidRuntime(29285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
10-10 08:43:03.328: E/AndroidRuntime(29285): ... 12 more
So basically, when I do not try to get the int pos in the OnCreate() method, the app opens up but doesn't show anything. But when I try to get the int pos, the app FC at start. How would I get the int pos in MainActivity without the app FC?
You have a NullPointerException in MainActivity on line 17, because getIntent().getExtras() may be null.
You must first check if b != null.
That means:
int pos;
if (b == null) {
pos = 0;
}
else {
pos = b.getInt("com.biekerwebdesign.scrollingplanets.Position");
}
The stack trace from the LogCat that you have posted is really helpful in such situations. You should always look for the Caused by: ... lines.

activity crashes when going back home

I have an edit task activity with a single edittext view. When i click the back button on the action bar, the database is updated and the log even registers it, but the activity crashes while going back. The main activity does display the text that was added in the task activity.
Here is the code for the home button:
public class NewTask extends Activity {
protected TaskerDbHelper db;
MyAdapter adapt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_task);
db = new TaskerDbHelper(this);
setupActionBar();
}
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_task, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
EditText t = (EditText) findViewById(R.id.editText1);
String s = t.getText().toString();
if (s.equalsIgnoreCase("")) {
Toast.makeText(this, "enter the task description first!!",
Toast.LENGTH_LONG);
} else {
Task task = new Task(s, 0);
db.addTask(task);
Log.d("tasker", "data added");
//t.setText("");
//adapt.add(task);
adapt.notifyDataSetChanged();
}
setResult(RESULT_OK);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Whats wrong?
Here is the log:
08-16 17:56:05.117: E/AndroidRuntime(6162): FATAL EXCEPTION: main
08-16 17:56:05.117: E/AndroidRuntime(6162): java.lang.NullPointerException
08-16 17:56:05.117: E/AndroidRuntime(6162): at com.example.tasker.NewTask.onOptionsItemSelected(NewTask.java:62)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.app.Activity.onMenuItemSelected(Activity.java:2552)
08-16 17:56:05.117: E/AndroidRuntime(6162): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:167)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.view.View.performClick(View.java:4204)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.view.View$PerformClick.run(View.java:17355)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.os.Handler.handleCallback(Handler.java:725)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.os.Handler.dispatchMessage(Handler.java:92)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.os.Looper.loop(Looper.java:137)
08-16 17:56:05.117: E/AndroidRuntime(6162): at android.app.ActivityThread.main(ActivityThread.java:5234)
08-16 17:56:05.117: E/AndroidRuntime(6162): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 17:56:05.117: E/AndroidRuntime(6162): at java.lang.reflect.Method.invoke(Method.java:525)
08-16 17:56:05.117: E/AndroidRuntime(6162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
08-16 17:56:05.117: E/AndroidRuntime(6162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
08-16 17:56:05.117: E/AndroidRuntime(6162): at dalvik.system.NativeStart.main(Native Method)
Try checking if the adapter is null first
if(adapt != null) {
adapt.notifyDataSetChanged();
} else {
// Initialize and do other stuff with it
}
And in the declaration, declare adapt like this
public MyAdapter adapt;
I can see that your using a custom adapter class. I think the problem may also be there in the notifyDataSetChanged() method. Can you post it?
if(adapt != null) {
adapt.notifyDataSetChanged();
} else {
adapt = new Adapter();
listview.setAdapter(adapt);
}
check the answer from Noah. he is overriding the notify() to access the BaseAdapter
https://stackoverflow.com/a/5327197/2074990

How come this Fragment crashes my program on rotate?

I have the following Fragment. It works completely fine, until I rotate my device. It then crashes with errors about the RadarSelectionFragment failing to Instantiate. The code for the FragmentPagerAdapter and the Fragment in question are below:
#SuppressLint("DefaultLocale")
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter() {
// Do some stuff
}
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0) {
Fragment fragment = new RadarSelectionFragment();
Bundle args = new Bundle();
args.putInt(RadarSelectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
} else if (position == 1) {
Fragment fragment = new WeatherMapDisplayFragment();
Bundle args = new Bundle();
args.putInt(WeatherMapDisplayFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
} else {
Fragment tf = new RadarSelectionFragment();
return tf;
}
}
#Override
public int getCount() {
return 2;
}
#SuppressLint("DefaultLocale")
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase();
case 1:
return getString(R.string.title_section2).toUpperCase();
}
return null;
}
}
public static class RadarSelectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public RadarSelectionFragment() {}
#Override
public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
return inflater.inflate(R.layout.radars, container, false);
} else {
return container;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
...
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
}
}
public class WeatherMapDisplayFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public WeatherMapDisplayFragment() {
}
#Override
public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
return inflater.inflate(R.layout.display, container, false);
} else {
return container;
}
}
}
I have tried Googling the problem, but all I keep turning up are various solutions pertaining to ensuring that the Fragment Class is static (Which I've done).
I am relatively new to Android programming, so if you answer, could you please either post an example and/or link to other examples if you have the time.
Thanks in advance!
EDIT 1: Stacktrace
01-21 13:46:31.907: E/AndroidRuntime(1101): FATAL EXCEPTION: main
01-21 13:46:31.907: E/AndroidRuntime(1101): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radarau/com.example.radarau.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.radarau.MainActivity$WeatherMapDisplayFragment: make sure class name exists, is public, and has an empty constructor that is public
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.app.ActivityThread.access$700(ActivityThread.java:141)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.os.Looper.loop(Looper.java:137)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-21 13:46:31.907: E/AndroidRuntime(1101): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 13:46:31.907: E/AndroidRuntime(1101): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 13:46:31.907: E/AndroidRuntime(1101): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 13:46:31.907: E/AndroidRuntime(1101): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 13:46:31.907: E/AndroidRuntime(1101): at dalvik.system.NativeStart.main(Native Method)
01-21 13:46:31.907: E/AndroidRuntime(1101): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.radarau.MainActivity$WeatherMapDisplayFragment: make sure class name exists, is public, and has an empty constructor that is public
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.support.v4.app.Fragment.instantiate(Fragment.java:405)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
01-21 13:46:31.907: E/AndroidRuntime(1101): at com.example.radarau.MainActivity.onCreate(MainActivity.java:35)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.app.Activity.performCreate(Activity.java:5104)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-21 13:46:31.907: E/AndroidRuntime(1101): ... 12 more
01-21 13:46:31.907: E/AndroidRuntime(1101): Caused by: java.lang.InstantiationException: can't instantiate class com.example.radarau.MainActivity$WeatherMapDisplayFragment; no empty constructor
01-21 13:46:31.907: E/AndroidRuntime(1101): at java.lang.Class.newInstanceImpl(Native Method)
01-21 13:46:31.907: E/AndroidRuntime(1101): at java.lang.Class.newInstance(Class.java:1319)
01-21 13:46:31.907: E/AndroidRuntime(1101): at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
01-21 13:46:31.907: E/AndroidRuntime(1101): ... 19 more
Your logcat says
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.radarau.MainActivity$WeatherMapDisplayFragment: make sure class name exists, is public, and has an empty constructor that is public
You should provide a public Default constructor for that
public WeatherMapDisplayFragment() {
// Do some stuff
}
If that class is nested, make that class to static class and provide a public default constructor or move that fragment into a new java file.

Categories