Android: App crashes on clicking item of Quick action

577 views Asked by At

In my app, I am opening multiple activities under single tab. In one of the tabs I have quick action. I want to open another activity on click of item of Quick action, but app crashes when I do so. I have used Activity group for maintaining multiple activities under one activity. My first tab contains listview. On item click of list view I am opening another activity. The same activity I want to open with Quick action's item click as well. I am posting my code below.

Code for quick action

quickAction = new QuickAction(this, QuickAction.VERTICAL);

    ActionItem nextItem     = new ActionItem(ID_DOWN, "About TIE", getResources().getDrawable(R.drawable.dzologo));
    ActionItem prevItem     = new ActionItem(ID_UP, "About LKS", getResources().getDrawable(R.drawable.dzologo));

        prevItem.setSticky(true);
        nextItem.setSticky(true);

      final QuickAction quickAction = new QuickAction(this, QuickAction.VERTICAL);

        //add action items into QuickAction
        quickAction.addActionItem(nextItem);
        quickAction.addActionItem(prevItem);

//Set listener for action item clicked
        quickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {

            public void onItemClick(com.dzo.tie.quickaction.QuickAction source,
                    int pos, int actionId) {

                if(actionId == ID_DOWN)
                {
                    Intent  iInfo = new Intent(Awesome.this, TIEInfo.class);
                    startActivity(iInfo);
                    quickAction.dismiss();
                }//if
            }           
        });

Code for adding tabspec

addTab("Home", R.drawable.tab_home, TabGroup1Activity.class);

//Add tab method
private void addTab(String labelId, int drawableId, Class<?> c)
{
    Intent intent = null;
    intent = new Intent(this, c);

    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 
    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText(labelId);
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);      
    spec.setIndicator(tabIndicator);    
    spec.setContent(intent);

    tabHost.addTab(spec);   
}

Tabgroup1 Activity

public class TabGroup1Activity extends TabGroupActivity{

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    startChildActivity("MenuActivity", new Intent(getParent(), MenuActivity.class));
}
}

OnItem click of listview in menu activity

    public void onItemClick(AdapterView<?> parent, View v, int pos, long id)
{
    handleClick(pos);
}

private void handleClick(int position) 
{
    if(position==0)
    {
        Intent  tieInfoIntent = new Intent(getParent(), TIEInfo.class);
        TabGroupActivity parentActivity = (TabGroupActivity)getParent();
        parentActivity.startChildActivity("TIEInfoActivity", tieInfoIntent);
    }//if
}      

TIEInfo This activity I want to open on item click quick action.

public class TIEInfo extends Activity 
{
WebView webTieInfo;
String contents;
HeaderBar headerbar;
private String infoUrl = "https://www.tradeineu.com/tie_app/aboutTie.php";
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    init();
        new InfoAsyncTask(getParent(), infoUrl, webTieInfo).execute();
}//onCreate

private void init()
{
    setContentView(R.layout.tieinfo);
    headerbar = (HeaderBar)findViewById(R.id.headerBar);
    headerbar.setTitle("Info");
    webTieInfo = (WebView)findViewById(R.id.webTieInfo);
    webTieInfo.setVerticalFadingEdgeEnabled(true);
    webTieInfo.setVerticalScrollBarEnabled(true);
    webTieInfo.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
}//init
}//TIEInfo

Logcat trace

10-31 07:50:49.148: E/AndroidRuntime(1012): FATAL EXCEPTION: main
10-31 07:50:49.148: E/AndroidRuntime(1012): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dzo.tie/com.dzo.tie.TIEInfo}: java.lang.NullPointerException
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.os.Looper.loop(Looper.java:137)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at java.lang.reflect.Method.invokeNative(Native Method)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at java.lang.reflect.Method.invoke(Method.java:511)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at dalvik.system.NativeStart.main(Native Method)
10-31 07:50:49.148: E/AndroidRuntime(1012): Caused by: java.lang.NullPointerException
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.AlertDialog.<init>(AlertDialog.java:98)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at com.dzo.tie.asynctask.InfoAsyncTask.onPreExecute(InfoAsyncTask.java:43)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.os.AsyncTask.execute(AsyncTask.java:534)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at com.dzo.tie.TIEInfo.onCreate(TIEInfo.java:31)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.Activity.performCreate(Activity.java:5008)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
10-31 07:50:49.148: E/AndroidRuntime(1012):     ... 11 more

From the log, I came to know to app is crashing at the statement below which is inside onPreExecute() of AsyncTask. But, I am unable to point actual reason.

pd = new ProgressDialog(ctx);

Async Task

