1

I am quite new to Java and Android programming, and just working on my 3rd App - however I am encountering a Problem which I am not able to solve (I already tried and researched for days).

I have a list of Elements in a LinearLayout inside a Fragment - the Elements have a OnLongClickListener attached, with a popup showing up when the User long-presses the Element. That works fine. However, one of the Buttons in the Popup is "Edit", and when a user presses this Button, I want to start another Activity with Editing Options for this Element.

So there comes my Problem: when I want to start the new Activity with an Intent, I get following Error:

E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.NullPointerException
        at android.app.Activity.startActivityForResult(Activity.java:3370)
        at android.app.Activity.startActivityForResult(Activity.java:3331)
        at android.app.Activity.startActivity(Activity.java:3566)
        at android.app.Activity.startActivity(Activity.java:3534)
        at at.fekle.zipmanager.start.startzipinfo(start.java:299)
        at at.fekle.zipmanager.start$2.onLongClick(start.java:277)
        at android.view.View.performLongClick(View.java:4247)
        at android.view.View$CheckForLongPress.run(View.java:17341)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5227)
        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:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
        at dalvik.system.NativeStart.main(Native Method)

But, if i start the same Activity with the same Intent from a simple Button defined in the Layout, the Activity Starts fine without any Errors... weird, isn't it?

So, does anyone have an Idea how I can solve this problem?

Here are some extractions from my code:

The Creation of the Elements with the OnLongClickListener:

ScrollView parent = (ScrollView) maininflater.inflate(R.layout.start_ziplist_fragment_superwrapper, null, false);
    inf = maininflater;
    LinearLayout ll = (LinearLayout) maininflater.inflate(R.layout.start_ziplist_fragment_wrapper, parent, false);

    for (Integer i = 0; i < count; i++) {
        ArrayList<String> data = new DB(cont).getAllZips().get(i);
        View custom = maininflater.inflate(R.layout.start_ziplist_fragment_inner, ll, false);
        TextView tv = (TextView) custom.findViewById(R.id.ziplist_name);
        TextView tv2 = (TextView) custom.findViewById(R.id.ziplist_text);
        tv.setText(data.get(2));
        tv2.setText(data.get(1));
        Integer id =  Integer.valueOf(data.get(0));
        custom.setId(id);

        custom.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Integer id = view.getId();
                startzipinfo(id);
                return true;
            }
        });

        ll.addView(custom);
    }
    parent.addView(ll);

The void which starts the Activity:

public void startzipinfo(Integer id){
    Intent in = new Intent(getApplicationContext(), zipinfo.class);
    startActivity(in);
}

I'm looking forward to your Answers,

Felix

EDIT: This is the Activity which is about to be started:

package at.fekle.zipmanager;
import android.app.Activity;
import android.os.Bundle;

/**
 * Created by felix on 27.06.13.
 */
public class zipinfo extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zipinfo);
    }
}
20
  • are You sure that ArrayList<String> data is not null? Maybe You cannot get the data from database for any reason Commented Jun 28, 2013 at 8:14
  • post your zipinfo.class. because error can occur in this files too. Commented Jun 28, 2013 at 8:16
  • the data is fine, my list gets filled with the data from the database, no problem there ;) Commented Jun 28, 2013 at 8:16
  • Is this your complete stacktrace? Commented Jun 28, 2013 at 8:16
  • @Nirmal OK, I'll do that Commented Jun 28, 2013 at 8:17

3 Answers 3

1

Try to use YourActivityName.this instead of getApplicationContext() :

public void startzipinfo(Integer id){
    Intent in = new Intent(YourActivityName.this, zipinfo.class);
    startActivity(in);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Check whether your new intent class is mentioned in android manifest file if it does not exists then type:<activity name=".YourClassName> </activity>

2 Comments

But he said it starts some times, but not every time
Yes, it is included :-) Like @Plato said, it starts with a normal button, the problem only exists when starting the activity from the LongClickListener...
0

I just solved the problem!

I had to start the activity from the parent Fragment (passed to the method by the FragmentManager) - now everything works like a charm :)

custom.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    Integer id = view.getId();
                    id = 2;
                    Intent intent = new Intent(cont, zipinfo.class);
                    fragment.startActivity(intent);
                    return true;
                }
            });

Thanks for all answers!

Cheers,

Felix

1 Comment

I am facing the same issue.. can you please elaborate this. what is the variable fragment? can u show its declaration too, it will be helpfull

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.