1

I am making a widget and it installs fine to any device or emulator, but when I go to open an activity by pressing on the widget, I get this error:

10-16 11:28:43.585: E/AndroidRuntime(14065): FATAL EXCEPTION: main
10-16 11:28:43.585: E/AndroidRuntime(14065): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.awesomefilebuilderwidget/com.example.awesomefilebuilderwidget.Drag_and_Drop_App}: java.lang.NullPointerException
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1833)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1854)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.os.Looper.loop(Looper.java:150)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread.main( ActivityThread.java:4333)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at java.lang.reflect.Method.invokeNative(Native Method)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at java.lang.reflect.Method.invoke(Method.java:507)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at dalvik.system.NativeStart.main(Native Method)
10-16 11:28:43.585: E/AndroidRuntime(14065): Caused by: java.lang.NullPointerException
10-16 11:28:43.585: E/AndroidRuntime(14065):    at com.example.awesomefilebuilderwidget.Drag_and_Drop_App.onCreate(Drag_and_Drop_App.java:46)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
10-16 11:28:43.585: E/AndroidRuntime(14065):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1797)
10-16 11:28:43.585: E/AndroidRuntime(14065):    ... 11 more

It says there is an error on line 46, but I can't figure out what the error is. Here is my coding:

package com.example.awesomefilebuilderwidget;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set layout for the main screen
    setContentView(R.layout.drag_and_drop_app);
    // import buttons
    Button btnLinkToFeedback = (Button) findViewById(R.id.btnLinkToFeedback);

    // Link to Feedback Screen
    btnLinkToFeedback.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    Feedback.class);
            startActivity(i);
            finish();
        }
    });
    // create new adapter
    AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
    // set adapter to list view
    mListAppInfo.setAdapter(adapter);
    // search bar
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            // Drag_and_Drop_App.this.adapter.getFilter().filter(cs);  
            // Drag_and_Drop_App.this.adapter.getFilter().filter(cs);

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                          
        }
        });
     // load list application
    mListAppInfo = (ListView)findViewById(R.id.lvApps);

    // implement event when an item on list view is selected
    mListAppInfo.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView parent, View view, int pos, long id) {
            // get the list adapter
            AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
            // get selected item on the list
            ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
            // launch the selected application
            Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
        }

    });

    // implement event when an item on list view is selected via long-click for drag and drop
    mListAppInfo.setOnItemLongClickListener(new OnItemLongClickListener(){

        @Override
        public boolean onItemLongClick(AdapterView parent, View view,
                int pos, long id) {
            // TODO Auto-generated method stub
            // get the list adapter
            AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
            // get selected item on the list
            ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
            // launch the selected application
            Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
            return true;
        }


    });
}
}

This is line 46:

 mListAppInfo.setAdapter(adapter);

3 Answers 3

2

You have never assigned anything to mListAppInfo, so it is still null, and you get an exception when trying to access it.

Sign up to request clarification or add additional context in comments.

Comments

2

You just didn't initialize mListAppInfo before executing the method on it. You initialized it where it says somewhere like: mListAppInfo = (ListView)findViewById(R.id.lvApps);

Try initializing it before (I meant, try to declare it before executing the method on it), I hope that helps :D

1 Comment

I moved the line where I assigned it a value up before I set the Adapter and it worked. Thanks!
1

mListAppInfo is null because you havent' yet initialized it anywhere. You declare it here

private ListView mListAppInfo;

but you never give it a value. Something like

setContentView(R.layout.drag_and_drop_app);
mListAppInfo = (ListView) findViewById(R.id.myLV);  // where myLV is the id of your 
                                                    // ListView in drag_and_drop_app.xml

Edit

After looking at your code further, I see you do it later

 mListAppInfo = (ListView)findViewById(R.id.lvApps);

but this needs to be done before you try to set the Adapter on it or use it in any other way.

1 Comment

I moved the line where I assigned it a value up before I set the Adapter and it worked. Thanks!

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.