public class InfoAsyncTask extends AsyncTask<Void, Void, JSONObject>
{
Context ctx;
ProgressDialog pd; 
JSONParser jsonParser;
String url;
WebView webInfo;
public static TieInfoDAO tieInfoDao;
public static ComplianceInfoDAO complianceInfoDAO;
public static TAXInfoDAO taxDAO;
public static LogisticsDAO logisticsDAO;
public static LegalDAO legalDAO;
public static SmartAppDAO smartAppDAO;
private final String TAG = "InfoAsyncTask";

public InfoAsyncTask(Context ctx, String url, WebView webInfo) 
{
    this.ctx = ctx;
    this.url = url;
    this.webInfo = webInfo;
}//Constructor

@Override
protected void onPreExecute() 
{
    pd = new ProgressDialog(ctx);
    pd.setMessage("Please wait...");
    pd.show();
    Log.v(TAG, "onPreExecute called");
}//onPreExecute

protected JSONObject doInBackground(Void... params) 
{
    jsonParser = new JSONParser();
    JSONObject jsonObject = jsonParser.getJSONFromUrl(url);
    Log.v(TAG, "doInbackground called");
    return jsonObject;
}//doINBackground

@Override
protected void onPostExecute(JSONObject jsonObject) 
{
    String contents = null;

    if(url.contains("aboutTie"))
    {
        tieInfoDao = ParsedInfoData.getTIEInfo(jsonObject);
        contents = tieInfoDao.getInfo();
        showWebView(contents);
    }//if   
    else if(url.contains("compliance"))
    {
        complianceInfoDAO = ParsedComplianceInfoData.getComplianceInfo(jsonObject);
        contents = complianceInfoDAO.getComplianceInfo();
        showWebView(contents);
    }//else if
    else if(url.contains("tax"))
    {
        taxDAO = ParsedTaxData.getTaxInfo(jsonObject);
        contents = taxDAO.getTaxInfo();
        showWebView(contents);
    }//else if
    else if(url.contains("logistics"))
    {
        logisticsDAO = ParsedLogisticsData.getLogisticsInfo(jsonObject);
        contents = logisticsDAO.getLogisticsInfo();
        showWebView(contents);
    }//else if
    else if(url.contains("legal"))
    {
        legalDAO = ParsedLegalInfo.getLegalInfo(jsonObject);
        contents = legalDAO.getLegal_Info();
        showWebView(contents);
    }//else if
    else if(url.contains("smart_app"))
    {
        smartAppDAO = ParsedAppData.getAppInfo(jsonObject);
        contents = smartAppDAO.getAppInfo();
        showWebView(contents);
    }//else if
    pd.dismiss();
    Log.v(TAG, "onPostExecute called");
}//onPostExecute

private void showWebView(String contents) 
{
    if ((ctx.getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 1)
    {     
        contents = "<html><head>"
                  + "<style type=\"text/css\">body{color: #A17339;" +
                  "font-size:10px;" +
                  "text-align:justify;}"
                  + "</style></head>"
                  + "<body>"                          
                  + contents
                  + "</body></html>";
            Log.v(TAG+" onPostExecute if ldpi", "font setting for ldpi");
    }
    else if ((ctx.getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 2) 
    {     
        contents = "<html><head>"
              + "<style type=\"text/css\">body{color: #A17339;" +
              "font-size:12px;" +
              "text-align:justify;}"
              + "</style></head>"
              + "<body>"                          
              + contents
              + "</body></html>";
        Log.v(TAG+" onPostExecute if mdpi", "font setting for mdpi");
    } 
    else if ((ctx.getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 3)
    {     
        contents = "<html><head>"
              + "<style type=\"text/css\">body{color: #A17339;" +
              "font-size:18px;" +
              "text-align:justify;}"
              + "</style></head>"
              + "<body>"                          
              + contents
              + "</body></html>";
        Log.v(TAG+" onPostExecute if hdpi", "font setting for hdpi");
    }
    else if ((ctx.getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 4) {     
        contents = "<html><head>"
              + "<style type=\"text/css\">body{color: #A17339;" +
              "font-size:28px;" +
              "text-align:justify;}"
              + "</style></head>"
              + "<body>"                          
              + contents
              + "</body></html>";
        Log.v(TAG+" onPostExecute if xhdpi", "font setting for xhdpi");
    }
    //tieInfoObj.setInfo(contents);
    webInfo.loadData(contents, "text/html", "utf-8");
}//showWebView
}//TieInfoAsyncTask
1

There are 1 answers

2
Cat On
new InfoAsyncTask(getParent(), infoUrl, webTieInfo).execute();

getParent() is a bit testy, and only works if your Activity is embedded within a parent. Therefore, in this case it appears to be resulting in null.

Try the following instead, since you are creating this AsyncTask from within an Activity, which is also a Context:

new InfoAsyncTask(this, infoUrl, webTieInfo).execute();